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 33 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
- `[expect]` Improve report when matcher fails, part 17 ([#8349](https://github.com/facebook/jest/pull/8349))
- `[expect]` Improve report when matcher fails, part 18 ([#8356](https://github.com/facebook/jest/pull/8356))
- `[expect]` Improve report when matcher fails, part 19 ([#8367](https://github.com/facebook/jest/pull/8367))
- `[expect]` Add BigInt Support to `toBeGreaterThan`, `toBeGreaterThanOrEqual`, `toBeLessThan` and `toBeLessThanOrEqual` ([#8382](https://github.com/facebook/jest/pull/8382))
- `[jest-get-type]` Add BigInt Support. ([#8382](https://github.com/facebook/jest/pull/8382))
- `[jest-matcher-utils]`- Add BigInt Support to `ensureNumbers` `ensureActualIsNumber`, `ensureExpectedIsNumber` ([#8382](https://github.com/facebook/jest/pull/8382))
- `[babel-plugin-jest-hoist]` Add BigInt to `WHITELISTED_IDENTIFIERS`([#8382](https://github.com/facebook/jest/pull/8382))
- `[babel-preset-jest]` Add @babel/plugin-syntax-bigint`([#8382](https://github.com/facebook/jest/pull/8382))

### Fixes

Expand All @@ -31,6 +36,7 @@
- `[@jest/reporters]` Migrate away from `istanbul-api` ([#8294](https://github.com/facebook/jest/pull/8294))
- `[*]` Delete obsolete emails tag from header comment in test files ([#8377](https://github.com/facebook/jest/pull/8377))
- `[expect]` optimize compare nodes ([#8368](https://github.com/facebook/jest/pull/8368))
- `[eslintrc]` Disable valid-typeof, no-undef for ts overrides ([#8382](https://github.com/facebook/jest/pull/8382))

### Performance

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
1 change: 1 addition & 0 deletions packages/babel-preset-jest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ module.exports = () => ({
plugins: [
require.resolve('babel-plugin-jest-hoist'),
require.resolve('@babel/plugin-syntax-object-rest-spread'),
require.resolve('@babel/plugin-syntax-bigint'),
],
});
1 change: 1 addition & 0 deletions packages/babel-preset-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"license": "MIT",
"main": "index.js",
"dependencies": {
"@babel/plugin-syntax-bigint": "^7.0.0",
JoshRosenstein marked this conversation as resolved.
Show resolved Hide resolved
"@babel/plugin-syntax-object-rest-spread": "^7.0.0",
"babel-plugin-jest-hoist": "^24.6.0"
},
Expand Down

Large diffs are not rendered by default.

243 changes: 243 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ afterAll(() => {
chalk.enabled = chalkEnabled;
});

/* global BigInt */
const isBigIntDefined = typeof BigInt === 'function';

it('should throw if passed two arguments', () => {
expect(() => jestExpect('foo', 'bar')).toThrow(
new Error('Expect takes at most one argument.'),
Expand Down Expand Up @@ -198,6 +201,11 @@ describe('.toBe()', () => {
jestExpect(null).toBe(null);
jestExpect(undefined).toBe(undefined);
jestExpect(NaN).toBe(NaN);
if (isBigIntDefined) {
jestExpect(BigInt(1)).not.toBe(BigInt(2));
jestExpect(BigInt(1)).not.toBe(1);
jestExpect(BigInt(1)).toBe(BigInt(1));
}
});

[
Expand Down Expand Up @@ -226,12 +234,30 @@ describe('.toBe()', () => {
});
});

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

[false, 1, 'a', undefined, null, {}, []].forEach(v => {
it(`fails for '${stringify(v)}' with '.not'`, () => {
expect(() => jestExpect(v).not.toBe(v)).toThrowErrorMatchingSnapshot();
});
});

if (isBigIntDefined) {
[BigInt(1), BigInt('1')].forEach(v => {
it(`fails for '${stringify(v)}' with '.not'`, () => {
expect(() => jestExpect(v).not.toBe(v)).toThrowError('toBe');
});
});
}

it('does not crash on circular references', () => {
const obj = {};
obj.circular = obj;
Expand Down Expand Up @@ -446,6 +472,17 @@ describe('.toEqual()', () => {
});
});

if (isBigIntDefined) {
[[BigInt(1), BigInt(2)], [BigInt(1), 1]].forEach(([a, b]) => {
test(`{pass: false} expect(${stringify(a)}).toEqual(${stringify(
b,
)})`, () => {
expect(() => jestExpect(a).toEqual(b)).toThrowError('toEqual');
jestExpect(a).not.toEqual(b);
});
});
}

[
[true, true],
[1, 1],
Expand Down Expand Up @@ -577,6 +614,25 @@ describe('.toEqual()', () => {
});
});

if (isBigIntDefined) {
[
[BigInt(1), BigInt(1)],
[BigInt(0), BigInt('0')],
[[BigInt(1)], [BigInt(1)]],
[[BigInt(1), 2], [BigInt(1), 2]],
[Immutable.List([BigInt(1)]), Immutable.List([BigInt(1)])],
[{a: BigInt(99)}, {a: BigInt(99)}],
[new Set([BigInt(1), BigInt(2)]), new Set([BigInt(1), BigInt(2)])],
].forEach(([a, b]) => {
test(`{pass: true} expect(${stringify(a)}).not.toEqual(${stringify(
b,
)})`, () => {
jestExpect(a).toEqual(b);
expect(() => jestExpect(a).not.toEqual(b)).toThrowError('toEqual');
});
});
}

test('assertion error matcherResult property contains matcher name, expected and actual values', () => {
const actual = {a: 1};
const expected = {a: 2};
Expand Down Expand Up @@ -769,6 +825,19 @@ describe('.toBeTruthy(), .toBeFalsy()', () => {
});
});

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

expect(() => jestExpect(v).not.toBeTruthy()).toThrowError('toBeTruthy');

expect(() => jestExpect(v).toBeFalsy()).toThrowError('toBeFalsy');
});
});
}

[false, null, NaN, 0, '', undefined].forEach(v => {
test(`'${stringify(v)}' is falsy`, () => {
jestExpect(v).toBeFalsy();
Expand All @@ -781,6 +850,19 @@ describe('.toBeTruthy(), .toBeFalsy()', () => {
).toThrowErrorMatchingSnapshot();
});
});

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

expect(() => jestExpect(v).toBeTruthy()).toThrowError('toBeTruthy');

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

describe('.toBeNaN()', () => {
Expand Down Expand Up @@ -837,6 +919,23 @@ describe('.toBeDefined(), .toBeUndefined()', () => {
});
});

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

expect(() => jestExpect(v).not.toBeDefined()).toThrowError(
'toBeDefined',
);

expect(() => jestExpect(v).toBeUndefined()).toThrowError(
'toBeUndefined',
);
});
});
}

test('undefined is undefined', () => {
jestExpect(undefined).toBeUndefined();
jestExpect(undefined).not.toBeDefined();
Expand Down Expand Up @@ -931,6 +1030,105 @@ describe(
});
});

if (isBigIntDefined) {
test('can compare BigInt to Numbers', () => {
const a = BigInt(2);
jestExpect(a).toBeGreaterThan(1);
jestExpect(a).toBeGreaterThanOrEqual(2);
jestExpect(2).toBeLessThanOrEqual(a);
jestExpect(a).toBeLessThan(3);
jestExpect(a).toBeLessThanOrEqual(2);
});

[
[BigInt(1), BigInt(2)],
[BigInt(0x11), BigInt(0x22)],
[-1, BigInt(2)],
].forEach(([small, big]) => {
it(`{pass: true} expect(${stringify(small)}).toBeLessThan(${stringify(
big,
)})`, () => {
jestExpect(small).toBeLessThan(big);
});

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

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

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

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

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

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

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

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

expect(() => jestExpect(small).not.toBeLessThan(big)).toThrowError(
'toBeLessThan',
);

expect(() => jestExpect(big).not.toBeGreaterThan(small)).toThrowError(
'toBeGreaterThan',
);

expect(() => jestExpect(big).toBeLessThan(small)).toThrowError(
'toBeLessThan',
);

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

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

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

expect(() => jestExpect(big).toBeLessThanOrEqual(small)).toThrowError(
'toBeLessThanOrEqual',
);
});
});
}

[
[1, 1],
[Number.MIN_VALUE, Number.MIN_VALUE],
Expand All @@ -951,6 +1149,26 @@ describe(
).toThrowErrorMatchingSnapshot();
});
});

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

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

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

Expand Down Expand Up @@ -1000,6 +1218,21 @@ describe('.toContain(), .toContainEqual()', () => {
});
});

if (isBigIntDefined) {
[
[[BigInt(1), BigInt(2), BigInt(3), BigInt(4)], BigInt(1)],
[[1, 2, 3, BigInt(3), 4], BigInt(3)],
].forEach(([list, v]) => {
it(`'${stringify(list)}' contains '${stringify(v)}'`, () => {
jestExpect(list).toContain(v);

expect(() => jestExpect(list).not.toContain(v)).toThrowError(
'toContain',
);
});
});
}

[
[[1, 2, 3], 4],
[[null, undefined], 1],
Expand All @@ -1015,6 +1248,16 @@ describe('.toContain(), .toContainEqual()', () => {
});
});

if (isBigIntDefined) {
[[[BigInt(1), BigInt(2), BigInt(3)], 3]].forEach(([list, v]) => {
it(`'${stringify(list)}' does not contain '${stringify(v)}'`, () => {
jestExpect(list).not.toContain(v);

expect(() => jestExpect(list).toContain(v)).toThrowError('toContain');
});
});
}

test('error cases', () => {
expect(() => jestExpect(null).toContain(1)).toThrowErrorMatchingSnapshot();
});
Expand Down
Loading