Skip to content

auto cleanup will break down testing in mocha & esm environment #614

Closed
liquity/dev
#356
@whodoeshua

Description

@whodoeshua
  • @testing-library/react version: 9.5.0
  • react version: ^16.12.0
  • node version: 12.13.0
  • mocha version: ^6.2.2,
  • mochawesome version: ^4.1.0,
  • esm version: ^3.2.25
  • npm (or yarn) version: yarn@1.17.3

Relevant code or config:

Github demo as below:

https://github.com/whodoeshua/mocha-react-testing-library

package.json

  {
"scripts": {
    "test": "tsc --pretty && mocha --exit --trace-warnings -r source-map-support/register -r jsdom-global/register -r esm -c \"build/**/@(skip|test)-*.js\""
  },
}

What you did:

yarn test

What happened:

Global afterEach hook will beak down testing with timeout error, cause of

1) "after each" hook for "test":
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
      at listOnTimeout (internal/timers.js:531:17)
      at processTimers (internal/timers.js:475:7)

Reproduction:

https://github.com/whodoeshua/mocha-react-testing-library

Problem description:

This problem is cause of auto cleanup. Auto clean up is an async funtion and required timers module to make a fake timer. But require function will throw out a error once esm module has been required. And the async cleanup will never stop.

Auto cleanup will call flush-microtasks.

// @testing-library/react/src/index.js:9
if (typeof afterEach === 'function' && !process.env.RTL_SKIP_AUTO_CLEANUP) {
  afterEach(async () => {
    await flush()
    cleanup()
  })
}

require function in flush-microtasks will throw out an error.

// @testing-library/react/src/flush-microtasks.js:18
  enqueueTask = nodeRequire('timers').setImmediate

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions