我能够使用Jest测试多个文件,但我不知道如何测试单个文件。

我有:

执行npm install jest-cli——save-dev命令 更新包。Json: '{…"scripts": {"test": "jest"}…} 编写了许多测试。

运行npm测试可以正常工作(目前运行14个测试)。

如何测试单个文件,例如test app/foo/__tests__/bar.spec.js?

我已经尝试运行npm test app/foo/__tests__/bar.spec.js(从项目根),但我得到以下错误:

npm犯错!错误:ENOENT,打开'/node_modules/app/foo/tests/bar.spec.js/package.json'


当前回答

一个简单可行的解决方案:

yarn test -g文件名或

npm test -g fileName

例子:

yarn test -g cancelTransaction或

取消事务

关于测试过滤器的更多信息:

Test Filters
--fgrep, -f Only run tests containing this string [string]
--grep, -g Only run tests matching this string or regexp [string]
--invert, -i Inverts --grep and --fgrep matches [boolean]

其他回答

在特定文件中运行特定测试:

yarn test -f "partial-filename" -t "as split node"

npm测试,或笑话可以取代纱线测试,这取决于你喜欢的JS捆绑器。

这将只尝试在包含some-partial-filename的文件中查找测试,并且在这些文件中,测试将需要有一个describe或it指令,例如提到“作为分割节点”

// In cool-partial-filename.js
describe("with a less indented sibling", () => {
  it("should create a new check-list-item with the same indent as split node", () => {
    console.log("This would run with the invocation above");
  })
})

我只是将Jest安装为全局,运行Jest myFileToTest.spec.js,它工作了。

我们在Angular中使用了nrwl-nx。在这种情况下,我们可以使用这个命令:

npm test <ng-project> -- --testFile "file-name-part"

注:

npm test will run the test script specified in package.json: "test": "ng test" --: tells npm to pass the following parameters to the test script (instead of consuming them) Thus the rest of the cmd will be passed to ng test <ng-project> is the name of a project in angular.json when you omit this parameter, the "defaultProject" (specified in angular.json) will be used (so you must specify it, when the test is not in your default project) Next we must check which builder is used: In angular.json navigate to "project" - "<ng-project>" - "architect" - "test" and check the "builder", which in our case is: "@nrwl/jest:jest" Now that we know the builder, we need to find the available cmd-line parameters On the command line, run npm test <ng-project> -- --help to see all available options Or check the online documentation One of the options is --testFile which is used here

这就是如何在不重新启动测试的情况下动态地运行特定文件上的测试。

我的React项目创建为create-react-app。

所以它会监视测试的变化,当我做出改变时自动运行测试。

这就是我在终端中看到的测试结果:

Test Suites: 16 passed, 16 total
Tests:       98 passed, 98 total
Snapshots:   0 total
Time:        5.048s
Ran all test suites.

Watch Usage: Press w to show more.

按W

Watch Usage
 › Press f to run only failed tests.
 › Press o to only run tests related to changed files.
 › Press q to quit watch mode.
 › Press p to filter by a filename regex pattern.
 › Press t to filter by a test name regex pattern.
 › Press Enter to trigger a test run.

然后按P

Pattern Mode Usage
 › Press Esc to exit pattern mode.
 › Press Enter to filter by a filenames regex pattern.

 pattern ›

 Start typing to filter by a filename regex pattern.

这是在我想运行'Login'文件夹中的'index.es6.js'文件之后:

Pattern Mode Usage
 › Press Esc to exit pattern mode.
 › Press Enter to filter by a filenames regex pattern.

 pattern › login/index

 Pattern matches 1 file
 › src/containers/Login/index.es6.test.js

这就是我在特定文件上运行测试的方法。

你有两个选择:

Option 1: Command line. You can run the following command node '/Users/complete-path-to-you-project/your-project/node_modules/.bin/jest' '/Users/complete-path-to-you-project/your-project/path-to-your-file-within-the-project/your-file.spec.ts' This avoids you to install Jest globally. You use the jest used by your project. Option 2: If you are using Visual Studio Code you have a great plugin to do this: Jest Runner. It allows you not only to run tests file by file, but even specific suites and specs just by a right click on the tests you want to run.