Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JEST tests complete successfully but returns exit status 1 #9324

Open
muthukrishnanm opened this issue Dec 18, 2019 · 109 comments
Open

JEST tests complete successfully but returns exit status 1 #9324

muthukrishnanm opened this issue Dec 18, 2019 · 109 comments

Comments

@muthukrishnanm
Copy link

muthukrishnanm commented Dec 18, 2019

I run JEST tests using npm run test:jest and the corresponding entry in package.json is

"test:jest": "jest --config=./jest.config.js",

All tests run successfully, but returns with exit code 1

Test Suites: 1 skipped, 106 passed, 106 of 107 total
Tests:       10 skipped, 657 passed, 667 total
Snapshots:   14 files obsolete, 0 total
Time:        34.453s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @vc/toolchain@3.3.0 test:jest: `jest --config=./jest.config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @vc/toolchain@3.3.0 test:jest script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/.npm/_logs/2019-12-18T13_01_11_869Z-debug.log

10 silly lifecycle @vc/toolchain@3.3.0~test:jest: Args: [ '-c', 'jest --config=./jest.config.js' ]
11 silly lifecycle @vc/toolchain@3.3.0~test:jest: Returned: code: 1  signal: null
12 info lifecycle @vc/toolchain@3.3.0~test:jest: Failed to exec test:jest script
13 verbose stack Error: @vc/toolchain@3.3.0 test:jest: `jest --config=./jest.config.js`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at EventEmitter.emit (events.js:214:7)
13 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at ChildProcess.emit (events.js:214:7)
13 verbose stack     at maybeClose (internal/child_process.js:915:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
14 verbose pkgid @vc/toolchain@3.3.0
15 verbose cwd /workspace/vc.src/vco/src/server/node
16 verbose Linux 3.13.0-163-generic
17 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "test:jest"
18 verbose node v8.17.0
19 verbose npm  v6.13.4
20 error code ELIFECYCLE
21 error errno 1
22 error @vc/toolchain@3.3.0 test:jest: `jest --config=./jest.config.js`
22 error Exit status 1
23 error Failed at the @vc/toolchain@3.3.0 test:jest script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

I am not sure why the process exits with status code 1 even though all tests pass.

@thymikee
Copy link
Collaborator

Repro please, or it didn't happen

@muthukrishnanm
Copy link
Author

muthukrishnanm commented Dec 18, 2019

Hi @thymikee

This happens in our repo and I am not able to create a reproducible sample repo. But what I could say is that the following command runs successfully,

node /workspace/vc/vco/src/server/node/node_modules/.bin/jest --config=./jest.config.js

<Runs all the tests successfully >

Test Suites: 1 skipped, 106 passed, 106 of 107 total
Tests:       10 skipped, 657 passed, 667 total
Snapshots:   15 files obsolete, 0 total
Time:        24.668s, estimated 33s
Ran all test suites.

whereas when running JEST cases via npm is failing.

$ npm run test:jest

<Runs all the tests successfully >


Test Suites: 1 skipped, 106 passed, 106 of 107 total
Tests:       10 skipped, 657 passed, 667 total
Snapshots:   19 files obsolete, 0 total
Time:        25.982s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @velocloud/toolchain@3.3.0 test:jest: `jest --config=./jest.config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @velocloud/toolchain@3.3.0 test:jest script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/vagrant/.npm/_logs/2019-12-18T15_15_30_620Z-debug.log

jest.config.js

'use strict';

module.exports = {
    "projects": [
        {
            name: "vc-core",
            displayName: "Jest - VC services",
            bail: false,
            clearMocks: false,
            collectCoverage: false,
            collectCoverageFrom: [
                '**/lib/sample/getSample.js'
                
            ],
            coverageDirectory: "/test-output/coverage",
            coverageReporters: [
                "json",
                "text-summary",
                "cobertura"
            ],
            reporters: [
                "default",
                "./common/VcTestReporter",
                "jest-junit"
            ],
            setupFilesAfterEnv: ["./common/setup.js"],
            testEnvironment: "./common/VcTestEnv.js",
            testMatch: [
                "**/__tests__/**/*.js"
            ],
            testPathIgnorePatterns: [
                "__tests__/aws.js",
               
            ],
            verbose: true,
        }
    ]
};

@thymikee
Copy link
Collaborator

Can you try with yarn as well then? Maybe it's an issue with npm itself, try upgrading Node

@muthukrishnanm
Copy link
Author

I did a little more debugging and I could see that the exit code of the JEST execution (even when not run in npm context) is 1.


$node /workspace/vc/vco/src/server/node/node_modules/.bin/jest --config=./jest.config.js

<Runs all the tests successfully >

Test Suites: 1 skipped, 106 passed, 106 of 107 total
Tests:       10 skipped, 657 passed, 667 total
Snapshots:   15 files obsolete, 0 total
Time:        24.668s, estimated 33s
Ran all test suites.


$ echo $?
1

@thymikee
Copy link
Collaborator

Anyway, without a repro there's nothing I can do. Please make sure you're on the latest version and try to come up with a minimal case by filtering out some tests until you get a correct exit code.

@mikkotikkanen
Copy link

mikkotikkanen commented Jan 26, 2020

I'm running into same issue, test passess successfully but jest returns status code 1 and npm errors.

PASS  src/test/restart.test.ts
  √ restart (3438ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        5.298s, estimated 6s
npm ERR! code ELIFECYCLE
npm ERR! errno 1

Not sure if it's related but I'm using TypeScript and doing a async test with child processes and this seems so far to be the only case where I've encountered such behaviour. Though there seems to be no difference whether I'm running with ts-jest transform or directly against built js files. Oh and everything is updated to latest versions.

@mikkotikkanen
Copy link

mikkotikkanen commented Jan 27, 2020

As an update to this, it also seems to fail with Yarn

 PASS  src/test/restart.test.ts
  √ restart (711ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.478s
Ran all test suites.
error Command failed with exit code 1.

Same thing happens to me as for OP, if I run Jest directly from node_modules/.bin/jest it run through successfully but when ran as npm script it returns exit code 1. Happens with both v24.x and v25.x.

@janaroj
Copy link

janaroj commented Mar 24, 2020

Same issue here, any solutions?

Test Suites: 1 passed, 1 total
--
24-Mar-2020 18:09:46 | Tests:       4 passed, 4 total
24-Mar-2020 18:09:46 | Snapshots:   1 obsolete, 1 passed, 1 total
24-Mar-2020 18:09:46 | Time:        4.2s
24-Mar-2020 18:09:46 | Ran all test suites.
24-Mar-2020 18:09:46 | npm ERR! code ELIFECYCLE
24-Mar-2020 18:09:46 | npm ERR! errno 1
24-Mar-2020 18:09:46 | npm ERR! @app/front@ test:ci: `jest --ci`
24-Mar-2020 18:09:46 | npm ERR! Exit status 1

Edit: Upgrading node to latest version solved the issue for me

@jcampuza
Copy link

Getting the same here pretty frequently when running in CI. It is intermittent but frequent enough to have started slowing things down.

Test Suites: 2 skipped, 233 passed, 233 of 235 total
Tests:       25 skipped, 1009 passed, 1034 total
Snapshots:   1 passed, 1 total
Time:        721.58s
Ran all test suites.
error Command failed with exit code 1.
error Command failed with exit code 1.
error Command failed.
Exit code: 1

@frederic-bonjour
Copy link

frederic-bonjour commented Mar 29, 2020

Same issue here:

Test Suites: 14 passed, 14 total
Tests:       61 passed, 61 total
Snapshots:   1 passed, 1 total
Time:        22.589s
Ran all test suites.
error Command failed with exit code 1. 

Did you guys find a solution?
Thanks!

@rcbevans
Copy link

rcbevans commented Apr 1, 2020

I'm seeing the same issue and now CI is failing all buddy builds.

@nabilfreeman
Copy link

nabilfreeman commented Apr 4, 2020

Also getting this on our CI (Codeship):

Test Suites: 277 passed, 277 total
Tests:       1577 passed, 1577 total
Snapshots:   206 passed, 206 total
Time:        185.986s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! LeSalonApp@0.0.1 testNoCache: `jest --no-cache "--maxWorkers=1"`
npm ERR! Exit status 1

Here's the command being run:

jest --no-cache "--maxWorkers=1"

Jest 25.2.7

We are upgrading from React Native 59 to 61 which has brought a new Jest version with it.

I'm going to try downgrading back to Jest 24 and see if tests pass again.

@alexisponcet
Copy link

alexisponcet commented Apr 5, 2020

Same problem with yarn only v1.21.1 (jest v.25.2.3)

@fsantecchia
Copy link

fsantecchia commented Apr 7, 2020

i fixed it changing --runInBand to --maxWorkers=2 but i dont know the reason why it was failing.

Solved: i needed to add await to fireEvent like await fireEvent.press(button);

@ghost
Copy link

ghost commented Apr 10, 2020

I ran into this issue in two different ways while using ts-jest. I fixed both:

1.) I left a fit( in my test suite. This for whatever reason was claiming that I had a pending test (rather than skipped). This caused npm to exit with an error code as 1.
Solution: Removed the f that I accidentally left. This file was now fine and npm was exiting with exit code of 0.

2.) I was using a nyan cat jest reporter (because why not?), while also having type-specific errors.
Solution: I removed the nyan cat jest reporter and used the default reporter. This made the TypeScript related type errors show up. I specifically changed a tsconfig.json value (noImplicitAny: true) recently and had some errors but the nyan cat jest reporter was silencing these errors.

@nabilfreeman
Copy link

nabilfreeman commented Apr 13, 2020

Got it. I had a couple of these:

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.

which are probably down to bad mocks but I was just ignoring in previous versions of Jest.

I narrowed down all my tests to a specific file and despite only 10 or so tests passing that log would appear and the tests would still exit with error.

So I guess the tests should be failing because of this bad error, but maybe a specific jest error would be better than just exit 1!

p.s. it was actually due to an old function using Promise.resolve().then() that was leaking out. The test was bad. I refactored to async/await and made sure the test was also async

@MateusAndrade
Copy link

I was facing a similar issue using when I was running:

jest --silent --runInBand --testPathPattern=index.spec\\.tsx$

Which cause my tests to run, but the process was exiting with exit code 1. However, changing this to:

jest --silent --maxWorkers=2 --testPathPattern=index.spec\\.tsx$

i fixed it changing --runInBand to --maxWorkers=2 but i dont know the reason why it was failing.

Solved: i needed to add await to fireEvent like await fireEvent.press(button);

Also, possibly there is a misleading await on my code which can cause this problem. 🤔

@smith-xyz
Copy link

This was a face palm moment, I was digging around the tests and code trying to find some sign of why our CI test script was not running. Then it hit me. We are using snapshot tests and maybe the test suite could not run due to not being able to execute the snapshots. After adding -u to our npm run test:ci script, this resolved the issue.

I leave this here in case this saves someone some time in considering that maybe the script cannot execute the snapshots.

@thymikee
Copy link
Collaborator

thymikee commented May 4, 2020

@smith-xyz you should never run Jest with -u on CI. The generated snapshot may likely be wrong and your tests will still pass, giving you false sense of security.

@smith-xyz
Copy link

@thymikee I was just looking at that and thinking the same thing after posting that comment. Is it best to only use snapshot tests outside of CI?

@thymikee
Copy link
Collaborator

thymikee commented May 4, 2020

You can use snapshots on CI as well, but they need to be written to disk/source control. When Jest runs next time (whether on dev machine or CI) it will recreate the snapshot and compare to the one that's supposed to be physically in the file system. The reason your CI may fail is because the files are not there (e.g. they were not added to source control, hence missing on CI).

@corsik
Copy link

corsik commented Jun 4, 2020

I get the same error:
"jest": "^26.0.1",

Test Suites: 17 passed, 17 total
Tests:       57 passed, 57 total
Snapshots:   3 obsolete, 26 passed, 26 total
Time:        13.062 s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! test@1.0.0 test: `jest`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the test@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:

@picozzimichele
Copy link

I had the same problem, what worked for me was moving to an older build.

I used jest@20.0.4 rather than current version

yarn add jest@20.0.4

Let me know if this works!

@gbd77rc
Copy link

gbd77rc commented Jun 8, 2020

Yep downgrading to 20.0.4 worked for me as well. Current version worked fine from the command line, but when run via NPM I got the ELIFECYCLE error. There where no tests created yet.

jest config from package.json

"jest": {
        "moduleFileExtensions": [
            "js",
            "json",
            "ts"
        ],
        "rootDir": "src",
        "testRegex": ".spec.ts$",
        "transform": {
            "^.+\\.(t|j)s$": "ts-jest"
        },
        "coverageDirectory": "../coverage",
        "testEnvironment": "node"
    }

Node v14.3.0
NVM 0.35.3
TypeScript 3.9.3

@corsik
Copy link

corsik commented Jun 8, 2020

Snapshots: 3 obsolete, 26 passed, 26 total
obsolete - are the cause of the error, after their removal, the error is gone.

@sregg
Copy link

sregg commented Jun 14, 2020

I had a similar issue in my GH actions where all tests would pass but still exit 1.

Test Suites: 10 passed, 10 total
Tests:       38 passed, 38 total
Snapshots:   0 total
Time:        98.33s
Ran all test suites.
npm ERR! Test failed.  See above for more details.
##[error]Process completed with exit code 1.

It was all fine on my local machine.
The only thing I found that was different was the machine the test was running on in GH Actions.
So I changed my GH action runs-on from ubuntu-latest to macos-latest and no more exit code 1 🎉

@actuallymentor
Copy link

So I changed my GH action runs-on from ubuntu-latest to macos-latest and no more exit code 1 🎉

After 3 hours of messing around I ended up using this.

Anyway, without a repro there's nothing I can do

Happy to. Any criteria/preferred structure? I can make a repo with a repro if that suits you.

@brunolnetto
Copy link

Well, many languages have the expression:

Portuguese: "O que os olhos não vêem, o coração não sente."
Spanish: "Ojos que no ven, corazón que no siente"
French: "loin des yeux, loin du cœur"
English: "Out of sight, out of mind"
German: "Was ich nicht weiß, macht mich nicht heiß"
Russian: "С глаз долой, из сердца вон"

@iliakan
Copy link

iliakan commented Jan 1, 2023

If someone from jest team would like to take a deeper look, I can contribute and go deeper.

I'm glad this error doesn't bother you any more, @brunolnetto .

@brunolnetto
Copy link

I do not discard the hypothesis of mangled use case on jest library. My common debugging workflow is try to traceback working commits, understand the changes, reproduce locally, cut down accordingly to its dependencies and submit experimental commits. It worked for me sofar.

@adam-boosted
Copy link

@shousper fixed my problem with their comment

I was ignoring coverage from generated files, but there was a file being tested that included those other files. It spits out a more reasonable (thought not completely self-explanatory) error when you run it locally instead of in CI.

@AlexKorole
Copy link

AlexKorole commented Jan 19, 2023

Solution in my case:

  1. Find an error in the log: "Cannot log after tests are done. Did you forget to wait for something async in your test?"
  2. Find a test which produce the error and change "async await" logic to "Promise" and finish the test by calling "done()" command.

https://youtu.be/m-v_cXLGhEg
https://habr.com/ru/post/710342/](https://habr.com/ru/post/710342/

@Gideon-Felt
Copy link

I have this same issue after upgrading from jest 27.5.1 to 28.1.1
node 18.12.1

@Gideon-Felt
Copy link

My fix was by adding:
"coverageReporters": [ "text-summary", "lcov" ]
to my jest configuration

@brunolnetto
Copy link

My recommendation with Jest based on my experience is: keep it on essentials and don't make more hypothesis than you can test. The latest jest version has many features which allows clean and short test cases without entering the library organization itself.

@Leland
Copy link

Leland commented Apr 13, 2023

Just chiming in with my organization's own issue with this on a Create React App instance.

After upgrading to React 18.x, and upgrading Jest correspondingly, our test suite running on GitHub Actions began failing with a non-zero exit code despite passing all tests.

We couldn't reproduce this locally, and were unable to get to the bottom of what the issue even was after hours of investigation. It wasn't even cleanly reproducible in CI: it usually failed this way, but not always.

Given all of that, it might literally just be a GitHub Actions thing – but chiming in all the same.

image

@pavel-koryagin
Copy link

I've encountered the same variation as @shousper above #9324 (comment)

We have two options to fix it:

  • remove collectCoverageFrom as suggested by @shousper
  • add coverageProvider: 'v8' - if you cannot afford to remove collectCoverageFrom

@Rakesh-3388
Copy link

npm i --save-dev jest
npm ERR! code EJSONPARSE
npm ERR! file /home/coder/project/learn/jest-testing/package.json
npm ERR! JSON.parse Failed to parse json
npm ERR! JSON.parse Unexpected string in JSON at position 169 while parsing '{
npm ERR! JSON.parse "name": "jest-testing",
npm ERR! JSON.parse "version":'
npm ERR! JSON.parse Failed to parse package.json data.
npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.

npm ERR! A complete log of this run can be found in:
npm ERR! /home/coder/.npm/_logs/2023-05-27T13_01_50_769Z-debug.log

@pranshuchittora
Copy link

Faced the same issue on GHA. Change the runner from ubuntu-latest to macos-latest worked.
But I am afraid to use this fix in the long run, because of the 10x cost.

omarkohl added a commit to cleodora-forecasting/cleodora that referenced this issue Jun 13, 2023
Contrary to what I initially believed the issue does not seem to be in
the Footer.test.tsx but in App.test.tsx . The error message appears
after the Footer.test.tsx output and before the App.test.tsx output. The
App test includes all components but was not checking for the footer
content. My hypothesis is that when the test was done it tore down the
msw handlers making the fetch request for config.json made in the Footer
either abort or not be intercepted by msw. What set me on the right
track was the following issue, in particular this comment
jestjs/jest#9324 (comment) .

Before this fix the tests would fail about once every 10 to 40
executions. After this fix the tests failed due to #561 after about 320
and after about 60 executions. There were no more failures because of
this issue.
@furstenheim-goodnotes
Copy link

In my case the problem was that it was logging outside of the test suite. #10728

The message was logged but it was in the middle of all the execution so it was hard to see.

@shousper
Copy link

New cause discovered today: TypeScript compilation errors aren't reported by jest (through ts-jest I guess?) but will report an exit status code of 1, even if all tests pass 👍🏻

@agvglobant
Copy link

In my case, it was just related to code coverage falling under the defined thresholds in jest config. In our case, I just lowered those thresholds to the actual values and everything worked fine.

@vsheyanov
Copy link

In my case it was also doing something after an environments was torn down. Worth noticing - test itself is successful
image

But it is not clear that it is a problem since there is no message in the end with a Summary of all errors:
image

Solution - fix all instances where you perform actions after an environment is torn down. This finally allowed CircleCI to succeed in building the project.

@Drarig29
Copy link

If you are using Node.js 20.x, ensure nothing in your tested code is setting the process.exitCode property to non-zero.

This makes any version of Jest fail, even when all tests are passing. This is being tracked here: #14501

chandq added a commit to chandq/sculp-js that referenced this issue Nov 14, 2023
@CMCDragonkai
Copy link

#14501 got closed...? What's the work around here? We cannot set the exitCode? We need to do this because we want to let the program finish gracefully while setting a custom exit code.

@deltamualpha
Copy link

#14619 may fix this for a number of people who are hitting errors during the coverage step that are being silently swallowed today.

@gsplakmal
Copy link

gsplakmal commented Mar 19, 2024

Solution in my case:

  1. Find an error in the log: "Cannot log after tests are done. Did you forget to wait for something async in your test?"
  2. Find a test which produce the error and change "async await" logic to "Promise" and finish the test by calling "done()" command.

https://youtu.be/m-v_cXLGhEg [https://habr.com/ru/post/710342/](https://habr.com/ru/post/710342/](https://habr.com/ru/post/710342/%5D(https://habr.com/ru/post/710342/)

Thank you. This is the actual issue. Found some async methods called without awaiting and got the same error. Same test suite worked in older versions and got this error after upgrading node, jest, typeorm to latest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests