Skip to content

Commit

Permalink
Add each to jest-cirucs (#6309)
Browse files Browse the repository at this point in the history
* Add flowtypes to jest-each entrypoint

* Add jest-each dependency to jest-circus

* Add each circus test and describe

* Update broken snapshot

* Add describe only and skip tests

* Add changelog entry

* Fix lint warning

* Move failing describe.each test to bottom of file

* Update jest-each entry types to Array<mixed>

* Update changelog

* Update docs to point to node util format
  • Loading branch information
mattphillips authored and cpojer committed May 28, 2018
1 parent 7c78f3d commit 5b77278
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 79 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

### Chore & Maintenance

* `[jest-circus]` Add dependency on jest-each ([#6309](https://github.com/facebook/jest/pull/#6309))
* `[filenames]` Rename "integration-tests" to "e2e" ([#6315](https://github.com/facebook/jest/pull/6315))

## 23.0.1
Expand Down
141 changes: 77 additions & 64 deletions e2e/__tests__/__snapshots__/each.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`runs only the describe.only.each tests 1`] = `
"PASS __tests__/describe-only.test.js
passes all rows expected true == true
✓ passes
✓ passes
fails all rows expected false == true
○ skipped 1 test
"
`;

exports[`shows error message when not enough arguments are supplied to tests 1`] = `
"FAIL __tests__/each-exception.test.js
✕ throws exception when not enough arguments are supplied $left == $right
Expand Down Expand Up @@ -41,6 +52,8 @@ exports[`shows only the tests without .skip as being ran 1`] = `
✓ passes one row expected true == true
✓ passes one row expected true == true
○ skipped 4 tests
passes all rows expected true == true
○ skipped 2 tests
"
`;
Expand All @@ -55,14 +68,14 @@ exports[`shows the correct errors in stderr when failing tests 1`] = `
✓ template table fails on one row expected: true == true
✕ template table fails on all rows expected: 1 == 2
✕ template table fails on all rows expected: 3 == 4
array table describe fails on all rows expected a == b
✕ fails
array table describe fails on all rows expected c == d
✕ fails
template table describe fails on all rows expected a == b
✕ fails
template table describe fails on all rows expected c == d
✕ fails
array table describe fails on all rows expected a == b
✕ fails
array table describe fails on all rows expected c == d
✕ fails
● array table fails on one row: expected true == false
Expand Down Expand Up @@ -115,56 +128,22 @@ exports[`shows the correct errors in stderr when failing tests 1`] = `
at __tests__/failure.test.js:18:18
● array table describe fails on all rows expected a == b › fails
expect(received).toBe(expected) // Object.is equality
Expected: \\"b\\"
Received: \\"a\\"
24 | (left, right) => {
25 | it('fails', () => {
> 26 | expect(left).toBe(right);
| ^
27 | });
28 | }
29 | );
at __tests__/failure.test.js:26:20
● array table describe fails on all rows expected c == d › fails
expect(received).toBe(expected) // Object.is equality
Expected: \\"d\\"
Received: \\"c\\"
24 | (left, right) => {
25 | it('fails', () => {
> 26 | expect(left).toBe(right);
| ^
27 | });
28 | }
29 | );
at __tests__/failure.test.js:26:20
● template table fails on one row expected: true == false
expect(received).toBe(expected) // Object.is equality
Expected: false
Received: true
36 | 'template table fails on one row expected: $left == $right',
37 | ({left, right}) => {
> 38 | expect(left).toBe(right);
27 | 'template table fails on one row expected: $left == $right',
28 | ({left, right}) => {
> 29 | expect(left).toBe(right);
| ^
39 | }
40 | );
41 |
30 | }
31 | );
32 |
at __tests__/failure.test.js:38:18
at __tests__/failure.test.js:29:18
● template table fails on all rows expected: 1 == 2
Expand All @@ -173,15 +152,15 @@ exports[`shows the correct errors in stderr when failing tests 1`] = `
Expected: 2
Received: 1
47 | 'template table fails on all rows expected: $left == $right',
48 | ({left, right}) => {
> 49 | expect(left).toBe(right);
38 | 'template table fails on all rows expected: $left == $right',
39 | ({left, right}) => {
> 40 | expect(left).toBe(right);
| ^
50 | }
51 | );
52 |
41 | }
42 | );
43 |
at __tests__/failure.test.js:49:18
at __tests__/failure.test.js:40:18
● template table fails on all rows expected: 3 == 4
Expand All @@ -190,15 +169,15 @@ exports[`shows the correct errors in stderr when failing tests 1`] = `
Expected: 4
Received: 3
47 | 'template table fails on all rows expected: $left == $right',
48 | ({left, right}) => {
> 49 | expect(left).toBe(right);
38 | 'template table fails on all rows expected: $left == $right',
39 | ({left, right}) => {
> 40 | expect(left).toBe(right);
| ^
50 | }
51 | );
52 |
41 | }
42 | );
43 |
at __tests__/failure.test.js:49:18
at __tests__/failure.test.js:40:18
● template table describe fails on all rows expected a == b › fails
Expand All @@ -207,8 +186,42 @@ exports[`shows the correct errors in stderr when failing tests 1`] = `
Expected: \\"b\\"
Received: \\"a\\"
59 | ({left, right}) => {
60 | it('fails ', () => {
50 | ({left, right}) => {
51 | it('fails ', () => {
> 52 | expect(left).toBe(right);
| ^
53 | });
54 | }
55 | );
at __tests__/failure.test.js:52:20
● template table describe fails on all rows expected c == d › fails
expect(received).toBe(expected) // Object.is equality
Expected: \\"d\\"
Received: \\"c\\"
50 | ({left, right}) => {
51 | it('fails ', () => {
> 52 | expect(left).toBe(right);
| ^
53 | });
54 | }
55 | );
at __tests__/failure.test.js:52:20
● array table describe fails on all rows expected a == b › fails
expect(received).toBe(expected) // Object.is equality
Expected: \\"b\\"
Received: \\"a\\"
59 | (left, right) => {
60 | it('fails', () => {
> 61 | expect(left).toBe(right);
| ^
62 | });
Expand All @@ -217,15 +230,15 @@ exports[`shows the correct errors in stderr when failing tests 1`] = `
at __tests__/failure.test.js:61:20
template table describe fails on all rows expected c == d › fails
array table describe fails on all rows expected c == d › fails
expect(received).toBe(expected) // Object.is equality
Expected: \\"d\\"
Received: \\"c\\"
59 | ({left, right}) => {
60 | it('fails ', () => {
59 | (left, right) => {
60 | it('fails', () => {
> 61 | expect(left).toBe(right);
| ^
62 | });
Expand Down
10 changes: 7 additions & 3 deletions e2e/__tests__/each.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ const path = require('path');
const runJest = require('../runJest');
const {extractSummary} = require('../Utils');
const dir = path.resolve(__dirname, '../each');
const ConditionalTest = require('../../scripts/ConditionalTest');

ConditionalTest.skipSuiteOnJestCircus();

test('works with passing tests', () => {
const result = runJest(dir, ['success.test.js']);
Expand Down Expand Up @@ -48,7 +45,14 @@ test('shows only the tests with .only as being ran', () => {

test('shows only the tests without .skip as being ran', () => {
const result = runJest(dir, ['each-skip.test.js']);
const {rest} = extractSummary(result.stderr);
expect(rest).toMatchSnapshot();
expect(result.status).toBe(0);
});

test('runs only the describe.only.each tests', () => {
const result = runJest(dir, ['describe-only.test.js']);
const {rest} = extractSummary(result.stderr);
expect(rest).toMatchSnapshot();
expect(result.status).toBe(0);
});
26 changes: 26 additions & 0 deletions e2e/each/__tests__/describe-only.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2018-present, Facebook, Inc. 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.
*/

describe.only.each([[true, true], [true, true]])(
'passes all rows expected %s == %s',
(left, right) => {
it('passes', () => {
expect(left).toBe(right);
});
}
);

// This failing tests should never because of the above `only` so the suite
// should pass
describe.each([[false, true]])(
'fails all rows expected %s == %s',
(left, right) => {
it('fails', () => {
expect(left).toBe(right);
});
}
);
9 changes: 9 additions & 0 deletions e2e/each/__tests__/each-skip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ it.skip.each`
expect(left).toBe(right);
}
);

describe.skip.each([[true, true], [true, true]])(
'passes all rows expected %s == %s',
(left, right) => {
it('passes', () => {
expect(left).toBe(right);
});
}
);
18 changes: 9 additions & 9 deletions e2e/each/__tests__/failure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ it.each([[1, 2], [3, 4]])(
}
);

describe.each([['a', 'b'], ['c', 'd']])(
'array table describe fails on all rows expected %s == %s',
(left, right) => {
it('fails', () => {
expect(left).toBe(right);
});
}
);

it.each`
left | right
${true} | ${false}
Expand Down Expand Up @@ -62,3 +53,12 @@ describe.each`
});
}
);

