From 4a9a7feb32e146be62874adf7adcdaefcfe5d264 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Wed, 27 Feb 2019 22:41:50 +0000 Subject: [PATCH 1/5] Add ts setup files --- packages/jest-each/package.json | 1 + packages/jest-each/tsconfig.json | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 packages/jest-each/tsconfig.json diff --git a/packages/jest-each/package.json b/packages/jest-each/package.json index 7655f37b2505..7664311a1790 100644 --- a/packages/jest-each/package.json +++ b/packages/jest-each/package.json @@ -3,6 +3,7 @@ "version": "24.0.0", "description": "Parameterised tests for Jest", "main": "build/index.js", + "types": "build/index.d.ts", "repository": { "type": "git", "url": "https://github.com/facebook/jest.git", diff --git a/packages/jest-each/tsconfig.json b/packages/jest-each/tsconfig.json new file mode 100644 index 000000000000..11116d6bb5e4 --- /dev/null +++ b/packages/jest-each/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build" + }, + "references": [ + {"path": "../jest-get-type"}, + {"path": "../jest-util"}, + {"path": "../pretty-format"} + ] +} From e5469ce6031b16562dab41988cbd43a8fb9cf1a1 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Wed, 27 Feb 2019 22:42:02 +0000 Subject: [PATCH 2/5] Migrate js src to ts --- packages/jest-each/package.json | 1 + packages/jest-each/src/{bind.js => bind.ts} | 44 ++++++++++++------- packages/jest-each/src/{index.js => index.ts} | 27 ++++-------- packages/jest-each/tsconfig.json | 1 + 4 files changed, 38 insertions(+), 35 deletions(-) rename packages/jest-each/src/{bind.js => bind.ts} (84%) rename packages/jest-each/src/{index.js => index.ts} (54%) diff --git a/packages/jest-each/package.json b/packages/jest-each/package.json index 7664311a1790..8d85b5d9393d 100644 --- a/packages/jest-each/package.json +++ b/packages/jest-each/package.json @@ -18,6 +18,7 @@ "author": "Matt Phillips (mattphillips)", "license": "MIT", "dependencies": { + "@jest/types": "^24.1.0", "chalk": "^2.0.1", "jest-get-type": "^24.0.0", "jest-util": "^24.0.0", diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.ts similarity index 84% rename from packages/jest-each/src/bind.js rename to packages/jest-each/src/bind.ts index 8b8d95aa4f54..173c26e159c1 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.ts @@ -4,7 +4,6 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow */ import util from 'util'; @@ -15,8 +14,8 @@ import {ErrorWithStack} from 'jest-util'; type Table = Array>; type PrettyArgs = { - args: Array, - title: string, + args: Array; + title: string; }; const EXPECTED_COLOR = chalk.green; @@ -26,7 +25,7 @@ const PRETTY_PLACEHOLDER = '%p'; const INDEX_PLACEHOLDER = '%#'; export default (cb: Function, supportsDone: boolean = true) => (...args: any) => - function eachBind(title: string, test: Function, timeout: number): void { + function eachBind(title: string, test: Function, timeout?: number): void { if (args.length === 1) { const [tableArg] = args; @@ -122,19 +121,20 @@ export default (cb: Function, supportsDone: boolean = true) => (...args: any) => ); }; -const isTaggedTemplateLiteral = array => array.raw !== undefined; -const isEmptyTable = table => table.length === 0; -const isEmptyString = str => typeof str === 'string' && str.trim() === ''; +const isTaggedTemplateLiteral = (array: any) => array.raw !== undefined; +const isEmptyTable = (table: Array) => table.length === 0; +const isEmptyString = (str: string) => + typeof str === 'string' && str.trim() === ''; -const getPrettyIndexes = placeholders => - placeholders.reduce((indexes, placeholder, index) => { +const getPrettyIndexes = (placeholders: RegExpMatchArray) => + placeholders.reduce((indexes: Array, placeholder, index) => { if (placeholder === PRETTY_PLACEHOLDER) { indexes.push(index); } return indexes; }, []); -const arrayFormat = (title, rowIndex, ...args) => { +const arrayFormat = (title: string, rowIndex: number, ...args: Array) => { const placeholders = title.match(SUPPORTED_PLACEHOLDERS) || []; const prettyIndexes = getPrettyIndexes(placeholders); @@ -164,13 +164,15 @@ const arrayFormat = (title, rowIndex, ...args) => { ); }; +type Done = () => {}; + const applyRestParams = ( supportsDone: boolean, params: Array, test: Function, ) => supportsDone && params.length < test.length - ? done => test(...params, done) + ? (done: Done) => test(...params, done) : () => test(...params); const getHeadingKeys = (headings: string): Array => @@ -190,10 +192,15 @@ const buildTable = ( ), ); -const getMatchingKeyPaths = title => (matches, key) => - matches.concat(title.match(new RegExp(`\\$${key}[\\.\\w]*`, 'g')) || []); +const getMatchingKeyPaths = (title: string) => ( + matches: Array, + key: string, +) => matches.concat(title.match(new RegExp(`\\$${key}[\\.\\w]*`, 'g')) || []); -const replaceKeyPathWithValue = data => (title, match) => { +const replaceKeyPathWithValue = (data: any) => ( + title: string, + match: string, +) => { const keyPath = match.replace('$', '').split('.'); const value = getPath(data, keyPath); @@ -209,12 +216,17 @@ const interpolate = (title: string, data: any) => .reduce(replaceKeyPathWithValue(data), title); const applyObjectParams = (supportsDone: boolean, obj: any, test: Function) => - supportsDone && test.length > 1 ? done => test(obj, done) : () => test(obj); + supportsDone && test.length > 1 + ? (done: Done) => test(obj, done) + : () => test(obj); const pluralize = (word: string, count: number) => word + (count === 1 ? '' : 's'); -const getPath = (o: Object, [head, ...tail]: Array) => { +const getPath = ( + o: {[key: string]: any}, + [head, ...tail]: Array, +): any => { if (!head || !o.hasOwnProperty || !o.hasOwnProperty(head)) return o; return getPath(o[head], tail); }; diff --git a/packages/jest-each/src/index.js b/packages/jest-each/src/index.ts similarity index 54% rename from packages/jest-each/src/index.js rename to packages/jest-each/src/index.ts index 70b7baed917a..b77335f361b5 100644 --- a/packages/jest-each/src/index.js +++ b/packages/jest-each/src/index.ts @@ -4,29 +4,19 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow */ -import bind from './bind'; +type Global = NodeJS.Global; -type GlobalCallbacks = { - test(title: string, test: Function): void, - xtest(title: string, test: Function): void, - it(title: string, test: Function): void, - fit(title: string, test: Function): void, - xit(title: string, test: Function): void, - describe(title: string, test: Function): void, - fdescribe(title: string, test: Function): void, - xdescribe(title: string, test: Function): void, -}; +import bind from './bind'; -const install = (g: GlobalCallbacks, ...args: Array) => { - const test = (title: string, test: Function, timeout: number) => +const install = (g: Global, ...args: Array) => { + const test = (title: string, test: Function, timeout?: number) => bind(g.test)(...args)(title, test, timeout); test.skip = bind(g.test.skip)(...args); test.only = bind(g.test.only)(...args); - const it = (title: string, test: Function, timeout: number) => + const it = (title: string, test: Function, timeout?: number) => bind(g.it)(...args)(title, test, timeout); it.skip = bind(g.it.skip)(...args); it.only = bind(g.it.only)(...args); @@ -35,7 +25,7 @@ const install = (g: GlobalCallbacks, ...args: Array) => { const fit = bind(g.fit)(...args); const xtest = bind(g.xtest)(...args); - const describe = (title: string, suite: Function, timeout: number) => + const describe = (title: string, suite: Function, timeout?: number) => bind(g.describe, false)(...args)(title, suite, timeout); describe.skip = bind(g.describe.skip, false)(...args); describe.only = bind(g.describe.only, false)(...args); @@ -45,10 +35,9 @@ const install = (g: GlobalCallbacks, ...args: Array) => { return {describe, fdescribe, fit, it, test, xdescribe, xit, xtest}; }; -const each = (...args: Array) => install(global, ...args); +const each = (...args: Array) => install(global, ...args); -each.withGlobal = (g: GlobalCallbacks) => (...args: Array) => - install(g, ...args); +each.withGlobal = (g: Global) => (...args: Array) => install(g, ...args); export {bind}; diff --git a/packages/jest-each/tsconfig.json b/packages/jest-each/tsconfig.json index 11116d6bb5e4..b88a75787fde 100644 --- a/packages/jest-each/tsconfig.json +++ b/packages/jest-each/tsconfig.json @@ -6,6 +6,7 @@ }, "references": [ {"path": "../jest-get-type"}, + {"path": "../jest-types"}, {"path": "../jest-util"}, {"path": "../pretty-format"} ] From aed760dc1fc610bc813de52174f099b491d2dd03 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 28 Feb 2019 17:39:57 +0000 Subject: [PATCH 3/5] Migrate jest-each tests to ts; --- .../__snapshots__/{array.test.js.snap => array.test.ts.snap} | 0 .../{template.test.js.snap => template.test.ts.snap} | 0 .../jest-each/src/__tests__/{array.test.js => array.test.ts} | 2 +- .../jest-each/src/__tests__/{index.test.js => index.test.ts} | 0 .../src/__tests__/{template.test.js => template.test.ts} | 2 +- 5 files changed, 2 insertions(+), 2 deletions(-) rename packages/jest-each/src/__tests__/__snapshots__/{array.test.js.snap => array.test.ts.snap} (100%) rename packages/jest-each/src/__tests__/__snapshots__/{template.test.js.snap => template.test.ts.snap} (100%) rename packages/jest-each/src/__tests__/{array.test.js => array.test.ts} (99%) rename packages/jest-each/src/__tests__/{index.test.js => index.test.ts} (100%) rename packages/jest-each/src/__tests__/{template.test.js => template.test.ts} (99%) diff --git a/packages/jest-each/src/__tests__/__snapshots__/array.test.js.snap b/packages/jest-each/src/__tests__/__snapshots__/array.test.ts.snap similarity index 100% rename from packages/jest-each/src/__tests__/__snapshots__/array.test.js.snap rename to packages/jest-each/src/__tests__/__snapshots__/array.test.ts.snap diff --git a/packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap b/packages/jest-each/src/__tests__/__snapshots__/template.test.ts.snap similarity index 100% rename from packages/jest-each/src/__tests__/__snapshots__/template.test.js.snap rename to packages/jest-each/src/__tests__/__snapshots__/template.test.ts.snap diff --git a/packages/jest-each/src/__tests__/array.test.js b/packages/jest-each/src/__tests__/array.test.ts similarity index 99% rename from packages/jest-each/src/__tests__/array.test.js rename to packages/jest-each/src/__tests__/array.test.ts index 9dafb79def18..ceeffe197a92 100644 --- a/packages/jest-each/src/__tests__/array.test.js +++ b/packages/jest-each/src/__tests__/array.test.ts @@ -16,7 +16,7 @@ const get = (object, lensPath) => lensPath.reduce((acc, key) => acc[key], object); const getGlobalTestMocks = () => { - const globals = { + const globals: any = { describe: jest.fn(), fdescribe: jest.fn(), fit: jest.fn(), diff --git a/packages/jest-each/src/__tests__/index.test.js b/packages/jest-each/src/__tests__/index.test.ts similarity index 100% rename from packages/jest-each/src/__tests__/index.test.js rename to packages/jest-each/src/__tests__/index.test.ts diff --git a/packages/jest-each/src/__tests__/template.test.js b/packages/jest-each/src/__tests__/template.test.ts similarity index 99% rename from packages/jest-each/src/__tests__/template.test.js rename to packages/jest-each/src/__tests__/template.test.ts index 8d0ee6042735..7518ee153a79 100644 --- a/packages/jest-each/src/__tests__/template.test.js +++ b/packages/jest-each/src/__tests__/template.test.ts @@ -15,7 +15,7 @@ const get = (object, lensPath) => lensPath.reduce((acc, key) => acc[key], object); const getGlobalTestMocks = () => { - const globals = { + const globals: any = { describe: jest.fn(), fdescribe: jest.fn(), fit: jest.fn(), From 141ff99f6075868629ddff82ff20a864a0a76eb7 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 28 Feb 2019 17:46:46 +0000 Subject: [PATCH 4/5] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7e612711dae..1a2f8af388d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ ### Chore & Maintenance +- `[jest-each]`: Migrate to Typescript ([#8007](https://github.com/facebook/jest/pull/8007)) - `[jest-environment-jsdom]`: Migrate to TypeScript ([#7985](https://github.com/facebook/jest/pull/8003)) - `[jest-environment-node]`: Migrate to TypeScript ([#7985](https://github.com/facebook/jest/pull/7985)) - `[*]`: Setup building, linting and testing of TypeScript ([#7808](https://github.com/facebook/jest/pull/7808), [#7855](https://github.com/facebook/jest/pull/7855), [#7951](https://github.com/facebook/jest/pull/7951)) From b99365a1d4cf31561d099c7abecc2385ce884633 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 28 Feb 2019 20:37:09 +0000 Subject: [PATCH 5/5] Remove jest-each ts-ignore in jest-circus --- packages/jest-circus/src/index.ts | 2 +- packages/jest-circus/tsconfig.json | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/jest-circus/src/index.ts b/packages/jest-circus/src/index.ts index d4ed333084b7..788af87df0c0 100644 --- a/packages/jest-circus/src/index.ts +++ b/packages/jest-circus/src/index.ts @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ -// @ts-ignore TODO Remove ignore when jest-each is migrated to ts + import {bind as bindEach} from 'jest-each'; import {ErrorWithStack} from 'jest-util'; import {Global} from '@jest/types'; diff --git a/packages/jest-circus/tsconfig.json b/packages/jest-circus/tsconfig.json index 726c0ac6b1e5..8da1643828ba 100644 --- a/packages/jest-circus/tsconfig.json +++ b/packages/jest-circus/tsconfig.json @@ -6,6 +6,7 @@ }, "references": [ {"path": "../jest-diff"}, + {"path": "../jest-each"}, {"path": "../jest-environment"}, {"path": "../jest-matcher-utils"}, {"path": "../jest-message-util"},