Jest: Jest fails with EBADF: bad file descriptor, close

Created on 25 Jan 2019  路  40Comments  路  Source: facebook/jest

馃悰 Bug Report

Jests fails multiple times with:

jest: failed to cache transform results in: C:/Users/Henning/AppData/Local/Temp/jest/jest-transform-cache-21ff455c7f0bd09e66d08b5df7fc2a40-e2c4083dbe718f612529eff10eba50fe/e3/error-guard_e3806b360161eb5615267fc2e1997a27
    Failure message: EBADF: bad file descriptor, close

at Object.closeSync (node_modules/graceful-fs/graceful-fs.js:52:27)

To Reproduce

Run react-native init, copy the file App.js in __Tests__ several times (more than 10 times).
Run yarn test.

Expected behavior

All tests pass.

Run npx envinfo --preset jest

  System:
    OS: Windows 10
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  Binaries:
    Yarn: 1.10.1 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD
Bug

Most helpful comment

I can confirm 23.6.0 is affected too and was fixed by using
npm resolutions

and
````
"resolutions": {
"write-file-atomic": "2.4.1"
}

````
in package.json

All 40 comments

What version of jest and what version of node?

PS C:\Users\Henning\Workspace\reactmain> yarn run jest --version
yarn run v1.10.1
$ C:\Users\Henning\Workspace\reactmain\node_modules\.bin\jest --version
24.0.0
Done in 0.34s.
PS C:\Users\Henning\Workspace\reactmain> node --version
v10.11.0

@rubennorte does RN support another jest than the bundled version?

I'm also seeing this error after upgrading. Was previously using [email protected] successfully. Post upgrade getting this cache error randomly.

With RN or plain Jest?

Plain jest.

abbreviated jest.config.js

{
    automock: true,
    cacheDirectory: '<rootDir>/.jest-cache',
    clearMocks: true,
    collectCoverageFrom: [...],
    coverageReporters: ['text-summary', 'html', 'clover'],
    coverageThreshold: {
        global: {
            statements: 85,
            branches: 80,
            functions: 72,
            lines: 85,
        },
    },
    moduleFileExtensions: ['tsx', 'ts', 'js', 'json'],
    moduleNameMapper: {
        'cv/(.*)': '<rootDir>/node_modules/content-viewer/dist/commonjs/$1',
    },
    setupTestFrameworkScriptFile: '<rootDir>/jestEnvironment.js',
    testEnvironment: 'node',
    testPathIgnorePatterns: ['_Auth', 'IntegrationSpec'],
    testRegex: 'Spec.tsx?$',
    timers: 'fake',
    transform: {
        '\\.(ts|tsx)$': '<rootDir>/src/testHelpers/transformTS.js',
        '\\.(css|svg|gif|png|jpg|jpeg)$': '<rootDir>/src/testHelpers/transformResource.js',
    },
    unmockedModulePathPatterns: [...],
}

Related to #4444 (last few comments there), seems like something in the upgrade from write-file-atomic 2.3.0 -> 2.4.2 broke something.

Can you try to either manually edit node_modules/write-file-atomic/index.js or use yarn resolutions to force 2.3.0 and see if that fixes your error?

Diff: https://github.com/npm/write-file-atomic/compare/v2.3.0...v2.4.2

Occurring with me too. Fails when >100 tests are run in a single run.
With this blocker Jest cannot be used along with some precommit hook on Windows

I've opened up #7725.
@pribilinskiy it would be awesome if you could test by installing [email protected] (jest comes with 2.4.2) locally and see if it fixes your issue. Either uses resolutions if you use yarn, or manually replacing in node_modules

