Jest: [Question] Is Jest a good fit for backend unit tests?

Created on 4 Aug 2016  ·  3Comments  ·  Source: facebook/jest

I've been loving Jest so far testing React components.

I've used/been using Mocha/Jasmine for testing node backends though, and I just wanted to ask if it's possible (and recommended) to use Jest for backend testing, since I could not find any indication of it in the docs (React, React-Native, Async, Webpack, i.e. no Node)

I just tried using it, and unfortunately I hit an instant roadblock. brcypt triggers a [SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random() warning and the test fails due to a runtime error (TypeError: Path must be a string. Received undefined)

Is it possible to use Jest for backend testing right now, or is it not part of the project's scope and something like Jasmine be a better fit?

Most helpful comment

What kind of backend testing are you trying to do? Jest itself uses Jest for tests and loads of libraries use Jest for testing. Can you provide a repository on github that I can use to npm install and npm test that shows your issue?

Two things I recommend: try setting "automock": false and "testEnvironment": "node". If your other tests rely on this, you can create a separate Jest config and run jest with --config=pathToConfig.json and use modulePathIgnorePatterns to ignore the frontend tests.

All 3 comments

What kind of backend testing are you trying to do? Jest itself uses Jest for tests and loads of libraries use Jest for testing. Can you provide a repository on github that I can use to npm install and npm test that shows your issue?

Two things I recommend: try setting "automock": false and "testEnvironment": "node". If your other tests rely on this, you can create a separate Jest config and run jest with --config=pathToConfig.json and use modulePathIgnorePatterns to ignore the frontend tests.

Thanks for the quick reply @cpojer!

Glad to hear Jest can be used with Node! I was thinking it would, since it's essentially a testing library and the API should just work, but felt the need to ask it especially since there's almost no resources online showing it being used with Node backends and mostly with react (or my Google-Fu just wasn't good enough).

I was already mocking my node_modules so that wasn't the case, but testEnvironment: node did the trick!

Cheers!

Yes, the default environment (jsdom) doesn't always lend itself well to node testing. Another benefit is that the node env will load 500ms faster (effectively instant) because jsdom is kinda slow.

Was this page helpful?
0 / 5 - 0 ratings