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

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

问题描述

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

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

我有:

  • 运行npm install jest-cli --save-dev
  • 更新的package.json:`{..."scripts":{"test":"jest" } ...}
  • 进行了许多测试.
  • Run npm install jest-cli --save-dev
  • Updated package.json: `{ ... "scripts": { "test": "jest" } ... }
  • Written a number of tests.

运行npm test的效果正常(当前运行14个测试).

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

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

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

我尝试从项目根目录运行npm 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!错误:ENOENT,打开'/node_modules/app/foo/测试/bar.spec.js/package.json'

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

推荐答案

至少从2019年开始:

npm test -- SomeTestFileToRun

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

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.

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

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天全站免登陆