jest: [Bug]: jest isolation conflicts with better-sqlite3 native modules

Version

29.7.0

Steps to reproduce

Node modules like better-sqlite3 use native modules. Those native modules can create JavaScript error objects. The error class is set to the native module via:

https://github.com/WiseLibs/better-sqlite3/blob/67d69e5b3dc67539d42a2b3a06492246ff0b68fa/lib/database.js#L59

addon.setErrorConstructor(SqliteError);

The Jest isolation does not work on native modules, but only on the JS environment. That’s why the first Test sets the Error Constructor. The second test uses the same native module, and therefore uses the error constructor of the first test.

As the global error changed from test1 to test2, errors thrown in test2 are checked against the global error of node env of test 1, which always fails.

I’ve added a minimal sample to reproduce the issue. It includes two identical tests which only pass once. bs-jest-issue.zip

  • npm i
  • npm t

Expected behavior

All tests run successfully. Native modules shall also be isolated, so that each jest test run gets it’s own native module, to be able to store the current global error object of the current node env.

Actual behavior

The second test fails, because the error object of second test node env is not an instance of global error object of the first test node env. The isolation between node envs of test 1 and test 2 are violated, as native modules are shared between the two node envs in Jest setup.

Additional context

No response

Environment

System:
    OS: macOS 14.4.1
    CPU: (12) arm64 Apple M3 Pro
  Binaries:
    Node: 20.11.1 - ~/.nvm/versions/node/v20.11.1/bin/node
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.1/bin/npm
  npmPackages:
    jest: ^29.7.0 => 29.7.0

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 2
  • Comments: 29

Most upvoted comments

can i work on it?