I have

    "jest": {
      "version": "23.6.0",
    "write-file-atomic": {
      "version": "2.3.0",

checked both in package-lock.json and in package.json-s in node_modules

btw, there's no string match for write-file-atomic in node_modules/jest

It's part of jest-runtime. Easiest is to run npm ls write-file-atomic and see what it spits out

But if you're on Jest 23 (and on [email protected]), it's not the same issue (I think).

I see, well jest-runtime uses write-file-atomic directly from node_modules/.
What else can trigger this error

It's not the same as we're seeing on our own CI, or the user above here

I'm also seeing this error after upgrading. Was previously using [email protected] successfully. Post upgrade getting this cache error randomly.

In your case, I'd follow #4444

Same problem once upgrading to jest 24, windows 10. I've downgraded write-file-atomic to 2.4.1 with manual copying of an old version and it stopped having errors. re installing and the error comes back. i cleared my cache before each test.

@SimenB Any info when a new version with this fix will be released?

hopefully today

I encountered this problem today on our large scale project. We use locked 23.6.0 jest version. Could the fix be also applied to 23.x versions?

Could the fix be also applied to 23.x versions?

No, not unless absolutely necessary. And we have don't really have a fix. The "fix" is to rollback write-file-atomic, which you can do yourself with a lockfile or yarn's resolutions. 2.4.2 is the one with the error, so use 2.3.0 (what jest 23 came with) or 2.4.0/2.4.1

We encountered this issue today as well. Did you manage to fix this yesterday?

this was happening with jest 23.6 for my team as well. I went ahead and added "write-file-atomic": "2.4.1" to resolutions in my package.json:
"resolutions": { "write-file-atomic": "2.4.1" },

Could the fix be also applied to 23.x versions?

No, not unless absolutely necessary. And we have don't really have a fix. The "fix" is to rollback write-file-atomic, which you can do yourself with a lockfile or yarn's resolutions. 2.4.2 is the one with the error, so use 2.3.0 (what jest 23 came with) or 2.4.0/2.4.1

Thank you, this was helpful 馃槃!

I can confirm 23.6.0 is affected too and was fixed by using
npm resolutions

and
````
"resolutions": {
"write-file-atomic": "2.4.1"
}

````
in package.json

Feels a bit dirty using resolutions.. do we know when a 24.0.1 or 24.1 will be out?

i get

npm ERR! code ETARGET
npm ERR! notarget No matching version found for [email protected]

That's an issue either with your network or a proxy/registry of some kind. It's definitely available:

https://www.npmjs.com/package/jest
image

Yep. Definitely there. I鈥檒l make use of this tomorrow. Thank you

You linked to this very issue

The write-file-atomic resolution strategy does not appear to work with yarn workspaces 馃憥

Resolutions doesn't matter here, jest has locked down the dependency. If you've got issues, please open up a new issue with reproduction steps

That's great, just letting the next google-fu-master that finds this thread to avoid trying the workaround if they might be using yarn workspaces.

I encountered this problem today on our large scale project. We use locked 23.6.0 jest version. Could the fix be also applied to 23.x versions?

22.4 also has this issue

Any chance this fix to be merged to 23?

@tscislo not likely, we don't maintain past versions, only the current one. There would be too much maintenance burden and we just don't have enough people do deal with it.

What's holding you back from upgrading to v24?

@thymikee well we need to migrate sooner or later, but having a fix for 23 would allow us to keep it for some time now.
Especially as the fix on Jest side is easy you just need to change dependency in package.json in
jest-runtime from "write-file-atomic": "^2.1.0", to "write-file-atomic": "=2.4.1"

fix on Jest side is easy

Well, it isn't as easy as you'd think. We need a FB employee to release a version, which may be hard to arrange at times. Feel free to ping @scotthovestadt and maybe he'll do something about it :).

Oh, and btw, if you use Yarn, feel free to use the "resolutions" field in package.json to get this done, or use the patch-package project 馃檪

In case it helps anyone - I had forgotten to npm install and received this message. Installing fixed it for me.

We have a project using Yarn workspaces, with 5 TypeScript packages. Jest and ts-jest installed at the root.
Tests ran ok locally but kept getting this error in our pipeline. Can confirm that adding

"resolutions": {
    "write-file-atomic": "2.4.1"
}

to our package.json got around the issue.

"@types/jest": "25.1.4",
"jest": "25.2.3",
"ts-jest": "25.2.1",
Was this page helpful?
0 / 5 - 0 ratings