Skip to content

Commit

Permalink
feat: Support concurrent in each
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark1626 committed Jul 19, 2020
1 parent 63ebaa6 commit f2498de
Show file tree
Hide file tree
Showing 16 changed files with 342 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
### Features

- `[pretty-format]` Added support for serializing custom elements (web components) ([#10217](https://github.com/facebook/jest/pull/10237))
- `[jest-each, jest-jasmine, jest-circus]` Add support for .concurrent.each ([#9326](https://github.com/facebook/jest/pull/9326))

### Fixes

Expand Down
18 changes: 18 additions & 0 deletions e2e/__tests__/jasmineAsync.test.ts
Expand Up @@ -128,6 +128,24 @@ describe('async jasmine', () => {
expect(json.testResults[0].message).toMatch(/concurrent test fails/);
});

it('works with concurrent.each', () => {
const {json} = runWithJson('jasmine-async', ['concurrent-each.test.js']);
expect(json.numTotalTests).toBe(4);
expect(json.numPassedTests).toBe(2);
expect(json.numFailedTests).toBe(0);
expect(json.numPendingTests).toBe(2);
});

it('works with concurrent.only.each', () => {
const {json} = runWithJson('jasmine-async', [
'concurrent-only-each.test.js',
]);
expect(json.numTotalTests).toBe(4);
expect(json.numPassedTests).toBe(2);
expect(json.numFailedTests).toBe(0);
expect(json.numPendingTests).toBe(2);
});

it("doesn't execute more than 5 tests simultaneously", () => {
const {json} = runWithJson('jasmine-async', ['concurrent-many.test.js']);
expect(json.numTotalTests).toBe(10);
Expand Down
20 changes: 20 additions & 0 deletions e2e/jasmine-async/__tests__/concurrent-each.test.js
@@ -0,0 +1,20 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

it.concurrent.each([
[1, 2],
[2, 3],
])('adds one to number', async (a, b) => {
expect(a + 1).toBe(b);
});

it.concurrent.skip.each([
[1, 2],
[2, 3],
])('should skip this test', Promise.resolve());
22 changes: 22 additions & 0 deletions e2e/jasmine-async/__tests__/concurrent-only-each.test.js
@@ -0,0 +1,22 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

it.concurrent.only.each([
[1, 2],
[2, 3],
])('adds one to number', async (a, b) => {
expect(a + 1).toBe(b);
});

it.concurrent.each([
[1, 2],
[2, 3],
])('adds one to number', async (a, b) => {
expect(a + 1).toBe(b);
});
Expand Up @@ -21,6 +21,7 @@ import {
addSerializer,
buildSnapshotResolver,
} from 'jest-snapshot';
import {bind} from 'jest-each';
import throat from 'throat';
import {
ROOT_DESCRIBE_BLOCK_NAME,
Expand Down Expand Up @@ -73,7 +74,7 @@ export const initialize = async ({
nodeGlobal.test.concurrent = (test => {
const concurrent = (
testName: string,
testFn: () => Promise<any>,
testFn: () => Promise<unknown>,
timeout?: number,
) => {
// For concurrent tests we first run the function that returns promise, and then register a
Expand All @@ -86,18 +87,23 @@ export const initialize = async ({
nodeGlobal.test(testName, () => promise, timeout);
};

concurrent.only = (
const only = (
testName: string,
testFn: () => Promise<any>,
testFn: () => Promise<unknown>,
timeout?: number,
) => {
const promise = mutex(() => testFn());
// eslint-disable-next-line jest/no-focused-tests
test.only(testName, () => promise, timeout);
};

concurrent.only = only;
concurrent.skip = test.skip;

concurrent.each = bind(test, false);
concurrent.skip.each = bind(test.skip, false);
only.each = bind(test.only, false);

return concurrent;
})(nodeGlobal.test);

Expand Down
36 changes: 36 additions & 0 deletions packages/jest-each/src/__tests__/__snapshots__/array.test.ts.snap
Expand Up @@ -84,6 +84,42 @@ Instead was called with: undefined
"
`;

exports[`jest-each .test.concurrent throws an error when called with an empty array 1`] = `
"Error: \`.each\` called with an empty Array of table data.
"
`;

exports[`jest-each .test.concurrent throws an error when not called with an array 1`] = `
"\`.each\` must be called with an Array or Tagged Template Literal.
Instead was called with: undefined
"
`;

exports[`jest-each .test.concurrent.only throws an error when called with an empty array 1`] = `
"Error: \`.each\` called with an empty Array of table data.
"
`;

exports[`jest-each .test.concurrent.only throws an error when not called with an array 1`] = `
"\`.each\` must be called with an Array or Tagged Template Literal.
Instead was called with: undefined
"
`;

exports[`jest-each .test.concurrent.skip throws an error when called with an empty array 1`] = `
"Error: \`.each\` called with an empty Array of table data.
"
`;

exports[`jest-each .test.concurrent.skip throws an error when not called with an array 1`] = `
"\`.each\` must be called with an Array or Tagged Template Literal.
Instead was called with: undefined
"
`;

exports[`jest-each .test.only throws an error when called with an empty array 1`] = `
"Error: \`.each\` called with an empty Array of table data.
"
Expand Down
117 changes: 117 additions & 0 deletions packages/jest-each/src/__tests__/__snapshots__/template.test.ts.snap
Expand Up @@ -273,6 +273,123 @@ exports[`jest-each .test throws error when there are no arguments for given head
"
`;
exports[`jest-each .test.concurrent throws an error when called with an empty string 1`] = `
"Error: \`.each\` called with an empty Tagged Template Literal of table data.
"
`;
exports[`jest-each .test.concurrent throws error when there are fewer arguments than headings over multiple rows 1`] = `
"Not enough arguments supplied for given headings:
<green>a | b | expected</>
Received:
<red>Array [</>
<red> 0,</>
<red> 1,</>
<red> 1,</>
<red> 1,</>
<red> 1,</>
<red>]</>
Missing <red>2</> arguments"
`;
exports[`jest-each .test.concurrent throws error when there are fewer arguments than headings when given one row 1`] = `
"Not enough arguments supplied for given headings:
<green>a | b | expected</>
Received:
<red>Array [</>
<red> 0,</>
<red> 1,</>
<red>]</>
Missing <red>2</> arguments"
`;
exports[`jest-each .test.concurrent throws error when there are no arguments for given headings 1`] = `
"Error: \`.each\` called with a Tagged Template Literal with no data, remember to interpolate with \${expression} syntax.
"
`;
exports[`jest-each .test.concurrent.only throws an error when called with an empty string 1`] = `
"Error: \`.each\` called with an empty Tagged Template Literal of table data.
"
`;
exports[`jest-each .test.concurrent.only throws error when there are fewer arguments than headings over multiple rows 1`] = `
"Not enough arguments supplied for given headings:
<green>a | b | expected</>
Received:
<red>Array [</>
<red> 0,</>
<red> 1,</>
<red> 1,</>
<red> 1,</>
<red> 1,</>
<red>]</>
Missing <red>2</> arguments"
`;
exports[`jest-each .test.concurrent.only throws error when there are fewer arguments than headings when given one row 1`] = `
"Not enough arguments supplied for given headings:
<green>a | b | expected</>
Received:
<red>Array [</>
<red> 0,</>
<red> 1,</>
<red>]</>
Missing <red>2</> arguments"
`;
exports[`jest-each .test.concurrent.only throws error when there are no arguments for given headings 1`] = `
"Error: \`.each\` called with a Tagged Template Literal with no data, remember to interpolate with \${expression} syntax.
"
`;
exports[`jest-each .test.concurrent.skip throws an error when called with an empty string 1`] = `
"Error: \`.each\` called with an empty Tagged Template Literal of table data.
"
`;
exports[`jest-each .test.concurrent.skip throws error when there are fewer arguments than headings over multiple rows 1`] = `
"Not enough arguments supplied for given headings:
<green>a | b | expected</>
Received:
<red>Array [</>
<red> 0,</>
<red> 1,</>
<red> 1,</>
<red> 1,</>
<red> 1,</>
<red>]</>
Missing <red>2</> arguments"
`;
exports[`jest-each .test.concurrent.skip throws error when there are fewer arguments than headings when given one row 1`] = `
"Not enough arguments supplied for given headings:
<green>a | b | expected</>
Received:
<red>Array [</>
<red> 0,</>
<red> 1,</>
<red>]</>
Missing <red>2</> arguments"
`;
exports[`jest-each .test.concurrent.skip throws error when there are no arguments for given headings 1`] = `
"Error: \`.each\` called with a Tagged Template Literal with no data, remember to interpolate with \${expression} syntax.
"
`;
exports[`jest-each .test.only throws an error when called with an empty string 1`] = `
"Error: \`.each\` called with an empty Tagged Template Literal of table data.
"
Expand Down
9 changes: 9 additions & 0 deletions packages/jest-each/src/__tests__/array.test.ts
Expand Up @@ -28,6 +28,9 @@ const getGlobalTestMocks = () => {
};
globals.test.only = jest.fn();
globals.test.skip = jest.fn();
globals.test.concurrent = jest.fn();
globals.test.concurrent.only = jest.fn();
globals.test.concurrent.skip = jest.fn();
globals.it.only = jest.fn();
globals.it.skip = jest.fn();
globals.describe.only = jest.fn();
Expand All @@ -38,6 +41,9 @@ const getGlobalTestMocks = () => {
describe('jest-each', () => {
[
['test'],
['test', 'concurrent'],
['test', 'concurrent', 'only'],
['test', 'concurrent', 'skip'],
['test', 'only'],
['it'],
['fit'],
Expand Down Expand Up @@ -289,6 +295,8 @@ describe('jest-each', () => {
test.each([
[['test']],
[['test', 'only']],
[['test', 'concurrent']],
[['test', 'concurrent', 'only']],
[['it']],
[['fit']],
[['it', 'only']],
Expand Down Expand Up @@ -327,6 +335,7 @@ describe('jest-each', () => {
[
['xtest'],
['test', 'skip'],
['test', 'concurrent', 'skip'],
['xit'],
['it', 'skip'],
['xdescribe'],
Expand Down
15 changes: 15 additions & 0 deletions packages/jest-each/src/__tests__/index.test.ts
Expand Up @@ -20,6 +20,21 @@ describe('array', () => {
});
});

describe('concurrent', () => {
describe('.add', () => {
each([
[0, 0, 0],
[0, 1, 1],
[1, 1, 2],
]).test.concurrent(
'returns the result of adding %s to %s',
async (a, b, expected) => {
expect(a + b).toBe(expected);
},
);
});
});

describe('template', () => {
describe('.add', () => {
each`
Expand Down
9 changes: 9 additions & 0 deletions packages/jest-each/src/__tests__/template.test.ts
Expand Up @@ -27,6 +27,9 @@ const getGlobalTestMocks = () => {
};
globals.test.only = jest.fn();
globals.test.skip = jest.fn();
globals.test.concurrent = jest.fn();
globals.test.concurrent.only = jest.fn();
globals.test.concurrent.skip = jest.fn();
globals.it.only = jest.fn();
globals.it.skip = jest.fn();
globals.describe.only = jest.fn();
Expand All @@ -37,6 +40,9 @@ const getGlobalTestMocks = () => {
describe('jest-each', () => {
[
['test'],
['test', 'concurrent'],
['test', 'concurrent', 'only'],
['test', 'concurrent', 'skip'],
['test', 'only'],
['it'],
['fit'],
Expand Down Expand Up @@ -315,6 +321,7 @@ describe('jest-each', () => {
test.each([
[['test']],
[['test', 'only']],
[['test', 'concurrent', 'only']],
[['it']],
[['fit']],
[['it', 'only']],
Expand Down Expand Up @@ -361,6 +368,8 @@ describe('jest-each', () => {
[
['xtest'],
['test', 'skip'],
['test', 'concurrent'],
['test', 'concurrent', 'skip'],
['xit'],
['it', 'skip'],
['xdescribe'],
Expand Down

0 comments on commit f2498de

Please sign in to comment.