简体   繁体   English

Jest 命令无法识别

[英]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:所以我刚开始学习测试驱动开发,作为一个例子,我被要求在终端运行命令npm test helloWorld.spec.js但我得到了这个错误:

> javascript-exercises@1.0.0 test
> jest "helloWorld.spec.js"

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

I'm working on windows and the only thing i have installed is node so what do i have to do?我在 windows 上工作,我唯一安装的是节点,所以我该怎么办?

Choose one of the following methods选择以下方法之一


1) Install globally 1)全局安装

You need to install jest globally:你需要全局安装jest

npm install jest -g

Note: You will have to call it as jest something.spec.js in your cli or specify a test command in your package.json .注意:您必须在 cli 中将其称为jest something.spec.js或在package.json中指定test命令。

2) Install locally 2)本地安装

Install jest locally with npm install jest -D .使用 npm install jest npm install jest -D在本地安装 jest。

You can use a script in your package.json called test which would be "test": "jest" .您可以在package.json中使用名为testscript "test": "jest"

  • If any of the above don't work, try reinstalling jest .如果以上任何一项都不起作用,请尝试重新安装jest
  • If it still doesn't work, try removing node_modules and npm cache clean --force and npm install如果仍然不起作用,请尝试删除node_modulesnpm cache clean --forcenpm install

3) Config file 3)配置文件

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) .如果你已经安装了jest但它不工作,你可以使用一个配置文件来跟踪基于正则表达式模式的文件(如果你查看文档,你可以做更多)

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. Jest 的配置可以在项目的package.json文件中定义,或者通过jest.config.jsjest.config.ts文件或通过--config <path/to/file.js|ts|cjs|mjs|json>选项。 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:如果你想使用你的package.json来存储 Jest 的配置, "jest"键应该在顶层使用,这样 Jest 就会知道如何找到你的设置:

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

Or through JavaScript:或拨打 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):或者通过 TypeScript(如果安装了ts-node ):

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:使用 --config 选项时,JSON 文件不得包含“jest”键:

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

Regex options正则表达式选项

testMatch [array] testMatch [数组]

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

The glob patterns Jest uses to detect test files. Jest 用于检测测试文件的 glob 模式。 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 (eg Component.test.js or Component.spec.js ).默认情况下,它会在__tests__文件夹中查找.js.jsx.ts.tsx文件,以及任何后缀为.test.spec的文件(例如Component.test.jsComponent.spec.js ) . It will also find files called test.js or spec.js .它还会找到名为test.jsspec.js的文件。

Note: Each glob pattern is applied in the order they are specified in the config.注意:每个 glob 模式都按照它们在配置中指定的顺序应用。 (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.) (例如 [",**/__fixtures__/**"."**/__tests__/**/*.js"] 不会排除 __fixtures__ 因为否定被第二个模式覆盖。为了使否定的 glob在这个例子中工作它必须在 **/__tests__/**/*.js 之后。)

testRegex [string | testRegex [字符串 | array]大批]

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

The pattern or patterns Jest uses to detect test files. Jest 用于检测测试文件的模式。 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 (eg Component.test.js or Component.spec.js ).默认情况下,它会在\_\_tests\_\_文件夹中查找.js.jsx.ts.tsx文件,以及任何后缀为.test.spec的文件(例如Component.test.jsComponent.spec.js )。 It will also find files called test.js or spec.js .它还会找到名为test.jsspec.js的文件。 See also testMatch [array], but note that you cannot specify both options.另请参见testMatch [array],但请注意您不能同时指定这两个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM