Skip to content

Instantly share code, notes, and snippets.

@andreiashu
Created July 31, 2018 06:01
Show Gist options
  • Save andreiashu/aff9308c89f132a210bcea8893bb2e30 to your computer and use it in GitHub Desktop.
Save andreiashu/aff9308c89f132a210bcea8893bb2e30 to your computer and use it in GitHub Desktop.
Jest tests that helped me understand how different Jest settings behave
describe("Understand Jest framework", () => {
test("async works with Promise.resolve", async () => Promise.resolve(true))
test("async works with return", async () => true)
test("async works with expect met", async () => expect(true).toBe(true))
test("expect throws", async () => {
try {
expect(true).toBe(false)
console.log('This should not be printed')
} catch (err) {
// silence the error
}
})
test("async with resolving promise", async () => {
const result = await new Promise((resolve) => {
setTimeout(() => {
resolve(1)
}, 1)
})
expect(result).toBe(1)
})
test("this test should fail", async () => {
expect(true).toBe(false)
})
test("test not reached if using --bail and --inband. right?!", async () => {
expect("Should not be evaluated").toBe("Jest bug?")
})
})
describe("Understand Jest --bail + --inband param", () => {
test('How does --bail work?', async () => {
expect(1).toBe(1)
})
})
@andreiashu
Copy link
Author

With Jest 23.4.1 I get:

  Understand Jest framework
    ✓ async works with Promise.resolve (16ms)
    ✓ async works with return (3ms)
    ✓ async works with expect met (9ms)
    ✓ async works with expect failure (12ms)
    ✓ async works with resolving promise (15ms)
    ✕ this test should fail (17ms)
    ✕ test not reached if using --bail and --inband. right?! (15ms)
  Understand Jest --bail + --inband param
    ✓ How does --bail work? (11ms)

  ● Understand Jest framework › this test should fail

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

    Expected: false
    Received: true

      23 |
      24 |   test("this test should fail", async () => {
    > 25 |     expect(true).toBe(false)
         |                  ^
      26 |   })
      27 |
      28 |   test("test not reached if using --bail and --inband. right?!", async () => {

      at Object.test (api/__tests__/jest.test.ts:25:18)

  ● Understand Jest framework › test not reached if using --bail and --inband. right?!

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

    Expected: "Jest bug?"
    Received: "Should not be evaluated"

      27 |
      28 |   test("test not reached if using --bail and --inband. right?!", async () => {
    > 29 |     expect("Should not be evaluated").toBe("Jest bug?")
         |                                       ^
      30 |   })
      31 | })
      32 |

      at Object.test (api/__tests__/jest.test.ts:29:39)

Test Suites: 1 failed, 1 total
Tests:       2 failed, 6 passed, 8 total
Snapshots:   0 total
Time:        4.669s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment