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

feat: add test.failing method #12610

Merged
merged 24 commits into from May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b8a31ce
Add purposefully failing test case.
michalwarda Mar 27, 2022
b48dc98
Handle failing with only and skipped
michalwarda Mar 27, 2022
e5516b3
Add documentation about failing tests
michalwarda Mar 27, 2022
7547ebe
Add failing to types
michalwarda Apr 26, 2022
159c4f8
Add info about failing to changelog
michalwarda Apr 26, 2022
839964c
Merge branch 'main' into main
michalwarda Apr 26, 2022
913623a
Add missing specs and docs for skipping with different aliases
michalwarda Apr 26, 2022
a4cecd8
Fix typos in README and specs
michalwarda Apr 27, 2022
57e8e51
Fix usage type in GlobalAPI.md
michalwarda Apr 27, 2022
c52e21d
Merge branch 'main' into michalwarda_main
SimenB Apr 30, 2022
d9a5af8
snapshot
SimenB Apr 30, 2022
a0df0bf
move changelog entry
SimenB Apr 30, 2022
fde999d
correct changelog link
SimenB Apr 30, 2022
bf007fa
Add another point to README for failing test.
michalwarda May 5, 2022
874d454
Add skipping failing functionality with jasmine
michalwarda May 5, 2022
4ed39f6
Add skipping failing functionality to concurrent
michalwarda May 5, 2022
73e8484
Merge branch 'facebook:main' into main
michalwarda May 5, 2022
60ffb4c
Add type tests for concurrent functions
michalwarda May 5, 2022
3e70058
Fix typos in tests
michalwarda May 5, 2022
9faefc5
Throw when trying to use `failing` in jasmine
michalwarda May 6, 2022
a804799
fix codeframe
SimenB May 6, 2022
f8cc53d
throw error for failing in jasmine
SimenB May 6, 2022
a02b159
Update packages/jest-jasmine2/src/jasmineAsyncInstall.ts
SimenB May 6, 2022
368eefd
sort
SimenB May 6, 2022
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
Expand Up @@ -2,6 +2,7 @@

### Features

