Skip to content

Test with rejected Promise fails in Node, not in browser #468

Closed
@ghengeveld

Description

@ghengeveld

Relevant code

import React from "react"
import { render } from "@testing-library/react"

const Hello = () => {
  const promise = Promise.reject("Oh no!")
  console.log(promise)
  return <div>Hello RTL</div>
}

test("HelloRTL", async () => {
  const { findByText } = render(<Hello />)
  await findByText("Hello RTL")
})

The problem

Ran from the command line with yarn test --watchAll, this fails with the following output:

 FAIL  src/hello.spec.js
  ✕ HelloRTL (22ms)

  ● HelloRTL

    Oh no!



  console.log src/hello.spec.js:6
    Promise { <rejected> 'Oh no!' }

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.774s, estimated 1s
Ran all test suites.

Reproduction

  • Naturally I've setup a CodeSandbox.
  • As you can see the test actually passes in CodeSandbox, I suppose this is because it's run in the browser?
  • I've setup a repository here.
  • I've added the repo to Travis, where you can see it fail.
  • I've tried using plain Jest, where this issue doesn't appear.
  • I've tried without react-scripts, but the issue persists.
  • I've tried jest-environment-jsdom-fourteen, but the issue persists.
  • I was able to pin this down to v8.0.6. In v8.0.5 it works fine.

Expected outcome

I would expect the test to pass in both environments. There is nothing wrong with assigning a rejected Promise to a variable, as long as it doesn't get rendered. The React component works fine.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions