From a24e89673bad011cfc4a780b612d7f77212855f3 Mon Sep 17 00:00:00 2001 From: Kyle Hensel Date: Mon, 2 Nov 2020 11:03:10 +1300 Subject: [PATCH 1/3] Support index in it.each with template strings --- docs/GlobalAPI.md | 2 +- packages/jest-each/README.md | 1 + .../jest-each/src/__tests__/template.test.ts | 24 ++++++++++++------- packages/jest-each/src/table/template.ts | 9 +++---- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/docs/GlobalAPI.md b/docs/GlobalAPI.md index 220985c9053e..2472688b8264 100644 --- a/docs/GlobalAPI.md +++ b/docs/GlobalAPI.md @@ -273,7 +273,7 @@ describe.each([ - `table`: `Tagged Template Literal` - First row of variable name column headings separated with `|` - One or more subsequent rows of data supplied as template literal expressions using `${value}` syntax. -- `name`: `String` the title of the test suite, use `$variable` to inject test data into the suite title from the tagged template expressions. +- `name`: `String` the title of the test suite, use `$variable` to inject test data into the suite title from the tagged template expressions, and `$#` for the index of the row. - To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` - `fn`: `Function` the suite of tests to be ran, this is the function that will receive the test data object. - Optionally, you can provide a `timeout` (in milliseconds) for specifying how long to wait for each row before aborting. _Note: The default timeout is 5 seconds._ diff --git a/packages/jest-each/README.md b/packages/jest-each/README.md index 16c845c7e41a..c06a6ad2b589 100644 --- a/packages/jest-each/README.md +++ b/packages/jest-each/README.md @@ -338,6 +338,7 @@ each` - name: `String` the title of the `test`, use `$variable` in the name string to inject test values into the test title from the tagged template expressions - To inject nested object values use you can supply a keyPath i.e. `$variable.path.to.value` + - You can use `$#` to inject the index of the table row. - testFn: `Function` the test logic, this is the function that will receive the parameters of each row as function arguments #### `each[tagged template].describe(name, suiteFn)` diff --git a/packages/jest-each/src/__tests__/template.test.ts b/packages/jest-each/src/__tests__/template.test.ts index fe5d67217d0b..fb7687249d06 100644 --- a/packages/jest-each/src/__tests__/template.test.ts +++ b/packages/jest-each/src/__tests__/template.test.ts @@ -171,17 +171,20 @@ describe('jest-each', () => { ${1} | ${1} | ${2} `; const testFunction = get(eachObject, keyPath); - testFunction('expected string: a=$a, b=$b, expected=$expected', noop); + testFunction( + 'expected string: a=$a, b=$b, expected=$expected index=$#', + noop, + ); const globalMock = get(globalTestMocks, keyPath); expect(globalMock).toHaveBeenCalledTimes(2); expect(globalMock).toHaveBeenCalledWith( - 'expected string: a=0, b=1, expected=1', + 'expected string: a=0, b=1, expected=1 index=0', expectFunction, undefined, ); expect(globalMock).toHaveBeenCalledWith( - 'expected string: a=1, b=1, expected=2', + 'expected string: a=1, b=1, expected=2 index=1', expectFunction, undefined, ); @@ -196,19 +199,19 @@ describe('jest-each', () => { `; const testFunction = get(eachObject, keyPath); testFunction( - 'add($a, $b) expected string: a=$a, b=$b, expected=$expected', + 'add($a, $b) expected string: a=$a, b=$b, expected=$expected index=$#', noop, ); const globalMock = get(globalTestMocks, keyPath); expect(globalMock).toHaveBeenCalledTimes(2); expect(globalMock).toHaveBeenCalledWith( - 'add(0, 1) expected string: a=0, b=1, expected=1', + 'add(0, 1) expected string: a=0, b=1, expected=1 index=0', expectFunction, undefined, ); expect(globalMock).toHaveBeenCalledWith( - 'add(1, 1) expected string: a=1, b=1, expected=2', + 'add(1, 1) expected string: a=1, b=1, expected=2 index=1', expectFunction, undefined, ); @@ -426,17 +429,20 @@ describe('jest-each', () => { ${1} | ${1} | ${2} `; const testFunction = get(eachObject, keyPath); - testFunction('expected string: a=$a, b=$b, expected=$expected', noop); + testFunction( + 'expected string: a=$a, b=$b, expected=$expected index=$#', + noop, + ); const globalMock = get(globalTestMocks, keyPath); expect(globalMock).toHaveBeenCalledTimes(2); expect(globalMock).toHaveBeenCalledWith( - 'expected string: a=0, b=1, expected=1', + 'expected string: a=0, b=1, expected=1 index=0', expectFunction, undefined, ); expect(globalMock).toHaveBeenCalledWith( - 'expected string: a=1, b=1, expected=2', + 'expected string: a=1, b=1, expected=2 index=1', expectFunction, undefined, ); diff --git a/packages/jest-each/src/table/template.ts b/packages/jest-each/src/table/template.ts index 995a1fbe0738..684188a3eaf5 100644 --- a/packages/jest-each/src/table/template.ts +++ b/packages/jest-each/src/table/template.ts @@ -22,9 +22,9 @@ export default ( ): EachTests => { const table = convertRowToTable(row, headings); const templates = convertTableToTemplates(table, headings); - return templates.map(template => ({ + return templates.map((template, index) => ({ arguments: [template], - title: interpolate(title, template), + title: interpolate(title, template, index), })); }; @@ -47,10 +47,11 @@ const convertTableToTemplates = ( ), ); -const interpolate = (title: string, template: Template) => +const interpolate = (title: string, template: Template, index: number) => Object.keys(template) .reduce(getMatchingKeyPaths(title), []) // aka flatMap - .reduce(replaceKeyPathWithValue(template), title); + .reduce(replaceKeyPathWithValue(template), title) + .replace('$#', '' + index); const getMatchingKeyPaths = (title: string) => ( matches: Headings, From 9ec83e6355af9c638daf82d6244f7afc71e6c11b Mon Sep 17 00:00:00 2001 From: Kyle Hensel Date: Mon, 2 Nov 2020 11:12:11 +1300 Subject: [PATCH 2/3] update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0017592475c4..2de17bb2af70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - `[jest-core]` Add `findRelatedTests` and `nonFlagArgs` in allowed config options for `updateConfigAndRun` in watch plugins ([#10659](https://github.com/facebook/jest/pull/10659)) +- `[jest-each]` Support array index with template strings ([#10763](https://github.com/facebook/jest/pull/10763)) ### Fixes From 31eca03b2b339580acbd6d67ea4084cc843b0c50 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 22 Apr 2021 08:44:07 +0200 Subject: [PATCH 3/3] move changelog entry [skip ci] --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bf76954e050..2fa452bf7523 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ - `[jest-core]` Don't report PerformanceObserver as open handle ([#11123](https://github.com/facebook/jest/pull/11123)) - `[jest-core]` Use `WeakRef` to hold timers when detecting open handles ([#11277](https://github.com/facebook/jest/pull/11277)) - `[jest-each]` [**BREAKING**] Ignore excess words in headings ([#8766](https://github.com/facebook/jest/pull/8766)) +- `[jest-each]` Support array index with template strings ([#10763](https://github.com/facebook/jest/pull/10763)) - `[jest-environment]` [**BREAKING**] Drop support for `runScript` for test environments ([#11155](https://github.com/facebook/jest/pull/11155)) - `[jest-environment-jsdom]` Use inner realm’s `ArrayBuffer` constructor ([#10885](https://github.com/facebook/jest/pull/10885)) - `[jest-environment-jsdom]` [**BREAKING**] Remove Node globals `setImmediate` and `clearImmediate` [#11222](https://github.com/facebook/jest/pull/11222) @@ -130,7 +131,6 @@ ### Features - `[jest-core]` Add `findRelatedTests` and `nonFlagArgs` in allowed config options for `updateConfigAndRun` in watch plugins ([#10659](https://github.com/facebook/jest/pull/10659)) -- `[jest-each]` Support array index with template strings ([#10763](https://github.com/facebook/jest/pull/10763)) ### Fixes