Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: BigInt Support #8382

Merged
merged 52 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
1941be0
feat(jest-get-type) include bigint
JoshRosenstein Apr 25, 2019
d39a157
(jest-matcher-utils) add ensureActualIsNumberOrBigInit
JoshRosenstein Apr 26, 2019
16a2312
(expect) add biginit support
JoshRosenstein Apr 26, 2019
3ccbce4
Revert gettype to ifelse
JoshRosenstein Apr 26, 2019
2995d69
Add tests mixing numbers and bigint
JoshRosenstein Apr 26, 2019
ff21d21
bigDumbTypo
JoshRosenstein Apr 26, 2019
4128975
babel-plugin-jest-hoist add to whitelist
JoshRosenstein Apr 26, 2019
2488c34
Wrap BigInt Tests with guard
JoshRosenstein Apr 26, 2019
9c68a16
temp fix test suite must contain at least one test
JoshRosenstein Apr 26, 2019
3e33530
remove MatchingSnapshot tests due to unsupported env having obsoletes
JoshRosenstein Apr 26, 2019
50b3bb4
remove unnecessary bigint check in equals
JoshRosenstein Apr 29, 2019
cde6327
Remove ensure*NumbersOrBigInt, include BigInt in originals, add `or b…
JoshRosenstein Apr 29, 2019
8e0656a
Update snapshots with new error message `number or bigint`
JoshRosenstein Apr 29, 2019
d1e1b88
add bigint support to toBeCloseTo, replace ensureNumbersOrBigInt to e…
JoshRosenstein Apr 29, 2019
1a2ff60
tests(expect) remove temp_test, add bigint tests to matchers.test
JoshRosenstein Apr 29, 2019
0c594cf
tests(jest-matcher-utils) remove temp_tests, add bigint tests to inde…
JoshRosenstein Apr 29, 2019
b122b02
Update CHANGELOG.md
JoshRosenstein Apr 29, 2019
c640ec3
Merge branch 'master' into BigInt
JoshRosenstein Apr 29, 2019
f5cec2a
Update CHANGELOG.md
JoshRosenstein Apr 30, 2019
5bf3acc
(babel-preset-jest)-add @babel/plugin-syntax-bigint
JoshRosenstein Apr 30, 2019
be5cbb6
Add tests for bigint syntax
JoshRosenstein Apr 30, 2019
8c1d55c
Update CHANGELOG.md
JoshRosenstein Apr 30, 2019
3459c44
:rage2: :abc:
JoshRosenstein Apr 30, 2019
592d6f3
Checking if node6 breaks with lookup
JoshRosenstein Apr 30, 2019
c2c771a
Revert "Checking if node6 breaks with lookup"
JoshRosenstein Apr 30, 2019
fb6655d
Revert "Add tests for bigint syntax"
JoshRosenstein Apr 30, 2019
0646729
Update CHANGELOG.md
pedrottimark Apr 30, 2019
9390440
Remove Default Precision Value and update error msg via code review
JoshRosenstein May 1, 2019
52d110d
Replace ensureExpectedIsNumber with ensureExpectedIsNonNegativeIntege…
JoshRosenstein May 1, 2019
5430e1a
Update Matchers interface
JoshRosenstein May 1, 2019
42e4e89
Revert Bigint Support for 'toBeCloseTo'
JoshRosenstein May 1, 2019
67d1ad8
Update CHANGELOG.md
JoshRosenstein May 1, 2019
aba6474
pedrottimark's CR fixes in tests :eyes: :dart:
JoshRosenstein May 1, 2019
b4884f5
Merge branch 'master' into BigInt
JoshRosenstein May 5, 2019
c4aea47
Merge remote-tracking branch 'upstream/master' into BigInt
JoshRosenstein May 13, 2019
f2a5745
Update CHANGELOG.md
JoshRosenstein May 13, 2019
99642be
Merge branch 'master' into BigInt
thymikee Jun 14, 2019
f0d73f8
Merge commit '1749aeab92e33fe1ae65196c75aebd2d1f366ab2' into BigInt
SimenB Sep 1, 2019
0bdddfd
Merge commit 'b64d35d127ba41a8233ded35a52a9a70981d9d53' into BigInt
SimenB Sep 1, 2019
d22ed48
Merge commit 'b2c9fe3726d81c7ea6d71115352f177afd6210ba' into BigInt
SimenB Sep 1, 2019
46d64de
Merge commit '798891208e752e084c7935e2e52e0f765a758c1e' into BigInt
SimenB Sep 1, 2019
1d9e41a
Merge commit 'a1e30075ca60fb720ae1ba9a66ba1e4837c67428' into BigInt
SimenB Sep 1, 2019
e8d9411
Merge commit '88ccda0910c83b92bcb5671dc9b0de94e5250054' into BigInt
SimenB Sep 1, 2019
615b2c4
Merge branch 'master' into BigInt
SimenB Sep 1, 2019
6164740
move changelog entries
SimenB Sep 1, 2019
ad3c832
fix changelog formatting
SimenB Sep 1, 2019
c30ae9c
Delete unnecessary disable of eslint rules
pedrottimark Sep 3, 2019
de4d01c
Update ExpectAPI.md
pedrottimark Sep 3, 2019
0e2ef7d
Update CHANGELOG.md
pedrottimark Sep 3, 2019
007919d
Edit ExpectAPI.md
pedrottimark Sep 4, 2019
2d026e9
Merge branch 'master' into BigInt
SimenB Sep 6, 2019
89b1538
Merge branch 'master' into BigInt
SimenB Sep 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ module.exports = {
],
'import/order': 'error',
'no-dupe-class-members': 'off',
// ts handles this check for us, also eslint does not include Bigint
'no-undef': 'off',
'no-unused-vars': 'off',
// ts handles this check for us, also eslint does not include bigint
'valid-typeof': 'off',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valid-typeof includes bigint starting in 6.0.0-rc.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, we can revert the eslint changes if they no longer cause issues. Makes this PR a bit bigger than it needs to be. you wanna do that?

},
},
// to make it more suitable for running on code examples in docs/ folder
Expand Down
1 change: 1 addition & 0 deletions packages/babel-plugin-jest-hoist/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const WHITELISTED_IDENTIFIERS: Set<string> = new Set([
'Array',
'ArrayBuffer',
'Boolean',
'BigInt',
'DataView',
'Date',
'Error',
Expand Down
257 changes: 257 additions & 0 deletions packages/expect/src/__tests__/bigInt_temp.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

/* global BigInt */

const {stringify} = require('jest-matcher-utils');

const chalk = require('chalk');
const jestExpect = require('../');

const chalkEnabled = chalk.enabled;

beforeAll(() => {
chalk.enabled = true;
});

afterAll(() => {
chalk.enabled = chalkEnabled;
});

describe('BigInt', () => {
test('TEMP HAPPY JEST', () => {
expect(1).toBe(1);
});

/* global BigInt */
if (typeof BigInt === 'function') {
describe('.toBe()', () => {
it('does not throw', () => {
jestExpect(BigInt(1)).not.toBe(BigInt(2));
jestExpect(BigInt(1)).toBe(BigInt(1));
});

[[BigInt(1), BigInt(2)]].forEach(([a, b]) => {
it(`fails for: ${stringify(a)} and ${stringify(b)}`, () => {
expect(() => jestExpect(a).toBe(b)).toThrow();
});
});

[BigInt(1)].forEach(v => {
it(`fails for '${stringify(v)}' with '.not'`, () => {
expect(() => jestExpect(v).not.toBe(v)).toThrow();
});
});
});
describe('.toEqual()', () => {
[[BigInt(1), BigInt(2)]].forEach(([a, b]) => {
test(`{pass: false} expect(${stringify(a)}).toEqual(${stringify(
b,
)})`, () => {
expect(() => jestExpect(a).toEqual(b)).toThrow();
jestExpect(a).not.toEqual(b);
});
});

[[BigInt(1), BigInt(1)]].forEach(([a, b]) => {
test(`{pass: true} expect(${stringify(a)}).not.toEqual(${stringify(
b,
)})`, () => {
jestExpect(a).toEqual(b);
expect(() => jestExpect(a).not.toEqual(b)).toThrow();
});
});
});

describe('.toBeTruthy(), .toBeFalsy()', () => {
[BigInt(1)].forEach(v => {
test(`'${stringify(v)}' is truthy`, () => {
jestExpect(v).toBeTruthy();
jestExpect(v).not.toBeFalsy();

expect(() => jestExpect(v).not.toBeTruthy()).toThrow();

expect(() => jestExpect(v).toBeFalsy()).toThrow();
});
});

[BigInt(0)].forEach(v => {
test(`'${stringify(v)}' is falsy`, () => {
jestExpect(v).toBeFalsy();
jestExpect(v).not.toBeTruthy();

expect(() => jestExpect(v).toBeTruthy()).toThrow();

expect(() => jestExpect(v).not.toBeFalsy()).toThrow();
});
});
});

describe('.toBeDefined(), .toBeUndefined()', () => {
[BigInt(1)].forEach(v => {
test(`'${stringify(v)}' is defined`, () => {
jestExpect(v).toBeDefined();
jestExpect(v).not.toBeUndefined();

expect(() => jestExpect(v).not.toBeDefined()).toThrow();

expect(() => jestExpect(v).toBeUndefined()).toThrow();
});
});
});

describe(
'.toBeGreaterThan(), .toBeLessThan(), ' +
'.toBeGreaterThanOrEqual(), .toBeLessThanOrEqual()',
() => {
[[BigInt(1), BigInt(2)]].forEach(([small, big]) => {
it(`{pass: true} expect(${small}).toBeLessThan(${big})`, () => {
jestExpect(small).toBeLessThan(big);
});

it(`{pass: false} expect(${big}).toBeLessThan(${small})`, () => {
jestExpect(big).not.toBeLessThan(small);
});

it(`{pass: true} expect(${big}).toBeGreaterThan(${small})`, () => {
jestExpect(big).toBeGreaterThan(small);
});

it(`{pass: false} expect(${small}).toBeGreaterThan(${big})`, () => {
jestExpect(small).not.toBeGreaterThan(big);
});

it(`{pass: true} expect(${small}).toBeLessThanOrEqual(${big})`, () => {
jestExpect(small).toBeLessThanOrEqual(big);
});

it(`{pass: false} expect(${big}).toBeLessThanOrEqual(${small})`, () => {
jestExpect(big).not.toBeLessThanOrEqual(small);
});

it(`{pass: true} expect(${big}).toBeGreaterThanOrEqual(${small})`, () => {
jestExpect(big).toBeGreaterThanOrEqual(small);
});

it(`{pass: false} expect(${small}).toBeGreaterThanOrEqual(${big})`, () => {
jestExpect(small).not.toBeGreaterThanOrEqual(big);
});

it(`throws: [${small}, ${big}]`, () => {
expect(() => jestExpect(small).toBeGreaterThan(big)).toThrow();

expect(() => jestExpect(small).not.toBeLessThan(big)).toThrow();

expect(() => jestExpect(big).not.toBeGreaterThan(small)).toThrow();

expect(() => jestExpect(big).toBeLessThan(small)).toThrow();

expect(() =>
jestExpect(small).toBeGreaterThanOrEqual(big),
).toThrow();

expect(() =>
jestExpect(small).not.toBeLessThanOrEqual(big),
).toThrow();

expect(() =>
jestExpect(big).not.toBeGreaterThanOrEqual(small),
).toThrow();

expect(() => jestExpect(big).toBeLessThanOrEqual(small)).toThrow();
});
});

[[BigInt(1), BigInt(1)]].forEach(([n1, n2]) => {
test(`equal numbers: [${n1}, ${n2}]`, () => {
jestExpect(n1).toBeGreaterThanOrEqual(n2);
jestExpect(n1).toBeLessThanOrEqual(n2);

expect(() =>
jestExpect(n1).not.toBeGreaterThanOrEqual(n2),
).toThrow();

expect(() => jestExpect(n1).not.toBeLessThanOrEqual(n2)).toThrow();
});
});
},
);

describe('.toStrictEqual()', () => {
class TestClassA {
constructor(a, b) {
this.a = a;
this.b = b;
}
}

it('passes when comparing same type', () => {
expect({
test: new TestClassA(BigInt(1), BigInt(2)),
}).toStrictEqual({test: new TestClassA(BigInt(1), BigInt(2))});
});

it('matches the expected snapshot when it fails', () => {
expect(() =>
jestExpect({
test: BigInt(2),
}).toStrictEqual({test: new TestClassA(BigInt(1), BigInt(2))}),
).toThrow();

expect(() =>
jestExpect({
test: new TestClassA(BigInt(1), BigInt(2)),
}).not.toStrictEqual({test: new TestClassA(BigInt(1), BigInt(2))}),
).toThrow();
});

/* eslint-disable no-sparse-arrays */
it('passes for matching sparse arrays', () => {
expect([, BigInt(1)]).toStrictEqual([, BigInt(1)]);
});

it('does not pass when sparseness of arrays do not match', () => {
expect([, BigInt(1)]).not.toStrictEqual([undefined, BigInt(1)]);
expect([undefined, BigInt(1)]).not.toStrictEqual([, BigInt(1)]);
expect([, , , BigInt(1)]).not.toStrictEqual([, BigInt(1)]);
});

it('does not pass when equally sparse arrays have different values', () => {
expect([, BigInt(1)]).not.toStrictEqual([, BigInt(2)]);
});
/* eslint-enable */
});

describe('Reproduce From #6829', () => {
test('It should accept BigInt("x") way', () => {
const a = BigInt('123456789012345678901234567890');
jestExpect(typeof a).toEqual('bigint');
});
test('It should allow to do equal comparision', () => {
const a = BigInt(2);
jestExpect(a).toEqual(BigInt(2));
});
test('It should allow to do greater than comparision', () => {
const a = BigInt(2);
jestExpect(a).toBeGreaterThan(1);
});
test('It should allow to do greater than or equal comparision', () => {
const a = BigInt(2);
jestExpect(a).toBeGreaterThanOrEqual(2);
});
test('It should allow to do less than comparision', () => {
const a = BigInt(2);
jestExpect(a).toBeLessThan(3);
});
test('It should allow to do less than or equal comparision', () => {
const a = BigInt(2);
jestExpect(a).toBeLessThanOrEqual(2);
});
});
}
});
2 changes: 2 additions & 0 deletions packages/expect/src/jasmineUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ function eq(
return a == String(b);
case '[object Number]':
return Object.is(Number(a), Number(b));
case '[object BigInt]':
return Object.is(BigInt(a), BigInt(b));
JoshRosenstein marked this conversation as resolved.
Show resolved Hide resolved
case '[object Date]':
case '[object Boolean]':
// Coerce dates and booleans to numeric primitive values. Dates are compared by their
Expand Down
31 changes: 22 additions & 9 deletions packages/expect/src/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ensureExpectedIsNonNegativeInteger,
ensureNoExpected,
ensureNumbers,
ensureNumbersOrBigInt,
getLabelPrinter,
matcherErrorMessage,
matcherHint,
Expand Down Expand Up @@ -208,14 +209,18 @@ const matchers: MatchersObject = {
return {message, pass};
},

toBeGreaterThan(this: MatcherState, received: number, expected: number) {
toBeGreaterThan(
this: MatcherState,
received: number | bigint,
SimenB marked this conversation as resolved.
Show resolved Hide resolved
expected: number | bigint,
) {
const matcherName = 'toBeGreaterThan';
const isNot = this.isNot;
const options: MatcherHintOptions = {
isNot,
promise: this.promise,
};
ensureNumbers(received, expected, matcherName, options);
ensureNumbersOrBigInt(received, expected, matcherName, options);

const pass = received > expected;

Expand All @@ -230,16 +235,16 @@ const matchers: MatchersObject = {

toBeGreaterThanOrEqual(
this: MatcherState,
received: number,
expected: number,
received: number | bigint,
expected: number | bigint,
) {
const matcherName = 'toBeGreaterThanOrEqual';
const isNot = this.isNot;
const options: MatcherHintOptions = {
isNot,
promise: this.promise,
};
ensureNumbers(received, expected, matcherName, options);
ensureNumbersOrBigInt(received, expected, matcherName, options);

const pass = received >= expected;

Expand Down Expand Up @@ -302,14 +307,18 @@ const matchers: MatchersObject = {
return {message, pass};
},

toBeLessThan(this: MatcherState, received: number, expected: number) {
toBeLessThan(
this: MatcherState,
received: number | bigint,
expected: number | bigint,
) {
const matcherName = 'toBeLessThan';
const isNot = this.isNot;
const options: MatcherHintOptions = {
isNot,
promise: this.promise,
};
ensureNumbers(received, expected, matcherName, options);
ensureNumbersOrBigInt(received, expected, matcherName, options);

const pass = received < expected;

Expand All @@ -322,14 +331,18 @@ const matchers: MatchersObject = {
return {message, pass};
},

toBeLessThanOrEqual(this: MatcherState, received: number, expected: number) {
toBeLessThanOrEqual(
this: MatcherState,
received: number | bigint,
expected: number | bigint,
) {
const matcherName = 'toBeLessThanOrEqual';
const isNot = this.isNot;
const options: MatcherHintOptions = {
isNot,
promise: this.promise,
};
ensureNumbers(received, expected, matcherName, options);
ensureNumbersOrBigInt(received, expected, matcherName, options);

const pass = received <= expected;

Expand Down
4 changes: 4 additions & 0 deletions packages/jest-get-type/src/__tests__/getType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ describe('.getType()', () => {
test('map', () => expect(getType(new Map())).toBe('map'));
test('set', () => expect(getType(new Set())).toBe('set'));
test('date', () => expect(getType(new Date())).toBe('date'));
/* global BigInt */
if (typeof BigInt === 'function') {
test('bigint', () => expect(getType(BigInt(1))).toBe('bigint'));
}
});
Loading