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

How to use Jest for Node.js projects #5122

Closed
ai opened this issue Dec 18, 2017 · 24 comments
Closed

How to use Jest for Node.js projects #5122

ai opened this issue Dec 18, 2017 · 24 comments

Comments

@ai
Copy link
Contributor

ai commented Dec 18, 2017

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?

@cpojer
Copy link
Member

cpojer commented Dec 18, 2017

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.

@cpojer cpojer closed this as completed Dec 18, 2017
@ai
Copy link
Contributor Author

ai commented Dec 18, 2017

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"?

@ai
Copy link
Contributor Author

ai commented Dec 18, 2017

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

@cpojer
Copy link
Member

cpojer commented Dec 18, 2017

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).

@thymikee
Copy link
Collaborator

@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.

@ai
Copy link
Contributor Author

ai commented Dec 18, 2017

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.

@ai
Copy link
Contributor Author

ai commented Dec 18, 2017

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?

@SimenB
Copy link
Member

SimenB commented Dec 18, 2017

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

@thymikee
Copy link
Collaborator

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

@ai
Copy link
Contributor Author

ai commented Dec 18, 2017

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?

@SimenB
Copy link
Member

SimenB commented Dec 18, 2017

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

@ai
Copy link
Contributor Author

ai commented Dec 19, 2017

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

@ai
Copy link
Contributor Author

ai commented Dec 19, 2017

I sent PR with ... fix #5134

@ai
Copy link
Contributor Author

ai commented Dec 20, 2017

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
Copy link
Member

SimenB commented Dec 20, 2017

@ai
Copy link
Contributor Author

ai commented Dec 20, 2017

@SimenB my bad 😓 PR was sent #5142

@ai
Copy link
Contributor Author

ai commented Dec 22, 2017

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

@cpojer
Copy link
Member

cpojer commented Dec 22, 2017

Thanks for pushing on this @ai!

@genintho
Copy link
Contributor

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.

@shawnmclean
Copy link

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

@thymikee
Copy link
Collaborator

@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.

@shawnmclean
Copy link

@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.

@SimenB
Copy link
Member

SimenB commented Apr 24, 2018

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

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants