Skip to content

Commit

Permalink
Fix global imports
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips committed Mar 1, 2019
1 parent deac6c2 commit cff2dfa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 39 deletions.
36 changes: 17 additions & 19 deletions packages/jest-each/src/bind.ts
Expand Up @@ -6,13 +6,7 @@
*
*/

import {
DoneFn,
EachTable,
TemplateData,
EachTestFn,
ArrayTable,
} from '@jest/types/build/Global';
import {Global} from '@jest/types/';
import {ErrorWithStack} from 'jest-util';

import convertArrayTable from './table/array';
Expand All @@ -24,14 +18,18 @@ export type EachTests = Array<{
arguments: Array<unknown>;
}>;

type TestFn = (done?: DoneFn) => Promise<any> | void | undefined;
type TestFn = (done?: Global.DoneFn) => Promise<any> | void | undefined;
type GlobalCallback = (testName: string, fn: TestFn, timeout?: number) => void;

export default (cb: GlobalCallback, supportsDone: boolean = true) => (
table: EachTable,
...taggedTemplateData: TemplateData
table: Global.EachTable,
...taggedTemplateData: Global.TemplateData
) =>
function eachBind(title: string, test: EachTestFn, timeout?: number): void {
function eachBind(
title: string,
test: Global.EachTestFn,
timeout?: number,
): void {
try {
const tests = isArrayTable(taggedTemplateData)
? buildArrayTests(title, table)
Expand All @@ -52,17 +50,17 @@ export default (cb: GlobalCallback, supportsDone: boolean = true) => (
}
};

const isArrayTable = (data: TemplateData) => data.length === 0;
const isArrayTable = (data: Global.TemplateData) => data.length === 0;

const buildArrayTests = (title: string, table: EachTable): EachTests => {
const buildArrayTests = (title: string, table: Global.EachTable): EachTests => {
validateArrayTable(table);
return convertArrayTable(title, table as ArrayTable);
return convertArrayTable(title, table as Global.ArrayTable);
};

const buildTemplateTests = (
title: string,
table: EachTable,
taggedTemplateData: TemplateData,
table: Global.EachTable,
taggedTemplateData: Global.TemplateData,
): EachTests => {
const headings = getHeadingKeys(table[0] as string);
validateTemplateTableHeadings(headings, taggedTemplateData);
Expand All @@ -75,8 +73,8 @@ const getHeadingKeys = (headings: string): Array<string> =>
const applyArguments = (
supportsDone: boolean,
params: Array<unknown>,
test: EachTestFn,
): EachTestFn =>
test: Global.EachTestFn,
): Global.EachTestFn =>
supportsDone && params.length < test.length
? (done: DoneFn) => test(...params, done)
? (done: Global.DoneFn) => test(...params, done)
: () => test(...params);
22 changes: 14 additions & 8 deletions packages/jest-each/src/index.ts
Expand Up @@ -6,18 +6,22 @@
*
*/

import {TestFn, EachTable, TemplateData} from '@jest/types/build/Global';
import {Global} from '@jest/types';
import bind from './bind';

type Global = NodeJS.Global;

const install = (g: Global, table: EachTable, ...data: TemplateData) => {
const test = (title: string, test: TestFn, timeout?: number) =>
const install = (
g: Global,
table: Global.EachTable,
...data: Global.TemplateData
) => {
const test = (title: string, test: Global.TestFn, timeout?: number) =>
bind(g.test)(table, ...data)(title, test, timeout);
test.skip = bind(g.test.skip)(table, ...data);
test.only = bind(g.test.only)(table, ...data);

const it = (title: string, test: TestFn, timeout?: number) =>
const it = (title: string, test: Global.TestFn, timeout?: number) =>
bind(g.it)(table, ...data)(title, test, timeout);
it.skip = bind(g.it.skip)(table, ...data);
it.only = bind(g.it.only)(table, ...data);
Expand All @@ -26,7 +30,7 @@ const install = (g: Global, table: EachTable, ...data: TemplateData) => {
const fit = bind(g.fit)(table, ...data);
const xtest = bind(g.xtest)(table, ...data);

const describe = (title: string, suite: TestFn, timeout?: number) =>
const describe = (title: string, suite: Global.TestFn, timeout?: number) =>
bind(g.describe, false)(table, ...data)(title, suite, timeout);
describe.skip = bind(g.describe.skip, false)(table, ...data);
describe.only = bind(g.describe.only, false)(table, ...data);
Expand All @@ -36,11 +40,13 @@ const install = (g: Global, table: EachTable, ...data: TemplateData) => {
return {describe, fdescribe, fit, it, test, xdescribe, xit, xtest};
};

const each = (table: EachTable, ...data: TemplateData) =>
const each = (table: Global.EachTable, ...data: Global.TemplateData) =>
install(global, table, ...data);

each.withGlobal = (g: Global) => (table: EachTable, ...data: TemplateData) =>
install(g, table, ...data);
each.withGlobal = (g: Global) => (
table: Global.EachTable,
...data: Global.TemplateData
) => install(g, table, ...data);

export {bind};

Expand Down
21 changes: 14 additions & 7 deletions packages/jest-each/src/table/array.ts
@@ -1,27 +1,34 @@
import util from 'util';
import pretty from 'pretty-format';

import {ArrayTable, Table, Row, Col} from '@jest/types/build/Global';
import {Global} from '@jest/types';
import {EachTests} from '../bind';

const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g;
const PRETTY_PLACEHOLDER = '%p';
const INDEX_PLACEHOLDER = '%#';

export default (title: string, arrayTable: ArrayTable): EachTests =>
export default (title: string, arrayTable: Global.ArrayTable): EachTests =>
normaliseTable(arrayTable).map((row, index) => ({
title: formatTitle(title, row, index),
arguments: row,
}));

const normaliseTable = (table: ArrayTable): Table =>
isTable(table) ? (table as Table) : (table as Row).map(colToRow);
const normaliseTable = (table: Global.ArrayTable): Global.Table =>
isTable(table)
? (table as Global.Table)
: (table as Global.Row).map(colToRow);

const isTable = (table: ArrayTable): boolean => table.every(Array.isArray);
const isTable = (table: Global.ArrayTable): boolean =>
table.every(Array.isArray);

const colToRow = (col: Col): Row => [col];
const colToRow = (col: Global.Col): Global.Row => [col];

const formatTitle = (title: string, row: Row, rowIndex: number): string =>
const formatTitle = (
title: string,
row: Global.Row,
rowIndex: number,
): string =>
row.reduce<string>((formattedTitle, value) => {
const [placeholder] = getMatchingPlaceholders(formattedTitle);
if (!placeholder) return formattedTitle;
Expand Down
15 changes: 11 additions & 4 deletions packages/jest-each/src/table/template.ts
@@ -1,13 +1,17 @@
import pretty from 'pretty-format';
import {isPrimitive} from 'jest-get-type';
import {Row, Table} from '@jest/types/build/Global';
import {Global} from '@jest/types';
import {EachTests} from '../bind';

type Template = {[key: string]: unknown};
type Templates = Array<Template>;
type Headings = Array<string>;

export default (title: string, headings: Headings, row: Row): EachTests => {
export default (
title: string,
headings: Headings,
row: Global.Row,
): EachTests => {
const table = convertRowToTable(row, headings);
const templates = convertTableToTemplates(table, headings);
return templates.map(template => ({
Expand All @@ -16,15 +20,18 @@ export default (title: string, headings: Headings, row: Row): EachTests => {
}));
};

const convertRowToTable = (row: Row, headings: Headings): Table =>
const convertRowToTable = (row: Global.Row, headings: Headings): Global.Table =>
Array.from({length: row.length / headings.length}).map((_, index) =>
row.slice(
index * headings.length,
index * headings.length + headings.length,
),
);

const convertTableToTemplates = (table: Table, headings: Headings): Templates =>
const convertTableToTemplates = (
table: Global.Table,
headings: Headings,
): Templates =>
table.map(row =>
row.reduce<Template>(
(acc, value, index) => Object.assign(acc, {[headings[index]]: value}),
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-types/src/Global.ts
Expand Up @@ -23,7 +23,9 @@ export type ArrayTable = Table | Row;
export type TemplateTable = TemplateStringsArray;
export type TemplateData = Array<unknown>;
export type EachTable = ArrayTable | TemplateTable;
export type EachTestFn = (...args: any[]) => Promise<any> | void | undefined;
export type EachTestFn = (
...args: Array<any>
) => Promise<any> | void | undefined;

type Each = (
table: EachTable,
Expand Down

0 comments on commit cff2dfa

Please sign in to comment.