jest: Jest transform cache breaks tests using native code
Do you want to request a feature or report a bug? Bug
What is the current behavior?
Tests pass on first run or with --no-cache
flag, otherwise one of them fails.
Repro See following issues for details:
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 19
- Comments: 21 (4 by maintainers)
Commits related to this issue
- fix issue with re-require()'ing native modules A summary of why this change was required is discussed in the following issues: https://github.com/facebook/jest/issues/3552 https://github.com/faceboo... — committed to jmurzy/nodegit by jmurzy 7 years ago
- fix issue with re-require()'ing native modules Discussion on why this change was required can be found in the following issues: https://github.com/facebook/jest/issues/3552 https://github.com/facebo... — committed to jmurzy/nodegit by jmurzy 7 years ago
- fix issue with re-require()'ing native modules Discussion on why this change was required can be found in the following issues: https://github.com/facebook/jest/issues/3552 https://github.com/facebo... — committed to jmurzy/nodegit by jmurzy 7 years ago
- fix issue with re-require()'ing native modules Discussion on why this change was required can be found in the following issues: https://github.com/facebook/jest/issues/3552 https://github.com/facebo... — committed to sorenlouv/nodegit by jmurzy 7 years ago
- Fix the tests failing on single core cpu due to facebook/jest#3552 — committed to SleepWalker/atom-hooks by SleepWalker 7 years ago
- Stub `oniguruma` as reloading native modules are not well supported in Jest Summary: Use jest's functionality for mocking modules to always return an empty object when `oniguruma` is required. This c... — committed to facebookarchive/nuclide by deleted user 6 years ago
- Stub `oniguruma` as reloading native modules are not well supported in Jest Summary: Use jest's functionality for mocking modules to always return an empty object when `oniguruma` is required. This c... — committed to facebookarchive/atom-ide-ui by deleted user 6 years ago
- đ€« Mock oniguruma for jest https://github.com/facebook/jest/issues/3552 — committed to MoOx/phenomic by MoOx 6 years ago
@alvinsight Looks like the issue with native modules is pretty much the same as #2826, nodejs/node#5016.
Also, when
Object.defineProperty()
is used to modify an existing property, it throws in watch mode unless you make itconfigurable
âas it is immutable by default. Iâm not sure what can be done in Jest to prevent thisâperhaps rewrite alldefineProperty
declarations asconfigurable
at test time using a babel transform. Not sure I like that though as it might cause unexpected side effects. Probably a better option is to add a global config for a blacklist of modules that should only be loaded once and only once. This would also âfixâ the issues with native modulesâat least the ones Iâm experiencing so far.@cpojer @thymikee Thoughts?
With Nodegit, the
--no-cache
workaround doesnât help. It works with--maxWorkers
with a value larger than the number of tests but this workaround reaches its limits easily.Is there any hope this will be resolved soon?
Another one important thing needed for
--no-cache
workaround to work. The number of workers must be > 1, because the tests will still fail in runInBand mode, or when they are run in CI env with single core. So the test should be run with at least--maxWorkers=2
.To anyone else who finds themselves with this issue, I ended up stubbing out
argon2-ffi
, which is the library I was using that in turn requiresref-napi
that in turn causes issues. My test case was simply asserting that stored passwords were hashed and that when retrieved they verified correctly, so the below âno-hash hashâ is fine in tests.Adding
--runInBand
also breaks tests with native modules. The errorso it seems that itâs impossible to debug tests with native.
Whatâs worse I need to run my tests serially and right now I canât do it because of that error. Does somebody know any workaround to do it?
I have a workaround that seems to work pretty well. It seems the issue only occurs when there are multiple test files that ultimately require the same problematic native module. Instead of letting Jest discover a bunch of files, you can give it a single entry point and require/import your files from there.
In my Jest config:
test/index.js:
You could even glob for them and
require
in a loop if you have a lot of tests. One disadvantage is that snapshots all go to the same place. You also obviously canât filter a test run by file path anymore. This bug is a huge bummer.Unable to use Jest reliably to test an Electron app with native modules because of this.
Jest 24.8.0 and this kind of bug:
Note that in my case,
no-cache
is not helping everytime. I am trying--maxWorkers=2 --no-cache
and now (with some updates in my code) itâs impossible to have the tests to pass, always this errorTo reproduce:
You should get something like this
You will see that errors come from
oniguruma.js
which have native modules. Anything more I can do to help? I am totally unfamiliar with jest codebase etc.