Skip to content

Commit e5c70ac

Browse files
mattphillipsSimenB
authored andcommitted
[jest-each] Migrate to Typescript (#8007)
1 parent ec5e2d0 commit e5c70ac

File tree

12 files changed

+56
-38
lines changed

12 files changed

+56
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
### Chore & Maintenance
3434

35+
- `[jest-each]`: Migrate to Typescript ([#8007](https://github.com/facebook/jest/pull/8007))
3536
- `[jest-environment-jsdom]`: Migrate to TypeScript ([#7985](https://github.com/facebook/jest/pull/8003))
3637
- `[jest-environment-node]`: Migrate to TypeScript ([#7985](https://github.com/facebook/jest/pull/7985))
3738
- `[*]`: 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))

packages/jest-circus/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7-
// @ts-ignore TODO Remove ignore when jest-each is migrated to ts
7+
88
import {bind as bindEach} from 'jest-each';
99
import {ErrorWithStack} from 'jest-util';
1010
import {Global} from '@jest/types';

packages/jest-circus/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
},
77
"references": [
88
{"path": "../jest-diff"},
9+
{"path": "../jest-each"},
910
{"path": "../jest-environment"},
1011
{"path": "../jest-matcher-utils"},
1112
{"path": "../jest-message-util"},

packages/jest-each/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "24.0.0",
44
"description": "Parameterised tests for Jest",
55
"main": "build/index.js",
6+
"types": "build/index.d.ts",
67
"repository": {
78
"type": "git",
89
"url": "https://github.com/facebook/jest.git",
@@ -17,6 +18,7 @@
1718
"author": "Matt Phillips (mattphillips)",
1819
"license": "MIT",
1920
"dependencies": {
21+
"@jest/types": "^24.1.0",
2022
"chalk": "^2.0.1",
2123
"jest-get-type": "^24.0.0",
2224
"jest-util": "^24.0.0",

packages/jest-each/src/__tests__/array.test.js renamed to packages/jest-each/src/__tests__/array.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const get = (object, lensPath) =>
1616
lensPath.reduce((acc, key) => acc[key], object);
1717

1818
const getGlobalTestMocks = () => {
19-
const globals = {
19+
const globals: any = {
2020
describe: jest.fn(),
2121
fdescribe: jest.fn(),
2222
fit: jest.fn(),

packages/jest-each/src/__tests__/template.test.js renamed to packages/jest-each/src/__tests__/template.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const get = (object, lensPath) =>
1515
lensPath.reduce((acc, key) => acc[key], object);
1616

1717
const getGlobalTestMocks = () => {
18-
const globals = {
18+
const globals: any = {
1919
describe: jest.fn(),
2020
fdescribe: jest.fn(),
2121
fit: jest.fn(),

packages/jest-each/src/bind.js renamed to packages/jest-each/src/bind.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
87
*/
98

109
import util from 'util';
@@ -15,8 +14,8 @@ import {ErrorWithStack} from 'jest-util';
1514

1615
type Table = Array<Array<any>>;
1716
type PrettyArgs = {
18-
args: Array<mixed>,
19-
title: string,
17+
args: Array<any>;
18+
title: string;
2019
};
2120

2221
const EXPECTED_COLOR = chalk.green;
@@ -26,7 +25,7 @@ const PRETTY_PLACEHOLDER = '%p';
2625
const INDEX_PLACEHOLDER = '%#';
2726

2827
export default (cb: Function, supportsDone: boolean = true) => (...args: any) =>
29-
function eachBind(title: string, test: Function, timeout: number): void {
28+
function eachBind(title: string, test: Function, timeout?: number): void {
3029
if (args.length === 1) {
3130
const [tableArg] = args;
3231

@@ -122,19 +121,20 @@ export default (cb: Function, supportsDone: boolean = true) => (...args: any) =>
122121
);
123122
};
124123

125-
const isTaggedTemplateLiteral = array => array.raw !== undefined;
126-
const isEmptyTable = table => table.length === 0;
127-
const isEmptyString = str => typeof str === 'string' && str.trim() === '';
124+
const isTaggedTemplateLiteral = (array: any) => array.raw !== undefined;
125+
const isEmptyTable = (table: Array<any>) => table.length === 0;
126+
const isEmptyString = (str: string) =>
127+
typeof str === 'string' && str.trim() === '';
128128

129-
const getPrettyIndexes = placeholders =>
130-
placeholders.reduce((indexes, placeholder, index) => {
129+
const getPrettyIndexes = (placeholders: RegExpMatchArray) =>
130+
placeholders.reduce((indexes: Array<number>, placeholder, index) => {
131131
if (placeholder === PRETTY_PLACEHOLDER) {
132132
indexes.push(index);
133133
}
134134
return indexes;
135135
}, []);
136136

137-
const arrayFormat = (title, rowIndex, ...args) => {
137+
const arrayFormat = (title: string, rowIndex: number, ...args: Array<any>) => {
138138
const placeholders = title.match(SUPPORTED_PLACEHOLDERS) || [];
139139
const prettyIndexes = getPrettyIndexes(placeholders);
140140

@@ -164,13 +164,15 @@ const arrayFormat = (title, rowIndex, ...args) => {
164164
);
165165
};
166166

167+
type Done = () => {};
168+
167169
const applyRestParams = (
168170
supportsDone: boolean,
169171
params: Array<any>,
170172
test: Function,
171173
) =>
172174
supportsDone && params.length < test.length
173-
? done => test(...params, done)
175+
? (done: Done) => test(...params, done)
174176
: () => test(...params);
175177

176178
const getHeadingKeys = (headings: string): Array<string> =>
@@ -190,10 +192,15 @@ const buildTable = (
190192
),
191193
);
192194

193-
const getMatchingKeyPaths = title => (matches, key) =>
194-
matches.concat(title.match(new RegExp(`\\$${key}[\\.\\w]*`, 'g')) || []);
195+
const getMatchingKeyPaths = (title: string) => (
196+
matches: Array<string>,
197+
key: string,
198+
) => matches.concat(title.match(new RegExp(`\\$${key}[\\.\\w]*`, 'g')) || []);
195199

196-
const replaceKeyPathWithValue = data => (title, match) => {
200+
const replaceKeyPathWithValue = (data: any) => (
201+
title: string,
202+
match: string,
203+
) => {
197204
const keyPath = match.replace('$', '').split('.');
198205
const value = getPath(data, keyPath);
199206

@@ -209,12 +216,17 @@ const interpolate = (title: string, data: any) =>
209216
.reduce(replaceKeyPathWithValue(data), title);
210217

211218
const applyObjectParams = (supportsDone: boolean, obj: any, test: Function) =>
212-
supportsDone && test.length > 1 ? done => test(obj, done) : () => test(obj);
219+
supportsDone && test.length > 1
220+
? (done: Done) => test(obj, done)
221+
: () => test(obj);
213222

214223
const pluralize = (word: string, count: number) =>
215224
word + (count === 1 ? '' : 's');
216225

217-
const getPath = (o: Object, [head, ...tail]: Array<string>) => {
226+
const getPath = (
227+
o: {[key: string]: any},
228+
[head, ...tail]: Array<string>,
229+
): any => {
218230
if (!head || !o.hasOwnProperty || !o.hasOwnProperty(head)) return o;
219231
return getPath(o[head], tail);
220232
};

packages/jest-each/src/index.js renamed to packages/jest-each/src/index.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,19 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
87
*/
98

10-
import bind from './bind';
9+
type Global = NodeJS.Global;
1110

12-
type GlobalCallbacks = {
13-
test(title: string, test: Function): void,
14-
xtest(title: string, test: Function): void,
15-
it(title: string, test: Function): void,
16-
fit(title: string, test: Function): void,
17-
xit(title: string, test: Function): void,
18-
describe(title: string, test: Function): void,
19-
fdescribe(title: string, test: Function): void,
20-
xdescribe(title: string, test: Function): void,
21-
};
11+
import bind from './bind';
2212

23-
const install = (g: GlobalCallbacks, ...args: Array<mixed>) => {
24-
const test = (title: string, test: Function, timeout: number) =>
13+
const install = (g: Global, ...args: Array<any>) => {
14+
const test = (title: string, test: Function, timeout?: number) =>
2515
bind(g.test)(...args)(title, test, timeout);
2616
test.skip = bind(g.test.skip)(...args);
2717
test.only = bind(g.test.only)(...args);
2818

29-
const it = (title: string, test: Function, timeout: number) =>
19+
const it = (title: string, test: Function, timeout?: number) =>
3020
bind(g.it)(...args)(title, test, timeout);
3121
it.skip = bind(g.it.skip)(...args);
3222
it.only = bind(g.it.only)(...args);
@@ -35,7 +25,7 @@ const install = (g: GlobalCallbacks, ...args: Array<mixed>) => {
3525
const fit = bind(g.fit)(...args);
3626
const xtest = bind(g.xtest)(...args);
3727

38-
const describe = (title: string, suite: Function, timeout: number) =>
28+
const describe = (title: string, suite: Function, timeout?: number) =>
3929
bind(g.describe, false)(...args)(title, suite, timeout);
4030
describe.skip = bind(g.describe.skip, false)(...args);
4131
describe.only = bind(g.describe.only, false)(...args);
@@ -45,10 +35,9 @@ const install = (g: GlobalCallbacks, ...args: Array<mixed>) => {
4535
return {describe, fdescribe, fit, it, test, xdescribe, xit, xtest};
4636
};
4737

48-
const each = (...args: Array<mixed>) => install(global, ...args);
38+
const each = (...args: Array<any>) => install(global, ...args);
4939

50-
each.withGlobal = (g: GlobalCallbacks) => (...args: Array<mixed>) =>
51-
install(g, ...args);
40+
each.withGlobal = (g: Global) => (...args: Array<any>) => install(g, ...args);
5241

5342
export {bind};
5443

packages/jest-each/tsconfig.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "build"
6+
},
7+
"references": [
8+
{"path": "../jest-get-type"},
9+
{"path": "../jest-types"},
10+
{"path": "../jest-util"},
11+
{"path": "../pretty-format"}
12+
]
13+
}

0 commit comments

Comments
 (0)