- `[jest-circus]` Add `failing` test modifier that inverts the behaviour of tests ([#12610](https://github.com/facebook/jest/pull/12610))
- `[jest-environment-node, jest-environment-jsdom]` Allow specifying `customExportConditions` ([#12774](https://github.com/facebook/jest/pull/12774))

### Fixes
Expand Down
56 changes: 56 additions & 0 deletions docs/GlobalAPI.md
Expand Up @@ -731,6 +731,62 @@ test.each`
});
SimenB marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

oh, and test.concurrent.failing should work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I think I've managed to cover this! Though I'm not sure how to document it because it will be at least another few cases in README. test.concurrent.failing, test.concurrent.skip.failing, test.concurrent.only.failing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I add all of those? If so do we want it to say like Works like test.failing but concurrently? Or a whole copy of description?

Copy link
Member

Choose a reason for hiding this comment

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

I agree we should refactor these docs to not mention every variant, but rather which modifiers are compatible with each other. Not sure how to best do that though, maybe we can leave it for a follow-up?

```

### `test.failing(name, fn, timeout)`

Also under the alias: `it.failing(name, fn, timeout)`

:::note

This is only available with the default [jest-circus](https://github.com/facebook/jest/tree/main/packages/jest-circus) runner.

:::

Use `test.failing` when you are writing a test and expecting it to fail. These tests will behave the other way normal tests do. If `failing` test will throw any errors then it will pass. If it does not throw it will fail.

:::tip

You can use this type of tests i.e. when writing code in a BDD way. In that case the tests will not show up as failing until they pass. Then you can just remove the `failing` modifier to make them pass.
michalwarda marked this conversation as resolved.
Show resolved Hide resolved

It can also be a nice way to contribute failing tests to a project, even if you don't know how to fix the bug.

:::

Example:

```js
test.failing('it is not equal', () => {
expect(5).toBe(6); // this test will pass
});

test.failing('it is equal', () => {
expect(10).toBe(10); // this test will fail
});
```

### `test.only.failing(name, fn, timeout)`

Also under the aliases: `it.only.failing(name, fn, timeout)`, `fit.failing(name, fn, timeout)`

:::note

This is only available with the default [jest-circus](https://github.com/facebook/jest/tree/main/packages/jest-circus) runner.

:::

Use `test.only.failing` if you want to only run a specific failing test.

### `test.skip.failing(name, fn, timeout)`

Also under the aliases: `it.skip.failing(name, fn, timeout)`, `xit.failing(name, fn, timeout)`, `xtest.failing(name, fn, timeout)`

:::note

This is only available with the default [jest-circus](https://github.com/facebook/jest/tree/main/packages/jest-circus) runner.

:::

Use `test.skip.failing` if you want to skip running a specific failing test.

### `test.only(name, fn, timeout)`

Also under the aliases: `it.only(name, fn, timeout)`, and `fit(name, fn, timeout)`
Expand Down
Expand Up @@ -16,7 +16,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = `
14 | });
15 | });

at eventHandler (../../packages/jest-circus/build/eventHandler.js:145:11)
at eventHandler (../../packages/jest-circus/build/eventHandler.js:153:11)
at test (__tests__/asyncDefinition.test.js:12:5)

● Test suite failed to run
Expand Down Expand Up @@ -46,7 +46,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = `
20 | });
21 |

at eventHandler (../../packages/jest-circus/build/eventHandler.js:145:11)
at eventHandler (../../packages/jest-circus/build/eventHandler.js:153:11)
at test (__tests__/asyncDefinition.test.js:18:3)

● Test suite failed to run
Expand Down
226 changes: 226 additions & 0 deletions e2e/__tests__/__snapshots__/testFailing.test.ts.snap
@@ -0,0 +1,226 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`works with all statuses 1`] = `
"FAIL __tests__/statuses.test.js
✓ passes
✕ fails
✓ failing fails = passes
✓ failing fails = passes with test syntax
✕ failing passes = fails
○ skipped skips
○ skipped skipped failing 1
○ skipped skipped failing 2
○ skipped skipped failing with different syntax
○ skipped skipped failing with another different syntax
✎ todo todo

● fails

expect(received).toBe(expected) // Object.is equality

Expected: 101
Received: 10

11 |
12 | it('fails', () => {
> 13 | expect(10).toBe(101);
| ^
14 | });
15 |
16 | it.skip('skips', () => {

at Object.toBe (__tests__/statuses.test.js:13:14)

● failing passes = fails

Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error.

36 | });
37 |
> 38 | it.failing('failing passes = fails', () => {
| ^
39 | expect(10).toBe(10);
40 | });
41 |

at Object.failing (__tests__/statuses.test.js:38:4)"
`;

exports[`works with concurrent and only mode 1`] = `
"FAIL __tests__/worksWithConcurrentOnlyMode.test.js
block with concurrent
✕ failing passes = fails
✓ failing fails = passes
○ skipped skipped failing test
○ skipped skipped failing fails

● block with concurrent › failing passes = fails

Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error.

11 | });
12 |
> 13 | it.concurrent.only.failing('failing passes = fails', () => {
| ^
14 | expect(10).toBe(10);
15 | });
16 |

at failing (__tests__/worksWithConcurrentOnlyMode.test.js:13:22)
at Object.describe (__tests__/worksWithConcurrentOnlyMode.test.js:8:1)"
`;

exports[`works with concurrent mode 1`] = `
"FAIL __tests__/worksWithConcurrentMode.test.js
block with concurrent
✕ failing test
✕ failing passes = fails
✓ failing fails = passes
○ skipped skipped failing fails

● block with concurrent › failing test

expect(received).toBe(expected) // Object.is equality

Expected: 101
Received: 10

8 | describe('block with concurrent', () => {
9 | it('failing test', () => {
> 10 | expect(10).toBe(101);
| ^
11 | });
12 |
13 | it.concurrent.failing('failing passes = fails', () => {

at Object.toBe (__tests__/worksWithConcurrentMode.test.js:10:16)

● block with concurrent › failing passes = fails

Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error.

11 | });
12 |
> 13 | it.concurrent.failing('failing passes = fails', () => {
| ^
14 | expect(10).toBe(10);
15 | });
16 |

at failing (__tests__/worksWithConcurrentMode.test.js:13:17)
at Object.describe (__tests__/worksWithConcurrentMode.test.js:8:1)"
`;

exports[`works with only mode 1`] = `
"FAIL __tests__/worksWithOnlyMode.test.js
block with only, should pass
✓ failing fails = passes, should pass
○ skipped failing test but skipped
○ skipped passing test but skipped
block with only, should fail
✕ failing passes = fails, should fail
○ skipped failing test but skipped
○ skipped passing test but skipped
block with only in other it, should skip
✕ failing test
○ skipped failing passes = fails, should fail but skipped
○ skipped passing test but skipped
block with only with different syntax, should fail
✕ failing passes = fails, should fail 1
✕ failing passes = fails, should fail 2
○ skipped failing test but skipped
○ skipped passing test but skipped

● block with only, should fail › failing passes = fails, should fail

Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error.
SimenB marked this conversation as resolved.
Show resolved Hide resolved

21 |
22 | describe('block with only, should fail', () => {
> 23 | it.only.failing('failing passes = fails, should fail', () => {
| ^
24 | expect(10).toBe(10);
25 | });
26 |

at failing (__tests__/worksWithOnlyMode.test.js:23:11)
at Object.describe (__tests__/worksWithOnlyMode.test.js:22:1)

● block with only in other it, should skip › failing test

expect(received).toBe(expected) // Object.is equality

Expected: 101
Received: 10

41 | // eslint-disable-next-line jest/no-focused-tests
42 | it.only('failing test', () => {
> 43 | expect(10).toBe(101);
| ^
44 | });
45 |
46 | it('passing test but skipped', () => {

at Object.toBe (__tests__/worksWithOnlyMode.test.js:43:16)

● block with only with different syntax, should fail › failing passes = fails, should fail 1

Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error.

50 |
51 | describe('block with only with different syntax, should fail', () => {
> 52 | fit.failing('failing passes = fails, should fail 1', () => {
| ^
53 | expect(10).toBe(10);
54 | });
55 |

at failing (__tests__/worksWithOnlyMode.test.js:52:7)
at Object.describe (__tests__/worksWithOnlyMode.test.js:51:1)

● block with only with different syntax, should fail › failing passes = fails, should fail 2

Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error.

54 | });
55 |
> 56 | test.only.failing('failing passes = fails, should fail 2', () => {
| ^
57 | expect(10).toBe(10);
58 | });
59 |

at failing (__tests__/worksWithOnlyMode.test.js:56:13)
at Object.describe (__tests__/worksWithOnlyMode.test.js:51:1)"
`;

exports[`works with skip mode 1`] = `
"FAIL __tests__/worksWithSkipMode.test.js
block with only, should pass
✕ failing test
✓ failing fails = passes
○ skipped skipped failing fails = passes, should pass
○ skipped passing test
block with only, should fail
✓ passing test
✓ failing passes = fails
○ skipped failing passes = fails, should fail
○ skipped failing test

● block with only, should pass › failing test

expect(received).toBe(expected) // Object.is equality

Expected: 101
Received: 10

12 |
13 | it('failing test', () => {
> 14 | expect(10).toBe(101);
| ^
15 | });
16 |
17 | it.skip('passing test', () => {

at Object.toBe (__tests__/worksWithSkipMode.test.js:14:16)"
`;