From c29e59fc14f9a9b61aa3092bc1e5d7b8b6416191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Warda?= Date: Tue, 26 Apr 2022 19:14:35 +0200 Subject: [PATCH] Add missing specs and docs for skipping with different aliases --- docs/GlobalAPI.md | 76 ++++++++++++++----- .../__snapshots__/testFailing.test.ts.snap | 49 +++++++++++- e2e/test-failing/__tests__/statuses.test.js | 14 +++- .../__tests__/worksWithOnlyMode.test.js | 41 +++++++++- packages/jest-circus/src/eventHandler.ts | 10 ++- 5 files changed, 159 insertions(+), 31 deletions(-) diff --git a/docs/GlobalAPI.md b/docs/GlobalAPI.md index ae227c5e4571..dc5d03c91416 100644 --- a/docs/GlobalAPI.md +++ b/docs/GlobalAPI.md @@ -731,6 +731,61 @@ test.each` }); ``` +### `test.failing(name, fn)` + +Also under the aliases: `it.failing(name, fn)` + +:::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. + +::: + +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)` + +Also under the aliases: `it.only.failing(name, fn)`, `fit.failing(name, fn)` + +:::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 specific failing test. + + +### `test.skip.failing(name, fn)` + +Also under the aliases: `it.skip.failing(name, fn)`, `xit.failing(name, fn)`, `xtest.failing(name, fn)` + +:::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 run specific failing test. + ### `test.only(name, fn, timeout)` Also under the aliases: `it.only(name, fn, timeout)`, and `fit(name, fn, timeout)` @@ -878,24 +933,3 @@ const add = (a, b) => a + b; test.todo('add should be associative'); ``` - -### `test.failing(name, fn)` - -Also under the aliases: `it.failing(name, fn)`, `xtest.failing(name, fn)`, `xit.failing(name, fn)`, `it.skip.failing(name, fn)`, `it.only.failing(name, fn)` - -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. - -_Note_: 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. - -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 -}); -``` - diff --git a/e2e/__tests__/__snapshots__/testFailing.test.ts.snap b/e2e/__tests__/__snapshots__/testFailing.test.ts.snap index 271aee186a03..3d18915de0d5 100644 --- a/e2e/__tests__/__snapshots__/testFailing.test.ts.snap +++ b/e2e/__tests__/__snapshots__/testFailing.test.ts.snap @@ -5,10 +5,13 @@ exports[`works with all statuses 1`] = ` ✓ passes ✕ fails ✓ failing failes = passes + ✓ failing failes = 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 @@ -37,15 +40,53 @@ exports[`works with only mode 1`] = ` "FAIL __tests__/worksWithOnlyMode.test.js block with only, should pass ✓ failing failes = passes, should pass - ○ skipped failing test - ○ skipped passing test + ○ skipped failing test but skipped + ○ skipped passing test but skipped block with only, should fail ✕ failing passes = fails, should fail - ○ skipped failing test - ○ skipped passing test + ○ 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. + + + + ● 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. + + + + ● 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." `; diff --git a/e2e/test-failing/__tests__/statuses.test.js b/e2e/test-failing/__tests__/statuses.test.js index ac6c8489494d..d197f14637be 100644 --- a/e2e/test-failing/__tests__/statuses.test.js +++ b/e2e/test-failing/__tests__/statuses.test.js @@ -23,14 +23,26 @@ it.failing('failing failes = passes', () => { expect(10).toBe(101); }); +test.failing('failing failes = passes with test syntax', () => { + expect(10).toBe(101); +}); + it.skip.failing('skipped failing 1', () => { expect(10).toBe(10); }); -it.skip.failing('skipped failing 2', () => { +test.skip.failing('skipped failing 2', () => { expect(10).toBe(101); }); it.failing('failing passes = fails', () => { expect(10).toBe(10); }); + +xit.failing('skipped failing with different syntax', () => { + expect(10).toBe(10); +}); + +xtest.failing('skipped failing with another different syntax', () => { + expect(10).toBe(10); +}); diff --git a/e2e/test-failing/__tests__/worksWithOnlyMode.test.js b/e2e/test-failing/__tests__/worksWithOnlyMode.test.js index adfb5b43521d..4f41a13ed838 100644 --- a/e2e/test-failing/__tests__/worksWithOnlyMode.test.js +++ b/e2e/test-failing/__tests__/worksWithOnlyMode.test.js @@ -10,11 +10,11 @@ describe('block with only, should pass', () => { expect(10).toBe(101); }); - it('failing test', () => { + it('failing test but skipped', () => { expect(10).toBe(101); }); - it('passing test', () => { + it('passing test but skipped', () => { expect(10).toBe(10); }); }); @@ -24,11 +24,44 @@ describe('block with only, should fail', () => { expect(10).toBe(10); }); - it('failing test', () => { + it('failing test but skipped', () => { expect(10).toBe(101); }); - it('passing test', () => { + it('passing test but skipped', () => { + expect(10).toBe(10); + }); +}); + +describe('block with only in other it, should skip', () => { + it.failing('failing passes = fails, should fail but skipped', () => { + expect(10).toBe(10); + }); + + // eslint-disable-next-line jest/no-focused-tests + it.only('failing test', () => { + expect(10).toBe(101); + }); + + it('passing test but skipped', () => { + expect(10).toBe(10); + }); +}); + +describe('block with only with different syntax, should fail', () => { + fit.failing('failing passes = fails, should fail 1', () => { + expect(10).toBe(10); + }); + + test.only.failing('failing passes = fails, should fail 2', () => { + expect(10).toBe(10); + }); + + it('failing test but skipped', () => { + expect(10).toBe(101); + }); + + it('passing test but skipped', () => { expect(10).toBe(10); }); }); diff --git a/packages/jest-circus/src/eventHandler.ts b/packages/jest-circus/src/eventHandler.ts index 38aee07a563e..904ebd383d7a 100644 --- a/packages/jest-circus/src/eventHandler.ts +++ b/packages/jest-circus/src/eventHandler.ts @@ -122,7 +122,15 @@ const eventHandler: Circus.EventHandler = (event, state) => { } case 'add_test': { const {currentDescribeBlock, currentlyRunningTest, hasStarted} = state; - const {asyncError, fn, mode, testName: name, timeout, concurrent, failing} = event; + const { + asyncError, + fn, + mode, + testName: name, + timeout, + concurrent, + failing, + } = event; if (currentlyRunningTest) { currentlyRunningTest.errors.push(