Jest: How to use Jest for Node.js projects

Created on 18 Dec 2017  Â·  23Comments  Â·  Source: facebook/jest

TLTR: since Node.js 4 is still supported many companies use it and many Node.js projects (including PostCSS, Autoprefixer, Browserslist) must support it. What is a solution for them?

  1. Node.js 4 is not like Node.js 0.12. It is still supported and it is OK to still use it in production until April 2018.
  2. If you are developing popular Node.js library you must support all current Node.js version.
  3. If you support some version you should run tests for this Node.js version of Travis CI. It is the only way to be sure that you didn’t brake Node.js 4 support accidentally.

Of course, it is OK to run the only Node.js 9 on Travis CI for client-side projects like Jest. But Node.js projects is different.

What is your recommendation for the big Node.js project? Stick on Jest 21 until August 2018?

Most helpful comment

You'll have to ask the folks working on jsdom about that. Jest frequently segfaults on Node 4 which was fixed in later versions of Node. I generally don't recommend using Node 4 (for Jest) and would encourage you to push your company to upgrade to a later version which is about 2x faster, more stable and likely more secure.

Please note that it doesn't matter to this project which versions of Node.js are LTS. At Facebook, we only use the latest LTS release or unstable version. I'd even go so far as to drop support for Node 6 but I understand that's taking it a bit far for some folks, and it isn't causing major issues for us right now (like, segfaults).

All 23 comments

We do not have the time to support a version of Jest that is two versions behind the current LTS. Also, this decision was made for us by the jsdom team. There is no recommendation from us beyond using older versions of Jest. That said, if you'd like to make the latest version of Jest work with Node 4 and you are committed to supporting it until mid 2018, we are happy to accept pull requests and re-enable CI on Node 4.

That said, if you'd like to make the latest version of Jest work with Node 4 and you are committed to supporting it until mid 2018, we are happy to accept pull requests and re-enable CI on Node 4.

What was the problem of supporting Node.js 4? I can’t test it since I get yarn warning right now. It is about some very useful Node.js API or just using let without "use strict"?

Also, Node.js 4 is current LTS ;). All 4, 6 and 8 are LTS.

You'll have to ask the folks working on jsdom about that. Jest frequently segfaults on Node 4 which was fixed in later versions of Node. I generally don't recommend using Node 4 (for Jest) and would encourage you to push your company to upgrade to a later version which is about 2x faster, more stable and likely more secure.

Please note that it doesn't matter to this project which versions of Node.js are LTS. At Facebook, we only use the latest LTS release or unstable version. I'd even go so far as to drop support for Node 6 but I understand that's taking it a bit far for some folks, and it isn't causing major issues for us right now (like, segfaults).

@ai See their changelog regarding v10: https://github.com/tmpvar/jsdom/blob/master/Changelog.md#1000
Also, JSDOM v9 started becoming a burden, because of all sort of bugs, which needed to be patched in setup files.

I generally don't recommend using Node 4 (for Jest) and would encourage you to push your company to upgrade to a later version which is about 2x faster, more stable and likely more secure.

We don’t use Node.js 4 too. But PostCSS has 25 M downloads, so we have many Node.js 4 users, which I must support until Node.js 4 is still current LTS.

JSDOM v9 started becoming a burden, because of all sort of bugs, which needed to be patched in setup files.

The problem that PostCSS doesn’t even use JSDOM :(.

Can I install Jest 22 without JSDOM to use it for pure JS tests?

What was the problem of supporting Node.js 4?

Right now we've done nothing to remove node 4 support beyond upgrading to the latest version of jsdom in jest-environment-jsdom (which fixes bugs and add features "everybody" has to polyfill for older versions). There are still TODOs in the code marking removal when node 4 is dropped.

We might have some new issues as we haven't run node 4 on CI for a couple of months, but nothing major I wager.

I can’t test it since I get yarn warning right now.

YARN_IGNORE_ENGINES=true yarn.

It is about some very useful Node.js API or just using let without "use strict"?

Nope. That's more relevant when we wanna drop node 6 as we use a ton of async-await in the code base.

All 4, 6 and 8 are LTS.

True, only the latter two are _active_ LTS though, which I think counts for something.

Can I install Jest 22 without JSDOM to use it for pure JS tests?

If you use a lock file and ignore the warning from yarn about engines there should be no issue upgrading

You should be good to do it. Just use "testEnvironment": "node" in Jest config.

Awesome! Thanks for this advice. I run Jest on Node.js 4, but have this issue right now (destructor syntax?):

SyntaxError: Unexpected token ...
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at _load_normalize (/home/travis/build/postcss/postcss/node_modules/jest-config/build/index.js:14:73)
    at Object.<anonymous> (/home/travis/build/postcss/postcss/node_modules/jest-config/build/index.js:173:30)
    at Module._compile (module.js:409:26)

What do you think about some unofficial Node.js 4 support. You don’t use Node.js 4+ syntax, but this support is not official and may require some hacks?

What do you think about some unofficial Node.js 4 support. You don’t use Node.js 4+ syntax, but this support is not official and may require some hacks?

Something like that 🙂 I would definitely recommend a lockfile though so it doesn't change from under your feet.

PR welcome for you specific error here:
https://github.com/facebook/jest/blob/d1845116ebe818e6d945d50ad4857752758970ee/packages/jest-config/src/normalize.js#L284-L289 Just concat instead of spread syntax should work out fine.
Might be more places though, so I would check that it's enough before sending a PR so we can get them all in one swoop

I will send PR tomorrow :). And try to grep other ... symbols.

I fixed all ... in Jest repo, but now has this issue:

 FAIL  test/container.test.js
  â—Ź Test suite failed to run

    SyntaxError: Unexpected token ...

      at _load_jestJasmine (node_modules/jest-runner/build/run_test.js:146:355)

I look into jest-runner and there is no ... there. Seems like it is in some dependency and stacktrace is broken. Maybe somebody have a idea how find this dependency?

@SimenB my bad :sweat: PR was sent https://github.com/facebook/jest/pull/5142

Done. Now Jest works for Node.js 4 too. Thanks to everyone.

Thanks for pushing on this @ai!

This exchange and the changes made by @ai make me wonder if #4947 needs to be done or not?
Like if this todo gets fixed, https://github.com/facebook/jest/blob/master/packages/jest-message-util/src/index.js#L31 it should break again Node 4 support.

Is there something specific I need to do to run jest under node 4 environments?

@shawnmclean Node 4 gets it's end of life in 6 days, better use that time to migrate to Node 8 (latest LTS version) than figuring out your tools to work with soon unmaintained language.

@thymikee thats my issue now. We're migrating to Node 8, but during that time, we want to maintain backwards compatibility with 4, hence having tests running under Node 4 for the time being.

Jest 22 should work fine on node 4 as long as yiu use node env and not jsdom. Use jest 21 (or a custom jsdom env) if you need it

Was this page helpful?
0 / 5 - 0 ratings