diff --git a/CHANGELOG.md b/CHANGELOG.md index f188240e5201..97830c16c2dd 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 diff --git a/e2e/__tests__/__snapshots__/each.test.js.snap b/e2e/__tests__/__snapshots__/each.test.js.snap index ad2dbde73203..5d3a38d5d10c 100644 --- a/e2e/__tests__/__snapshots__/each.test.js.snap +++ b/e2e/__tests__/__snapshots__/each.test.js.snap @@ -76,6 +76,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 @@ -187,6 +190,57 @@ exports[`shows the correct errors in stderr when failing tests 1`] = ` at __tests__/failure.test.js:40:18 + ● The word red contains the letter 'z' + + expect(received).toBe(expected) // Object.is equality + + Expected: true + Received: false + + 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:47:28 + + ● The word green contains the letter 'z' + + expect(received).toBe(expected) // Object.is equality + + Expected: true + Received: false + + 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:47:28 + + ● The word bean contains the letter 'z' + + expect(received).toBe(expected) // Object.is equality + + Expected: true + Received: false + + 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:47:28 + ● template table describe fails on all rows expected a == b › fails expect(received).toBe(expected) // Object.is equality @@ -194,15 +248,15 @@ exports[`shows the correct errors in stderr when failing tests 1`] = ` Expected: \\"b\\" Received: \\"a\\" - 50 | ({left, right}) => { - 51 | it('fails ', () => { - > 52 | expect(left).toBe(right); + 57 | ({left, right}) => { + 58 | it('fails ', () => { + > 59 | expect(left).toBe(right); | ^ - 53 | }); - 54 | } - 55 | ); + 60 | }); + 61 | } + 62 | ); - at __tests__/failure.test.js:52:20 + at __tests__/failure.test.js:59:20 ● template table describe fails on all rows expected c == d › fails @@ -211,15 +265,15 @@ exports[`shows the correct errors in stderr when failing tests 1`] = ` Expected: \\"d\\" Received: \\"c\\" - 50 | ({left, right}) => { - 51 | it('fails ', () => { - > 52 | expect(left).toBe(right); + 57 | ({left, right}) => { + 58 | it('fails ', () => { + > 59 | expect(left).toBe(right); | ^ - 53 | }); - 54 | } - 55 | ); + 60 | }); + 61 | } + 62 | ); - at __tests__/failure.test.js:52:20 + at __tests__/failure.test.js:59:20 ● array table describe fails on all rows expected a == b › fails @@ -228,15 +282,15 @@ exports[`shows the correct errors in stderr when failing tests 1`] = ` Expected: \\"b\\" Received: \\"a\\" - 59 | (left, right) => { - 60 | it('fails', () => { - > 61 | expect(left).toBe(right); + 66 | (left, right) => { + 67 | it('fails', () => { + > 68 | expect(left).toBe(right); | ^ - 62 | }); - 63 | } - 64 | ); + 69 | }); + 70 | } + 71 | ); - at __tests__/failure.test.js:61:20 + at __tests__/failure.test.js:68:20 ● array table describe fails on all rows expected c == d › fails @@ -245,15 +299,15 @@ exports[`shows the correct errors in stderr when failing tests 1`] = ` Expected: \\"d\\" Received: \\"c\\" - 59 | (left, right) => { - 60 | it('fails', () => { - > 61 | expect(left).toBe(right); + 66 | (left, right) => { + 67 | it('fails', () => { + > 68 | expect(left).toBe(right); | ^ - 62 | }); - 63 | } - 64 | ); + 69 | }); + 70 | } + 71 | ); - at __tests__/failure.test.js:61:20 + at __tests__/failure.test.js:68:20 " `; diff --git a/e2e/each/__tests__/failure.test.js b/e2e/each/__tests__/failure.test.js index 7e64752635d1..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'} 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) => { 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 98e7c26c7c77..b9f3614c67be 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -19,7 +19,9 @@ const RECEIVED_COLOR = chalk.red; export default (cb: Function) => (...args: any) => function eachBind(title: string, 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)), );