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]: Add test/describe timeout support #6660

Merged
merged 7 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Fixes

- `[jest-each]` Add timeout support to parameterised tests ([#6660](https://github.com/facebook/jest/pull/6660))
- `[jest-runner]` Force parallel runs for watch mode, to avoid TTY freeze ([#6647](https://github.com/facebook/jest/pull/6647))
- `[jest-cli]` properly reprint resolver errors in watch mode ([#6407](https://github.com/facebook/jest/pull/6407))
- `[jest-cli]` Write configuration to stdout when the option was explicitly passed to Jest ([#6447](https://github.com/facebook/jest/pull/6447))
Expand Down
29 changes: 29 additions & 0 deletions packages/jest-each/src/__tests__/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'expected string',
expectFunction,
undefined,
);
});

Expand All @@ -66,10 +67,12 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'expected string',
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
'expected string',
expectFunction,
undefined,
);
});

Expand Down Expand Up @@ -111,12 +114,14 @@ describe('jest-each', () => {
foo: 'bar',
})} () => {} [] Infinity NaN`,
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
`expected string: world 1 null undefined 1.2 ${JSON.stringify({
baz: 'qux',
})} () => {} [] Infinity NaN`,
expectFunction,
undefined,
);
});

Expand All @@ -134,10 +139,12 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'expected string: hello',
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
'expected string: world',
expectFunction,
undefined,
);
});

Expand All @@ -157,12 +164,14 @@ describe('jest-each', () => {
'pretty2',
)}`,
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
`expected string: string1 ${pretty('pretty1')} string2 ${pretty(
'pretty2',
)}`,
expectFunction,
undefined,
);
});

Expand All @@ -180,10 +189,12 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
`expected string: string1 ${pretty('pretty1')} string2 %p`,
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
`expected string: string1 ${pretty('pretty1')} string2 %p`,
expectFunction,
undefined,
);
});

Expand Down Expand Up @@ -237,6 +248,20 @@ describe('jest-each', () => {
});
get(globalTestMocks, keyPath).mock.calls[0][1]('DONE');
});

test('calls gloabl with given timeout', () => {
Copy link
Member

Choose a reason for hiding this comment

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

typo

const globalTestMocks = getGlobalTestMocks();
const eachObject = each.withGlobal(globalTestMocks)([['hello']]);

const testFunction = get(eachObject, keyPath);
testFunction('some test', noop, 10000);
const globalMock = get(globalTestMocks, keyPath);
expect(globalMock).toHaveBeenCalledWith(
'some test',
expect.any(Function),
10000,
);
});
});
});

Expand Down Expand Up @@ -278,6 +303,7 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'expected string',
expectFunction,
undefined,
);
});

Expand All @@ -292,6 +318,7 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'expected string',
expectFunction,
undefined,
);
});

Expand All @@ -309,10 +336,12 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'expected string: hello 1',
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
'expected string: world 2',
expectFunction,
undefined,
);
});
});
Expand Down
31 changes: 31 additions & 0 deletions packages/jest-each/src/__tests__/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'expected string',
expectFunction,
undefined,
);
});

Expand All @@ -109,10 +110,12 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'expected string',
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
'expected string',
expectFunction,
undefined,
);
});

Expand All @@ -131,10 +134,12 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'expected string: a=0, b=1, expected=1',
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
'expected string: a=1, b=1, expected=2',
expectFunction,
undefined,
);
});

Expand All @@ -156,10 +161,12 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'add(0, 1) expected string: a=0, b=1, expected=1',
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
'add(1, 1) expected string: a=1, b=1, expected=2',
expectFunction,
undefined,
);
});

Expand All @@ -177,6 +184,7 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'interpolates object keyPath to value: "baz"',
expectFunction,
undefined,
);
});

Expand All @@ -194,6 +202,7 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'interpolates object keyPath to value: {"bar": "baz"}',
expectFunction,
undefined,
);
});

Expand Down Expand Up @@ -234,6 +243,23 @@ describe('jest-each', () => {
});
get(globalTestMocks, keyPath).mock.calls[0][1]('DONE');
});

test('calls gloabl with given timeout', () => {
Copy link
Member

Choose a reason for hiding this comment

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

typo

const globalTestMocks = getGlobalTestMocks();
const eachObject = each.withGlobal(globalTestMocks)`
a | b | expected
${0} | ${1} | ${1}
`;

const testFunction = get(eachObject, keyPath);
testFunction('some test', noop, 10000);
const globalMock = get(globalTestMocks, keyPath);
expect(globalMock).toHaveBeenCalledWith(
'some test',
expect.any(Function),
10000,
);
});
});
});

Expand Down Expand Up @@ -278,6 +304,7 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'expected string',
expectFunction,
undefined,
);
});

Expand All @@ -296,10 +323,12 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'expected string',
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
'expected string',
expectFunction,
undefined,
);
});

Expand All @@ -318,10 +347,12 @@ describe('jest-each', () => {
expect(globalMock).toHaveBeenCalledWith(
'expected string: a=0, b=1, expected=1',
expectFunction,
undefined,
);
expect(globalMock).toHaveBeenCalledWith(
'expected string: a=1, b=1, expected=2',
expectFunction,
undefined,
);
});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g;
const PRETTY_PLACEHOLDER = '%p';

export default (cb: Function) => (...args: any) =>
function eachBind(title: string, test: Function): void {
function eachBind(title: string, test: Function, timeout: number): void {
if (args.length === 1) {
const table: Table = args[0].every(Array.isArray)
? args[0]
: args[0].map(entry => [entry]);
return table.forEach(row =>
cb(arrayFormat(title, ...row), applyRestParams(row, test)),
cb(arrayFormat(title, ...row), applyRestParams(row, test), timeout),
);
}

Expand Down Expand Up @@ -65,7 +65,7 @@ export default (cb: Function) => (...args: any) =>
}

return table.forEach(row =>
cb(interpolate(title, row), applyObjectParams(row, test)),
cb(interpolate(title, row), applyObjectParams(row, test), timeout),
);
};

Expand Down
12 changes: 6 additions & 6 deletions packages/jest-each/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ type GlobalCallbacks = {
};

const install = (g: GlobalCallbacks, ...args: Array<mixed>) => {
const test = (title: string, test: Function) =>
bind(g.test)(...args)(title, test);
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) =>
bind(g.it)(...args)(title, test);
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);

const xit = bind(g.xit)(...args);
const fit = bind(g.fit)(...args);
const xtest = bind(g.xtest)(...args);

const describe = (title: string, suite: Function) =>
bind(g.describe)(...args)(title, suite);
const describe = (title: string, suite: Function, timeout: number) =>
bind(g.describe)(...args)(title, suite, timeout);
describe.skip = bind(g.describe.skip)(...args);
describe.only = bind(g.describe.only)(...args);
const fdescribe = bind(g.fdescribe)(...args);
Expand Down