admin管理员组

文章数量:1401618

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

> [email protected] test
> jest "helloWorld.spec.js"

'jest' n’est pas reconnu en tant que mande interne
ou externe, un programme exécutable ou un fichier de mandes.
// in english jest isn't recognized as an internal mand or external

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

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

> [email protected] test
> jest "helloWorld.spec.js"

'jest' n’est pas reconnu en tant que mande interne
ou externe, un programme exécutable ou un fichier de mandes.
// in english jest isn't recognized as an internal mand or external

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

Share Improve this question asked Feb 14, 2022 at 21:42 Rafik BouloudeneRafik Bouloudene 6356 silver badges14 bronze badges 1
  • Have you run npm install? Npm learns what to do when you run npm test from package.json (the scripts section). Also in the package.json file are listed the project dependencies (Jest is one of them, it seems) and npm install installs the project dependencies. – axiac Commented Apr 11, 2023 at 7:32
Add a ment  | 

2 Answers 2

Reset to default 5

Choose one of the following methods


1) Install globally

You need to install jest globally:

npm install jest -g

Note: You will have to call it as jest something.spec.js in your cli or specify a test mand 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:

{
  "name": "my-project",
  "jest": {
    "verbose": true
  }
}

Or through JavaScript:

// Sync object
/** @type {import('@jest/types').Config.InitialOptions} */
const config = {
  verbose: true,
};

module.exports = config;

// Or async function
module.exports = async () => {
  return {
    verbose: true,
  };
};

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

import type {Config} from '@jest/types';

// Sync object
const config: Config.InitialOptions = {
  verbose: true,
};
export default config;

// Or async function
export default async (): Promise<Config.InitialOptions> => {
  return {
    verbose: true,
  };
};

When using the --config option, the JSON file must not contain a "jest" key:

{
  "bail": 1,
  "verbose": true
}

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

you could add jest scripts on package.json

or else

you can use npx to access jest mands directly from the terminal i.e npx jest

本文标签: javascriptJest command not recognizedStack Overflow