Trying to use Jest 23 "test.each" error results in a TypeError.
Test
test.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])(
'.add(%i, %i)',
(a, b, expected) => {
expect(a + b).toBe(expected);
},
);
Error Message
FAIL src/app/wte/components/my-component.component.spec.ts (204 MB heap size)
MyComponent
✓ should create (743ms)
✕ encountered a declaration exception (625ms)
● MyComponent › encountered a declaration exception
TypeError: test.each is not a function
27 | });
28 |
> 29 | test.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])(
| ^
30 | '.add(%i, %i)',
31 | (a, b, expected) => {
32 | expect(a + b).toBe(expected);
at src/app/wte/components/my-component.component.spec.ts:29:8
at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:388:26)
at Zone.Object.<anonymous>.Zone.run (node_modules/zone.js/dist/zone.js:138:43)
at Suite.<anonymous> (node_modules/jest-zone-patch/index.js:39:25)
at env.(anonymous function) (node_modules/jest-zone-patch/index.js:57:27)
at Object.<anonymous> (src/app/wte/components/my-component.component.spec.ts:8:1)
Any suggestions what am I missing ?
regards
Looks like you're not the only one :D https://github.com/facebook/jest/issues/6612
We're not yet officially compatible with Jest 23, because we're waiting for ts-jest to update.
I see however that more folks try to use it and it mostly works. What needs to be updated on our side is jest-zone-patch, as it doesn't account for new additions to test() fn. Happy to accept PR with a fix :)
ok, seems to belong to https://github.com/facebook/jest/issues/6612
Installing jest-each does not resolve the problem either
@thymikee
could it be so simple ? Or is there something I do not think about ?
Will write some tests for my code and will create a PR this evening or tomorrow
['xit', 'fit', 'test', 'it'].forEach(methodName => {
const originaljestFn = env[methodName];
env[methodName] = function(description, specDefinitions, timeout) {
arguments[1] = wrapTestInZone(specDefinitions);
return originaljestFn.apply(this, arguments);
};
if (methodName === 'test' || methodName === 'it') {
env[methodName].only = env['fit'];
env[methodName].skip = env['xit'];
// modification is below this point
// test.each(table)(name, fn)
env[methodName].each = function (description, specDefinitions) {
arguments[1] = wrapTestInZone(specDefinitions);
return originaljestFn.each.apply(this, arguments);
};
// end of modification
}
});
Does successfully handle both testcases (ok and failure)
it.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])('.add(%i, %i)', (a, b, expected) => {
expect(a + b).toBe(expected);
});
it.each`
init | incr | expected
${11} | ${1} | ${12}
`('can handle +1 steps', ({init, incr, expected}) => {
component.initialSeatNumber = init;
component.onPlatznummerChangeBy(incr);
expect(MatrixManagerService.NEXT_SEAT_NUMBER).toEqual(expected);
});
test.each has different function signature: https://github.com/facebook/jest/blob/master/packages/jest-each/src/bind.js
Not sure if it matters for zone to work though.
@thymikee there is a PR
https://github.com/thymikee/jest-zone-patch/pull/7
But there seems to be a bug when having multiple entries in the table (using just the "array" methodology the test is working), maybe you can help
test.each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`('returns $expected when $a is added $b', ({a, b, expected}) => {
expect(a + b).toBe(expected);
});
results in the following
FAIL src/app/wte/components/MyComponent.component.spec.ts (389 MB heap size)
MyComponent
✓ should create (1228ms)
✕ returns 2 when [Function anonymous] is added 1 (732ms)
✓ returns 3 when 1 is added 2 (655ms)
✓ returns 3 when 2 is added 1 (637ms)
● MyComponent › returns 2 when [Function anonymous] is added 1
expect(received).toBe(expected) // Object.is equality
Expected: 2
Received: "done => testProxyZone.run(testBody, null, [done])1"
Difference:
Comparing two different types of values. Expected number but received string.
38 | ${2} | ${1} | ${3}
39 | `('returns $expected when $a is added $b', ({a, b, expected}) => {
> 40 | expect(a + b).toBe(expected);
| ^
41 | });
42 |
43 | /*it.only.each`
at src/app/wte/components/MyComponent.component.spec.ts:40:21
Test Suites: 1 failed, 1 total
Tests: 1 failed, 3 passed, 4 total
Snapshots: 0 total
Time: 4.168s
Ran all test suites matching /wte-naechs/i.
Watch Usage: Press w to show more.
This one works
test.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])(
'.add(%i, %i)',
(a, b, expected) => {
expect(a + b).toBe(expected);
},
);
Waiting for a fix too...
Just merged and released https://github.com/thymikee/jest-zone-patch/pull/8 so re-installing the preset should bump the jest-zone-patch and test.each should work now. Lemme know if you have any issues.
Turned out I need to publish another release. Fixed in v6.0.2
Will describe.each also be supported?
Should be supported, doesn't it work?
it is supported by this line https://github.com/thymikee/jest-zone-patch/blob/3c8aa04279c9c9de1edc75b4e0665c7a282ce3ba/index.js#L78 I guess ?
it.each works but example of describe.each from jest documentation:
describe.each([[1, 1, 2], [1, 2, 3], [2, 1, 3]])(
'.add(%i, %i)',
(a, b, expected) => {
test(`returns ${expected}`, () => {
expect(a + b).toBe(expected);
});
test(`returned value not be greater than ${expected}`, () => {
expect(a + b).not.toBeGreaterThan(expected);
});
test(`returned value not be less than ${expected}`, () => {
expect(a + b).not.toBeLessThan(expected);
});
},
);
Fails:
```.add(2, 1) › returned value not be less than undefined
expect(received)[.not].toBeLessThan(expected)
Received value must be a number.
Received:
string: "function (a, b, expected) {
test(\"returns \" + expected, function () {
expect(a + b).toBe(expected);
});
test(\"returned value not be greater than \" + expected, function () {
expect(a + b).not.toBeGreaterThan(expected);
});
test(\"returned value not be less than \" + expected, function () {
expect(a + b).not.toBeLessThan(expected);
});
}undefined"
45 |
46 | test(`returned value not be less than ${expected}`, () => {
> 47 | expect(a + b).not.toBeLessThan(expected);
| ^
48 | });
49 | },
50 | );
```
Looks like function is passed to expected argument instead of test data.
cc @JLHwung
@NagRock Should be good now, you'll need to re-install to update the transitive dependency of jest-zone-patch
Thanks. I will check it and get back with feedback.
@thymikee Works like a charm 🎉 Thanks!
Most helpful comment
Waiting for a fix too...