describe.each([['a', 'b'], ['c', 'd']])(
'array table describe fails on all rows expected %s == %s',
(left, right) => {
it('fails', () => {
expect(left).toBe(right);
});
}
);
1 change: 1 addition & 0 deletions packages/jest-circus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"expect": "^23.0.1",
"is-generator-fn": "^1.0.0",
"jest-diff": "^23.0.1",
"jest-each": "^23.0.1",
"jest-matcher-utils": "^23.0.1",
"jest-message-util": "^23.0.0",
"jest-snapshot": "^23.0.1",
Expand Down
9 changes: 9 additions & 0 deletions packages/jest-circus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
BlockName,
TestName,
} from 'types/Circus';
import {bind as bindEach} from 'jest-each';
import {dispatch} from './state';

type THook = (fn: HookFn, timeout?: number) => void;
Expand Down Expand Up @@ -105,6 +106,14 @@ test.only = (testName: TestName, fn: TestFn, timeout?: number) => {
});
};

test.each = bindEach(test);
test.only.each = bindEach(test.only);
test.skip.each = bindEach(test.skip);

describe.each = bindEach(describe);
describe.only.each = bindEach(describe.only);
describe.skip.each = bindEach(describe.skip);

module.exports = {
afterAll,
afterEach,
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-each/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jest-each allows you to provide multiple arguments to your `test`/`describe` whi
* `.describe.skip` to skip the parameterised suite of tests
* Also under the aliases: `.xdescribe`
* Asynchronous tests with `done`
* Unique test titles with: [sprintf](https://github.com/alexei/sprintf.js)
* Unique test titles with: [Node util.format](https://nodejs.org/api/util.html#util_util_format_format_args)
* 🖖 Spock like data tables with [Tagged Template Literals](#tagged-template-literal-of-rows)

---
Expand Down Expand Up @@ -72,7 +72,9 @@ jest-each is a default export so it can be imported with whatever name you like.
```js
// es6
import each from 'jest-each';
```

```js
// es5
const each = require('jest-each');
```
Expand Down
13 changes: 11 additions & 2 deletions packages/jest-each/src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
* @flow
*/

import bind from './bind';
import arrayEach from './array';
import templateEach from './template';

const each = (...args) => {
const each = (...args: Array<mixed>) => {
if (args.length > 1) {
return templateEach(global)(...args);
}

return arrayEach(global)(...args);
};

each.withGlobal = g => (...args) => {
each.withGlobal = g => (...args: Array<mixed>) => {
if (args.length > 1) {
return templateEach(g)(...args);
}
Expand Down

0 comments on commit 5b77278

Please sign in to comment.