1

Well, I have these scripts in my package.json for testing some code in NodeJS.

"scripts": {
    "pretest": "env NODE_ENV=test sequelize db:migrate",
    "test": "jest",
    "posttest": "env NODE_ENV=test sequelize db:migrate:undo:all"
}

When the tests go clear, the "posttest" runs, but when the tests fail, I receive a

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

in VS Code. There is nothing usefull on the link about the problem, neither on the internet.

So I found this link about NPM:

https://github.com/npm/npm/issues/5493

The guy said:

In the vast majority of cases, users are going to be unpleasantly surprised if posttest runs after test failures (you don't want your test environment being cleaned up or new versions being published if there were test failures, for instance). As such, this behavior isn't going to change. Putting something like "test":"npm run-script test-failing || npm run-script mandatory-cleanup" into your package.json will give you what you want.

This did not solve my problem. With more research I found this:

npm posttest doesn't trigger if npm test fails

The solutions did not work for me either.

So how can I run the "posttest" script even if the tests fail?

  • What exactly did not work for these solutions? This one is supposed to be applicable to your case and workable, https://stackoverflow.com/a/52073818/3731501 . – Estus Flask Jul 18 '20 at 16:52
  • Did you mean that `npm-run-all` is supposed to work? Because it did not. I tried all the possibilities that I could think of with it, but non of then worked. Is `--continue-on-error` an argument for NPM and work for Yarn too? – Kelvin de Miranda Barros Jul 18 '20 at 17:48
  • Yes, it is. --continue-on-error is essential in this case. It's npm-run-all arg, not NPM. npm-run-all supports Yarn as well. – Estus Flask Jul 18 '20 at 17:55
  • Well, I thought that `npm-run-all` was a native arg of npm, but it is a package. According with this page https://www.npmjs.com/package/npm-run-all there are the args `npm-run-all` and `run-s`, so I tried `"test": "npm-run-all jest posttest --continue-on-error"` and `"test": "run-s jest posttest --continue-on-error"` and in both I get `ERROR: Task not found: "jest" error Command failed with exit code 1.` – Kelvin de Miranda Barros Jul 18 '20 at 18:51
  • Do you have any ideas? – Kelvin de Miranda Barros Jul 18 '20 at 18:52
  • `jest` is not npm script so it fails. `test` and `posttest` need to be renamed, then it becomes `"test": "npm-run-all test-runme test-runafter --continue-on-error"`. This is what the answer in dupe question shows. – Estus Flask Jul 18 '20 at 19:04

1 Answers1

1

Well, with the conversation above, I got to this solution:

This is my scripts in package.json:

"scripts": {
    "dev": "nodemon src/server.js",
    "pretest": "env NODE_ENV=test sequelize db:migrate",
    "run-tests": "jest",
    "run-after-tests": "env NODE_ENV=test sequelize db:migrate:undo:all",
    "test": "npm-run-all run-tests run-after-tests --continue-on-error"
},

I installed the npm-run-all package and it runs the run-after-test script even if the tests fail. Now I get the errors

error Command failed with exit code 1.

from the test script, and

ERROR: "test" exited with 1.
error Command failed with exit code 1.

from run-after-test, but in the end my problem got solved. If someone has a better solution with no errors at the end of the scripts, please share with us.

  • It should be the opposite, custom `run-tests` script for `jest` and built-in `test` for npm-run-all. Otherwise `pretest` won't be matched with a cleanup automatically. The point of doing this with npm-run-all is to be able to get an error if it fails, otherwise shell `;` could be used. But it shouldn't exit with an error all the time, this means that either Jest or a clean-up failed. You can debug them separately but it seems like it's Jest. – Estus Flask Jul 18 '20 at 20:33
  • I will have to take some more time to understand the right way, but if you can fix the code, feel free to. – Kelvin de Miranda Barros Jul 19 '20 at 01:03
  • Should be `"run-tests": "jest", "run-after-test": "env NODE_ENV=test sequelize db:migrate:undo:all", "test": "npm-run-all run-tests run-after-test --continue-on-error"`. – Estus Flask Jul 19 '20 at 10:07
  • is there an alternative for yarn? Thanks! – Berci Apr 27 '21 at 10:17
  • Later edit: nevermind. Seems like `yarn-run-all` exists, but in the package.json script you run it with `npm-run-all` . Really weird interaction, but it does the job. – Berci Apr 27 '21 at 10:45