如何使用 Jest 测试单个文件? [英] How do I test a single file using Jest?

查看:42
本文介绍了如何使用 Jest 测试单个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 Jest 测试多个文件,但我不知道如何测试单个文件.

我有:

  • 运行 npm install jest-cli --save-dev
  • 更新了 package.json: `{ ... "scripts": { "test": "jest";} ... }
  • 编写了大量测试.

运行 npm test 按预期工作(目前它运行 14 个测试).

我如何测试单个文件,例如测试 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'

解决方案

至少从 2019 年开始:

npm test -- SomeTestFileToRun

2015 年:

为了运行特定的测试,您需要使用 jest 命令.npm test 将不起作用.要直接在命令行上访问 jest,请通过 npm i -g jest-cliyarn global add jest-cli 安装.>

然后只需使用 jest bar.spec.js 运行您的特定测试.

注意:您不必输入测试文件的完整路径.该参数被解释为正则表达式.唯一标识文件的完整路径的任何部分就足够了.

I am able to test multiple files using Jest, but I cannot figure out how to test a single file.

I have:

  • Run npm install jest-cli --save-dev
  • Updated package.json: `{ ... "scripts": { "test": "jest" } ... }
  • Written a number of tests.

Running npm test works as expected (currently it runs 14 tests).

How do I test a single file, e.g. test app/foo/__tests__/bar.spec.js?

I have tried running npm test app/foo/__tests__/bar.spec.js (from the project root), but I get the following error:

npm ERR! Error: ENOENT, open '/node_modules/app/foo/tests/bar.spec.js/package.json'

解决方案

Since at least 2019:

npm test -- SomeTestFileToRun

In 2015:

In order to run a specific test, you'll need to use the jest command. npm test will not work. To access jest directly on the command line, install it via npm i -g jest-cli or yarn global add jest-cli.

Then simply run your specific test with jest bar.spec.js.

Note: You don't have to enter the full path to your test file. The argument is interpreted as a regular expression. Any part of the full path that uniquely identifies a file suffices.

这篇关于如何使用 Jest 测试单个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