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

问题描述 投票:0回答:3

在使用 CRA v.1 创建的应用程序中,我需要运行特定的测试文件。我该怎么办?有

--testPathIgnorePatterns
标志可添加到
npm test
脚本中以忽略文件或路径,但如何从命令行使用 CRA 运行特定测试文件?

unit-testing create-react-app
3个回答
48
投票

我通过使用

--
将自定义参数传递给 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', () => { ... });
});

42
投票

您可以在命令中使用文件名,它只会运行它。

例如:

npm test src/App.test.js

0
投票

通过运行命令作为所提到的接受的答案,它将把它作为正则表达式,如果你有多个相同的文件名,如

index.test.tsx
,它将执行所有文件。

如果您使用

jest
,您可以运行下面的命令来强制它仅运行单个测试文件:

npm test --runTestsByPath "src/pages/parentpath/index.test.tsx"

参考这个

© www.soinside.com 2019 - 2024. All rights reserved.