Skip to content
Advertisement

Jest command not recognized

So i just started learning about Test Driven Developement and as an example i was asked to run the command npm test helloWorld.spec.js in the terminal but i got this error :

JavaScript

I’m working on windows and the only thing i have installed is node so what do i have to do?

Advertisement

Answer

Choose one of the following methods


1) Install globally

You need to install jest globally:

JavaScript

Note: You will have to call it as jest something.spec.js in your cli or specify a test command in your package.json.

2) Install locally

Install jest locally with npm install jest -D.

You can use a script in your package.json called test which would be "test": "jest".

  • If any of the above don’t work, try reinstalling jest.
  • If it still doesn’t work, try removing node_modules and npm cache clean --force and npm install

3) Config file

If you already have jest installed but it’s not working, you can use a config file to track files based on regex pattern (you can do a lot more if you check out the docs).

The following part is from the docs:

Jest’s configuration can be defined in the package.json file of your project, or through a jest.config.js, or jest.config.ts file or through the --config <path/to/file.js|ts|cjs|mjs|json> option. If you’d like to use your package.json to store Jest’s config, the "jest" key should be used on the top level so Jest will know how to find your settings:

JavaScript

Or through JavaScript:

JavaScript

Or through TypeScript (if ts-node is installed):

JavaScript

When using the –config option, the JSON file must not contain a “jest” key:

JavaScript

Regex options

testMatch [array]

(default: [ "**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ])

The glob patterns Jest uses to detect test files. By default it looks for .js, .jsx, .ts and .tsx files inside of __tests__ folders, as well as any files with a suffix of .test or .spec (e.g. Component.test.js or Component.spec.js). It will also find files called test.js or spec.js.

Note: Each glob pattern is applied in the order they are specified in the config. (For example [“!**/__fixtures__/**”, “**/__tests__/**/*.js”] will not exclude __fixtures__ because the negation is overwritten with the second pattern. In order to make the negated glob work in this example it has to come after **/__tests__/**/*.js.)

testRegex [string | array]

Default: (/__tests__/.*|(\.|/)(test|spec))\.[jt]sx?$

The pattern or patterns Jest uses to detect test files. By default it looks for .js, .jsx, .ts and .tsx files inside of __tests__ folders, as well as any files with a suffix of .test or .spec (e.g. Component.test.js or Component.spec.js). It will also find files called test.js or spec.js. See also testMatch [array], but note that you cannot specify both options.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement