Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent "test/expect/etc is not defined" errors when using Jest

Facebook's Jest testing framework is easy to get started with, but the documentation overlooks an annoying aspect: test statements will be highlighted as errors by any editor that tries to warn of undefined symbols, because test, expect, and all matcher methods are not defined.

WebStorm highlights errors in Jest test file

Similary, attempting to run a test file with node directly will fail with ReferenceError: test is not defined.

What require/import statement(s) need to be added for those errors to go away?

like image 602
Dan Dascalescu Avatar asked Jun 11 '17 00:06

Dan Dascalescu


People also ask

How do you stop a Jest test?

By default, Jest runs all your tests and shows their result in the console. Yet, Jest comes with a bail option allowing you to stop running tests after n failed tests. You may configure the bail option to a boolean value or a number. Using a boolean for bail will either stop or not stop after the first failed test.

Which of the following are features of the Jest testing framework?

Jest is a JavaScript testing framework designed to ensure correctness of any JavaScript codebase. It allows you to write tests with an approachable, familiar and feature-rich API that gives you results quickly. Jest is well-documented, requires little configuration and can be extended to match your requirements.

What can Jest reliably run tests in?

To speed-up your tests, Jest can run them in parallel. By default, Jest will parallelise tests that are in different files. IMPORTANT: Paralellising tests mean using different threads to run test-cases simultaneously. Figure 2.


1 Answers

Node

If you want to run them directly through node, try to require jest and/or jest-runtime. Also give @types/jest a try as well.

Check Edit 2 for new info about this

Edit

@types/jest (jest-DefinitelyTyped) is definitely needed (or just one solution). If you install it (e.g., dev dependency), the IDE errors should go away. I just tried it on Webstorm, and it works.

Edit 2

The new Jest@20 Matchers (e.g., .resolves and .rejects) are still not defined in @types/jest. You can keep track of its status on the links below: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/16645 https://github.com/DefinitelyTyped/DefinitelyTyped/issues/16803

It should be available soon, though!

Also, it doesn't seem possible to run it directly through node. Last night I tried a bunch of different things, but using jest is the way to go - it really uses node under the hood, so I thought it would be possible as well. @thymikee over your opened issue at GitHub made clear that it's not.

Edit 3

The new release (20.0.1) includes the newest Jest definitions.

Lint

this isn't in the scope of this specific problem, but it also helps

Are you using something like ESLint? If so, you'll need eslint-plugin-jest

Following the steps described in this page: https://www.npmjs.com/package/eslint-plugin-jest, you will basically need to add it as an ESLint plugin and set jest globals in the ESLint configuration:

{   "env": {     "jest/globals": true   } } 

If you plan on supporting ES6 tests, you'll also need Babel and babel-jest plugin with the following jest configuration:

"transform": {   "^.+\\.js$": "babel-jest" } 

Finally, for Typescript tests you'd need the @types/jest and ts-jest packages as well

like image 118
Apidcloud Avatar answered Sep 19 '22 06:09

Apidcloud