From a39810a42afbc6b9e8b35192da872aebb1087016 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Tue, 11 Oct 2022 11:28:39 +0800 Subject: [PATCH] fix --- .../__snapshots__/testFailing.test.ts.snap | 174 ++++++++---------- packages/jest-circus/src/index.ts | 18 +- packages/jest-each/src/bind.ts | 25 ++- 3 files changed, 107 insertions(+), 110 deletions(-) diff --git a/e2e/__tests__/__snapshots__/testFailing.test.ts.snap b/e2e/__tests__/__snapshots__/testFailing.test.ts.snap index acc16b6bcf13..899e759572c4 100644 --- a/e2e/__tests__/__snapshots__/testFailing.test.ts.snap +++ b/e2e/__tests__/__snapshots__/testFailing.test.ts.snap @@ -38,49 +38,43 @@ exports[`works with all statuses 1`] = ` Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error. + 28 | }); + 29 | + > 30 | test.failing.each([ + | ^ + 31 | {a: 1, b: 1, expected: 2}, 32 | {a: 1, b: 2, expected: 3}, 33 | {a: 2, b: 1, expected: 3}, - > 34 | ])('.add($a, $b)', ({a, b, expected}) => { - | ^ - 35 | expect(a + b).toBe(expected); - 36 | }); - 37 | - at ../../packages/jest-each/build/bind.js:38:11 - at Array.forEach () - at Object. (__tests__/statuses.test.js:34:3) + at Object.each (__tests__/statuses.test.js:30:14) ● .add(1, 2) Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error. + 28 | }); + 29 | + > 30 | test.failing.each([ + | ^ + 31 | {a: 1, b: 1, expected: 2}, 32 | {a: 1, b: 2, expected: 3}, 33 | {a: 2, b: 1, expected: 3}, - > 34 | ])('.add($a, $b)', ({a, b, expected}) => { - | ^ - 35 | expect(a + b).toBe(expected); - 36 | }); - 37 | - at ../../packages/jest-each/build/bind.js:38:11 - at Array.forEach () - at Object. (__tests__/statuses.test.js:34:3) + at Object.each (__tests__/statuses.test.js:30:14) ● .add(2, 1) Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error. + 28 | }); + 29 | + > 30 | test.failing.each([ + | ^ + 31 | {a: 1, b: 1, expected: 2}, 32 | {a: 1, b: 2, expected: 3}, 33 | {a: 2, b: 1, expected: 3}, - > 34 | ])('.add($a, $b)', ({a, b, expected}) => { - | ^ - 35 | expect(a + b).toBe(expected); - 36 | }); - 37 | - at ../../packages/jest-each/build/bind.js:38:11 - at Array.forEach () - at Object. (__tests__/statuses.test.js:34:3) + at Object.each (__tests__/statuses.test.js:30:14) ● failing passes = fails @@ -127,51 +121,45 @@ exports[`works with concurrent and only mode 1`] = ` Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error. + 15 | }); + 16 | + > 17 | test.concurrent.only.failing.each([ + | ^ + 18 | {a: 1, b: 1, expected: 2}, 19 | {a: 1, b: 2, expected: 3}, 20 | {a: 2, b: 1, expected: 3}, - > 21 | ])('.add($a, $b)', ({a, b, expected}) => { - | ^ - 22 | expect(a + b).toBe(expected); - 23 | }); - 24 | - at ../../packages/jest-each/build/bind.js:38:11 - at Array.forEach () - at __tests__/worksWithConcurrentOnlyMode.test.js:21:5 + at each (__tests__/worksWithConcurrentOnlyMode.test.js:17:32) at Object.describe (__tests__/worksWithConcurrentOnlyMode.test.js:8:1) ● block with concurrent › .add(1, 2) Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error. + 15 | }); + 16 | + > 17 | test.concurrent.only.failing.each([ + | ^ + 18 | {a: 1, b: 1, expected: 2}, 19 | {a: 1, b: 2, expected: 3}, 20 | {a: 2, b: 1, expected: 3}, - > 21 | ])('.add($a, $b)', ({a, b, expected}) => { - | ^ - 22 | expect(a + b).toBe(expected); - 23 | }); - 24 | - at ../../packages/jest-each/build/bind.js:38:11 - at Array.forEach () - at __tests__/worksWithConcurrentOnlyMode.test.js:21:5 + at each (__tests__/worksWithConcurrentOnlyMode.test.js:17:32) at Object.describe (__tests__/worksWithConcurrentOnlyMode.test.js:8:1) ● block with concurrent › .add(2, 1) Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error. + 15 | }); + 16 | + > 17 | test.concurrent.only.failing.each([ + | ^ + 18 | {a: 1, b: 1, expected: 2}, 19 | {a: 1, b: 2, expected: 3}, 20 | {a: 2, b: 1, expected: 3}, - > 21 | ])('.add($a, $b)', ({a, b, expected}) => { - | ^ - 22 | expect(a + b).toBe(expected); - 23 | }); - 24 | - at ../../packages/jest-each/build/bind.js:38:11 - at Array.forEach () - at __tests__/worksWithConcurrentOnlyMode.test.js:21:5 + at each (__tests__/worksWithConcurrentOnlyMode.test.js:17:32) at Object.describe (__tests__/worksWithConcurrentOnlyMode.test.js:8:1)" `; @@ -222,51 +210,45 @@ exports[`works with concurrent mode 1`] = ` Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error. + 15 | }); + 16 | + > 17 | test.concurrent.failing.each([ + | ^ + 18 | {a: 1, b: 1, expected: 2}, 19 | {a: 1, b: 2, expected: 3}, 20 | {a: 2, b: 1, expected: 3}, - > 21 | ])('.add($a, $b)', ({a, b, expected}) => { - | ^ - 22 | expect(a + b).toBe(expected); - 23 | }); - 24 | - at ../../packages/jest-each/build/bind.js:38:11 - at Array.forEach () - at __tests__/worksWithConcurrentMode.test.js:21:5 + at each (__tests__/worksWithConcurrentMode.test.js:17:27) at Object.describe (__tests__/worksWithConcurrentMode.test.js:8:1) ● block with concurrent › .add(1, 2) Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error. + 15 | }); + 16 | + > 17 | test.concurrent.failing.each([ + | ^ + 18 | {a: 1, b: 1, expected: 2}, 19 | {a: 1, b: 2, expected: 3}, 20 | {a: 2, b: 1, expected: 3}, - > 21 | ])('.add($a, $b)', ({a, b, expected}) => { - | ^ - 22 | expect(a + b).toBe(expected); - 23 | }); - 24 | - at ../../packages/jest-each/build/bind.js:38:11 - at Array.forEach () - at __tests__/worksWithConcurrentMode.test.js:21:5 + at each (__tests__/worksWithConcurrentMode.test.js:17:27) at Object.describe (__tests__/worksWithConcurrentMode.test.js:8:1) ● block with concurrent › .add(2, 1) Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error. + 15 | }); + 16 | + > 17 | test.concurrent.failing.each([ + | ^ + 18 | {a: 1, b: 1, expected: 2}, 19 | {a: 1, b: 2, expected: 3}, 20 | {a: 2, b: 1, expected: 3}, - > 21 | ])('.add($a, $b)', ({a, b, expected}) => { - | ^ - 22 | expect(a + b).toBe(expected); - 23 | }); - 24 | - at ../../packages/jest-each/build/bind.js:38:11 - at Array.forEach () - at __tests__/worksWithConcurrentMode.test.js:21:5 + at each (__tests__/worksWithConcurrentMode.test.js:17:27) at Object.describe (__tests__/worksWithConcurrentMode.test.js:8:1)" `; @@ -297,51 +279,45 @@ exports[`works with only mode 1`] = ` Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error. + 13 | }); + 14 | + > 15 | it.only.failing.each([ + | ^ + 16 | {a: 1, b: 1, expected: 2}, 17 | {a: 1, b: 2, expected: 3}, 18 | {a: 2, b: 1, expected: 3}, - > 19 | ])('.add($a, $b)', ({a, b, expected}) => { - | ^ - 20 | expect(a + b).toBe(expected); - 21 | }); - 22 | - - at ../../packages/jest-each/build/bind.js:38:11 - at Array.forEach () - at __tests__/worksWithOnlyMode.test.js:19:5 + + at each (__tests__/worksWithOnlyMode.test.js:15:19) at Object.describe (__tests__/worksWithOnlyMode.test.js:10:1) ● block with only, should pass › .add(1, 2) Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error. + 13 | }); + 14 | + > 15 | it.only.failing.each([ + | ^ + 16 | {a: 1, b: 1, expected: 2}, 17 | {a: 1, b: 2, expected: 3}, 18 | {a: 2, b: 1, expected: 3}, - > 19 | ])('.add($a, $b)', ({a, b, expected}) => { - | ^ - 20 | expect(a + b).toBe(expected); - 21 | }); - 22 | - - at ../../packages/jest-each/build/bind.js:38:11 - at Array.forEach () - at __tests__/worksWithOnlyMode.test.js:19:5 + + at each (__tests__/worksWithOnlyMode.test.js:15:19) at Object.describe (__tests__/worksWithOnlyMode.test.js:10:1) ● block with only, should pass › .add(2, 1) Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error. + 13 | }); + 14 | + > 15 | it.only.failing.each([ + | ^ + 16 | {a: 1, b: 1, expected: 2}, 17 | {a: 1, b: 2, expected: 3}, 18 | {a: 2, b: 1, expected: 3}, - > 19 | ])('.add($a, $b)', ({a, b, expected}) => { - | ^ - 20 | expect(a + b).toBe(expected); - 21 | }); - 22 | - - at ../../packages/jest-each/build/bind.js:38:11 - at Array.forEach () - at __tests__/worksWithOnlyMode.test.js:19:5 + + at each (__tests__/worksWithOnlyMode.test.js:15:19) at Object.describe (__tests__/worksWithOnlyMode.test.js:10:1) ● block with only, should fail › failing passes = fails, should fail diff --git a/packages/jest-circus/src/index.ts b/packages/jest-circus/src/index.ts index cb885c6d6a5e..43e039ceac7f 100644 --- a/packages/jest-circus/src/index.ts +++ b/packages/jest-circus/src/index.ts @@ -147,8 +147,19 @@ const test: Global.It = (() => { testName: Circus.TestNameLike, fn?: Circus.TestFn, timeout?: number, - ): void => _addTest(testName, mode, concurrent, fn, failing, timeout, true); - failing.each = bindEach(failing, false); + eachError?: Error, + ): void => + _addTest( + testName, + mode, + concurrent, + fn, + failing, + timeout, + true, + eachError, + ); + failing.each = bindEach(failing, false, true); return failing; }; @@ -175,8 +186,9 @@ const test: Global.It = (() => { ) => void, timeout?: number, failing?: boolean, + error?: Error, ) => { - const asyncError = new ErrorWithStack(undefined, testFn); + const asyncError = error || new ErrorWithStack(undefined, testFn); try { testName = convertDescriptorToString(testName); diff --git a/packages/jest-each/src/bind.ts b/packages/jest-each/src/bind.ts index 66839d9b1198..cdeb79042eb2 100644 --- a/packages/jest-each/src/bind.ts +++ b/packages/jest-each/src/bind.ts @@ -26,17 +26,19 @@ type GlobalCallback = ( testName: string, fn: Global.ConcurrentTestFn, timeout?: number, + eachError?: Error, ) => void; export default function bind( cb: GlobalCallback, supportsDone = true, -) { + needsEachError = false, +): Global.EachTestFn { const bindWrap = ( table: Global.EachTable, ...taggedTemplateData: Global.TemplateData ) => { - const error = new ErrorWithStack('', bindWrap); + const error = new ErrorWithStack(undefined, bindWrap); return function eachBind( title: Global.BlockNameLike, @@ -50,16 +52,23 @@ export default function bind( : buildTemplateTests(title, table, taggedTemplateData); return tests.forEach(row => - cb( - row.title, - applyArguments(supportsDone, row.arguments, test), - timeout, - ), + needsEachError + ? cb( + row.title, + applyArguments(supportsDone, row.arguments, test), + timeout, + error, + ) + : cb( + row.title, + applyArguments(supportsDone, row.arguments, test), + timeout, + ), ); } catch (e: any) { const err = new Error(e.message); err.stack = error.stack?.replace(/^Error: /s, e.message); - err.name = e.name; + err.name = ''; return cb(title, () => { throw err;