Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jest-each] Migrate to Typescript #8007

Merged
merged 5 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-circus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions packages/jest-circus/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
"references": [
{"path": "../jest-diff"},
{"path": "../jest-each"},
{"path": "../jest-environment"},
{"path": "../jest-matcher-utils"},
{"path": "../jest-message-util"},
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-each/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -17,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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
44 changes: 28 additions & 16 deletions packages/jest-each/src/bind.js → packages/jest-each/src/bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -15,8 +14,8 @@ import {ErrorWithStack} from 'jest-util';

type Table = Array<Array<any>>;
type PrettyArgs = {
args: Array<mixed>,
title: string,
args: Array<any>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
args: Array<any>;
args: Array<unknown>;

if it doesn't cause huge issues 🙂

title: string;
};

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

export default (cb: Function, supportsDone: boolean = true) => (...args: any) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export default (cb: Function, supportsDone: boolean = true) => (...args: any) =>
export default (cb: Function, supportsDone: boolean = true) => (...args: Array<unknown>) =>

or at least Array<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;

Expand Down Expand Up @@ -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<any>) => 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<number>, placeholder, index) => {
if (placeholder === PRETTY_PLACEHOLDER) {
indexes.push(index);
}
return indexes;
}, []);

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

Expand Down Expand Up @@ -164,13 +164,15 @@ const arrayFormat = (title, rowIndex, ...args) => {
);
};

type Done = () => {};

const applyRestParams = (
supportsDone: boolean,
params: Array<any>,
test: Function,
) =>
supportsDone && params.length < test.length
? done => test(...params, done)
? (done: Done) => test(...params, done)
: () => test(...params);

const getHeadingKeys = (headings: string): Array<string> =>
Expand All @@ -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<string>,
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);

Expand All @@ -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<string>) => {
const getPath = (
o: {[key: string]: any},
[head, ...tail]: Array<string>,
): any => {
if (!head || !o.hasOwnProperty || !o.hasOwnProperty(head)) return o;
return getPath(o[head], tail);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed>) => {
const test = (title: string, test: Function, timeout: number) =>
const install = (g: Global, ...args: Array<any>) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const install = (g: Global, ...args: Array<any>) => {
const install = (g: Global, ...args: Array<unknown>) => {

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);
Expand All @@ -35,7 +25,7 @@ const install = (g: GlobalCallbacks, ...args: Array<mixed>) => {
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);
Expand All @@ -45,10 +35,9 @@ const install = (g: GlobalCallbacks, ...args: Array<mixed>) => {
return {describe, fdescribe, fit, it, test, xdescribe, xit, xtest};
};

const each = (...args: Array<mixed>) => install(global, ...args);
const each = (...args: Array<any>) => install(global, ...args);

each.withGlobal = (g: GlobalCallbacks) => (...args: Array<mixed>) =>
install(g, ...args);
each.withGlobal = (g: Global) => (...args: Array<any>) => install(g, ...args);

export {bind};

Expand Down
13 changes: 13 additions & 0 deletions packages/jest-each/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
},
"references": [
{"path": "../jest-get-type"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../pretty-format"}
]
}