如何使用Create React App运行特定测试

时间:2019-06-01 10:53:13

标签: unit-testing create-react-app

在使用CRA v.1创建的应用中,我需要运行特定的测试文件。我该怎么办?有--testPathIgnorePatterns标志可以添加到npm test脚本中以忽略文件或路径,但是如何从命令行使用CRA运行特定的测试文件?

2 个答案:

答案 0 :(得分:1)

您可以在命令中使用文件名,它将仅运行该文件。

例如:

npm test src/components/App.test.js

答案 1 :(得分:1)

我通过使用--将自定义参数传递给npm脚本来完成此操作。

有关此选项的文档可以在这里找到:https://docs.npmjs.com/cli/run-script

因此,要在create-react-app应用程序中运行一个测试,请运行以下命令:

npm run test -- -t 'test-name'

test-name是在jest中的describe函数中使用的值-

describe('test-name', () => {
  it('does something', () => { ... });
});
相关问题