From bbe10844ee1c6075d20077cfd74b586c77a15df7 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Tue, 29 May 2018 23:22:56 +0100 Subject: [PATCH 1/4] Add logic to deal with 1D array of args --- .../jest-each/src/__tests__/array.test.js | 20 ++++++++++++++++++- packages/jest-each/src/bind.js | 5 ++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/jest-each/src/__tests__/array.test.js b/packages/jest-each/src/__tests__/array.test.js index a4a04cd0e4d7..1158c60a2cf4 100644 --- a/packages/jest-each/src/__tests__/array.test.js +++ b/packages/jest-each/src/__tests__/array.test.js @@ -93,7 +93,25 @@ describe('jest-each', () => { ); }); - test('calls global with cb function containing all parameters of each test case', () => { + test('calls global with cb function containing all parameters of each test case when given 1d array', () => { + const globalTestMocks = getGlobalTestMocks(); + const testCallBack = jest.fn(); + const eachObject = each.withGlobal(globalTestMocks)(['hello', 'world']); + const testFunction = get(eachObject, keyPath); + testFunction('expected string', testCallBack); + + const globalMock = get(globalTestMocks, keyPath); + + globalMock.mock.calls[0][1](); + expect(testCallBack).toHaveBeenCalledTimes(1); + expect(testCallBack).toHaveBeenCalledWith('hello'); + + globalMock.mock.calls[1][1](); + expect(testCallBack).toHaveBeenCalledTimes(2); + expect(testCallBack).toHaveBeenCalledWith('world'); + }); + + test('calls global with cb function containing all parameters of each test case 2d array', () => { const globalTestMocks = getGlobalTestMocks(); const testCallBack = jest.fn(); const eachObject = each.withGlobal(globalTestMocks)([ diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index 0df9276d660d..bd414576a925 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -21,7 +21,10 @@ export default (cb: Function) => (...args: any) => ( test: Function, ): void => { if (args.length === 1) { - const table: Table = args[0]; + const table: Table = args[0].every(Array.isArray) + ? args[0] + : args[0].map(entry => [entry]); + return table.forEach(row => cb(util.format(title, ...row), applyRestParams(row, test)), ); From 0f428a7f50e6620c0181036e2d0b847045732968 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Tue, 29 May 2018 23:23:12 +0100 Subject: [PATCH 2/4] Add 1d e2e tests --- e2e/__tests__/__snapshots__/each.test.js.snap | 56 ++++++++++++++++++- e2e/each/__tests__/failure.test.js | 7 +++ e2e/each/__tests__/success.test.js | 7 +++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/e2e/__tests__/__snapshots__/each.test.js.snap b/e2e/__tests__/__snapshots__/each.test.js.snap index 6a074807acbe..e61d34c7bf6a 100644 --- a/e2e/__tests__/__snapshots__/each.test.js.snap +++ b/e2e/__tests__/__snapshots__/each.test.js.snap @@ -29,7 +29,7 @@ exports[`shows error message when not enough arguments are supplied to tests 1`] Missing 1 arguments - at packages/jest-each/build/bind.js:81:17 + at packages/jest-each/build/bind.js:84:17 " `; @@ -68,6 +68,9 @@ exports[`shows the correct errors in stderr when failing tests 1`] = ` ✓ template table fails on one row expected: true == true ✕ template table fails on all rows expected: 1 == 2 ✕ template table fails on all rows expected: 3 == 4 + ✕ The word red contains the letter 'z' + ✕ The word green contains the letter 'z' + ✕ The word bean contains the letter 'z' template table describe fails on all rows expected a == b ✕ fails template table describe fails on all rows expected c == d @@ -247,5 +250,56 @@ exports[`shows the correct errors in stderr when failing tests 1`] = ` at __tests__/failure.test.js:61:20 + ● The word red contains the letter 'z' + + expect(received).toBe(expected) // Object.is equality + + Expected: true + Received: false + + 67 | \\"The word %s contains the letter 'z'\\", + 68 | word => { + > 69 | expect(/z/.test(word)).toBe(true); + | ^ + 70 | } + 71 | ); + 72 | + + at __tests__/failure.test.js:69:28 + + ● The word green contains the letter 'z' + + expect(received).toBe(expected) // Object.is equality + + Expected: true + Received: false + + 67 | \\"The word %s contains the letter 'z'\\", + 68 | word => { + > 69 | expect(/z/.test(word)).toBe(true); + | ^ + 70 | } + 71 | ); + 72 | + + at __tests__/failure.test.js:69:28 + + ● The word bean contains the letter 'z' + + expect(received).toBe(expected) // Object.is equality + + Expected: true + Received: false + + 67 | \\"The word %s contains the letter 'z'\\", + 68 | word => { + > 69 | expect(/z/.test(word)).toBe(true); + | ^ + 70 | } + 71 | ); + 72 | + + at __tests__/failure.test.js:69:28 + " `; diff --git a/e2e/each/__tests__/failure.test.js b/e2e/each/__tests__/failure.test.js index 7e64752635d1..bd3ee8f76caf 100644 --- a/e2e/each/__tests__/failure.test.js +++ b/e2e/each/__tests__/failure.test.js @@ -62,3 +62,10 @@ describe.each([['a', 'b'], ['c', 'd']])( }); } ); + +test.each(['red', 'green', 'bean'])( + "The word %s contains the letter 'z'", + word => { + expect(/z/.test(word)).toBe(true); + } +); diff --git a/e2e/each/__tests__/success.test.js b/e2e/each/__tests__/success.test.js index 9964f3c74c08..afb1780fcf88 100644 --- a/e2e/each/__tests__/success.test.js +++ b/e2e/each/__tests__/success.test.js @@ -5,6 +5,13 @@ * LICENSE file in the root directory of this source tree. */ +test.each(['red', 'green', 'bean'])( + "The word %s contains the letter 'e'", + word => { + expect(/e/.test(word)).toBe(true); + } +); + it.each([[true, true], [true, true]])( 'passes one row expected %s == %s', (left, right) => { From 49c053f53fe40b8390808ae4fbc2618710126775 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Tue, 29 May 2018 23:27:09 +0100 Subject: [PATCH 3/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4ed1c2d8212..1e9cc9d9b750 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +* `[jest-each]` Support one dimensional array of data ([#6351](https://github.com/facebook/jest/pull/6351)) * `[jest-watch]` create new package `jest-watch` to ease custom watch plugin development ([#6318](https://github.com/facebook/jest/pull/6318)) ### Fixes From 88f6a661c36702f8481f1c7f8a8261d8258163ae Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Wed, 30 May 2018 08:04:08 +0100 Subject: [PATCH 4/4] Move test above describe to fix circus --- e2e/__tests__/__snapshots__/each.test.js.snap | 144 +++++++++--------- e2e/each/__tests__/failure.test.js | 14 +- 2 files changed, 79 insertions(+), 79 deletions(-) diff --git a/e2e/__tests__/__snapshots__/each.test.js.snap b/e2e/__tests__/__snapshots__/each.test.js.snap index e61d34c7bf6a..b40c5a703452 100644 --- a/e2e/__tests__/__snapshots__/each.test.js.snap +++ b/e2e/__tests__/__snapshots__/each.test.js.snap @@ -182,124 +182,124 @@ exports[`shows the correct errors in stderr when failing tests 1`] = ` at __tests__/failure.test.js:40:18 - ● template table describe fails on all rows expected a == b › fails + ● The word red contains the letter 'z' expect(received).toBe(expected) // Object.is equality - Expected: \\"b\\" - Received: \\"a\\" + Expected: true + Received: false - 50 | ({left, right}) => { - 51 | it('fails ', () => { - > 52 | expect(left).toBe(right); - | ^ - 53 | }); - 54 | } - 55 | ); + 45 | \\"The word %s contains the letter 'z'\\", + 46 | word => { + > 47 | expect(/z/.test(word)).toBe(true); + | ^ + 48 | } + 49 | ); + 50 | - at __tests__/failure.test.js:52:20 + at __tests__/failure.test.js:47:28 - ● template table describe fails on all rows expected c == d › fails + ● The word green contains the letter 'z' expect(received).toBe(expected) // Object.is equality - Expected: \\"d\\" - Received: \\"c\\" + Expected: true + Received: false - 50 | ({left, right}) => { - 51 | it('fails ', () => { - > 52 | expect(left).toBe(right); - | ^ - 53 | }); - 54 | } - 55 | ); + 45 | \\"The word %s contains the letter 'z'\\", + 46 | word => { + > 47 | expect(/z/.test(word)).toBe(true); + | ^ + 48 | } + 49 | ); + 50 | - at __tests__/failure.test.js:52:20 + at __tests__/failure.test.js:47:28 - ● array table describe fails on all rows expected a == b › fails + ● The word bean contains the letter 'z' expect(received).toBe(expected) // Object.is equality - Expected: \\"b\\" - Received: \\"a\\" + Expected: true + Received: false - 59 | (left, right) => { - 60 | it('fails', () => { - > 61 | expect(left).toBe(right); - | ^ - 62 | }); - 63 | } - 64 | ); + 45 | \\"The word %s contains the letter 'z'\\", + 46 | word => { + > 47 | expect(/z/.test(word)).toBe(true); + | ^ + 48 | } + 49 | ); + 50 | - at __tests__/failure.test.js:61:20 + at __tests__/failure.test.js:47:28 - ● array table describe fails on all rows expected c == d › fails + ● template table describe fails on all rows expected a == b › fails expect(received).toBe(expected) // Object.is equality - Expected: \\"d\\" - Received: \\"c\\" + Expected: \\"b\\" + Received: \\"a\\" - 59 | (left, right) => { - 60 | it('fails', () => { - > 61 | expect(left).toBe(right); + 57 | ({left, right}) => { + 58 | it('fails ', () => { + > 59 | expect(left).toBe(right); | ^ - 62 | }); - 63 | } - 64 | ); + 60 | }); + 61 | } + 62 | ); - at __tests__/failure.test.js:61:20 + at __tests__/failure.test.js:59:20 - ● The word red contains the letter 'z' + ● template table describe fails on all rows expected c == d › fails expect(received).toBe(expected) // Object.is equality - Expected: true - Received: false + Expected: \\"d\\" + Received: \\"c\\" - 67 | \\"The word %s contains the letter 'z'\\", - 68 | word => { - > 69 | expect(/z/.test(word)).toBe(true); - | ^ - 70 | } - 71 | ); - 72 | + 57 | ({left, right}) => { + 58 | it('fails ', () => { + > 59 | expect(left).toBe(right); + | ^ + 60 | }); + 61 | } + 62 | ); - at __tests__/failure.test.js:69:28 + at __tests__/failure.test.js:59:20 - ● The word green contains the letter 'z' + ● array table describe fails on all rows expected a == b › fails expect(received).toBe(expected) // Object.is equality - Expected: true - Received: false + Expected: \\"b\\" + Received: \\"a\\" - 67 | \\"The word %s contains the letter 'z'\\", - 68 | word => { - > 69 | expect(/z/.test(word)).toBe(true); - | ^ + 66 | (left, right) => { + 67 | it('fails', () => { + > 68 | expect(left).toBe(right); + | ^ + 69 | }); 70 | } 71 | ); - 72 | - at __tests__/failure.test.js:69:28 + at __tests__/failure.test.js:68:20 - ● The word bean contains the letter 'z' + ● array table describe fails on all rows expected c == d › fails expect(received).toBe(expected) // Object.is equality - Expected: true - Received: false + Expected: \\"d\\" + Received: \\"c\\" - 67 | \\"The word %s contains the letter 'z'\\", - 68 | word => { - > 69 | expect(/z/.test(word)).toBe(true); - | ^ + 66 | (left, right) => { + 67 | it('fails', () => { + > 68 | expect(left).toBe(right); + | ^ + 69 | }); 70 | } 71 | ); - 72 | - at __tests__/failure.test.js:69:28 + at __tests__/failure.test.js:68:20 " `; diff --git a/e2e/each/__tests__/failure.test.js b/e2e/each/__tests__/failure.test.js index bd3ee8f76caf..0ae31f13d164 100644 --- a/e2e/each/__tests__/failure.test.js +++ b/e2e/each/__tests__/failure.test.js @@ -41,6 +41,13 @@ it.each` } ); +test.each(['red', 'green', 'bean'])( + "The word %s contains the letter 'z'", + word => { + expect(/z/.test(word)).toBe(true); + } +); + describe.each` left | right ${'a'} | ${'b'} @@ -62,10 +69,3 @@ describe.each([['a', 'b'], ['c', 'd']])( }); } ); - -test.each(['red', 'green', 'bean'])( - "The word %s contains the letter 'z'", - word => { - expect(/z/.test(word)).toBe(true); - } -);