Skip to content

Commit

Permalink
Migrate js src to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips committed Feb 28, 2019
1 parent 6afedc1 commit c0c7c4d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
1 change: 1 addition & 0 deletions packages/jest-each/package.json
Expand Up @@ -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",
Expand Down
44 changes: 28 additions & 16 deletions packages/jest-each/src/bind.js → packages/jest-each/src/bind.ts
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>;
title: string;
};

const EXPECTED_COLOR = chalk.green;
Expand All @@ -33,7 +32,7 @@ const PRIMITIVES = new Set([
]);

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;

Expand Down Expand Up @@ -129,19 +128,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 @@ -171,13 +171,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 @@ -197,10 +199,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);
const valueType = getType(value);
Expand All @@ -217,12 +224,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);
};
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>) => {
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
1 change: 1 addition & 0 deletions packages/jest-each/tsconfig.json
Expand Up @@ -6,6 +6,7 @@
},
"references": [
{"path": "../jest-get-type"},
{"path": "../jest-types"},
{"path": "../jest-util"},
{"path": "../pretty-format"}
]
Expand Down

0 comments on commit c0c7c4d

Please sign in to comment.