在 npm create-react-app 之后立即运行测试非常慢

lib*_*bby 8 javascript reactjs jestjs create-react-app react-testing-library

紧接着npx create-react-app my-app,我进入my-app文件夹并执行CI=true npm test

结果输出是:

> react-scripts test

PASS src/App.test.js
  ? renders learn react link (39ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        4.465s
Ran all test suites.
Run Code Online (Sandbox Code Playgroud)

package.json将标志添加--env=jsdom到 后react-scripts test,我得到:

PASS src/App.test.js
  ? renders learn react link (42ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.026s, estimated 3s
Ran all test suites.
Run Code Online (Sandbox Code Playgroud)

我的期望是一切都将在不到 1 秒的时间内运行。如果我在测试中渲染 React 组件,我可以让它运行得比上面显示的更快吗?

跑步yarn并没有什么区别。

小智 0

在 gitpod 中运行它...它是一个适用于 vscode 的容器化电子应用程序,在浏览器中运行,与在本地计算机上运行 npm 命令相比,速度快得惊人,而且也比 github 代码空间快得多。

如果这不符合您的喜好,您可能可以运行 npmauditfix -f 和 Or


npm cache clear --force
npm cache rm --force
npm cache verify 

Run Code Online (Sandbox Code Playgroud)

您还可以附加 test.skip 以节省您当前未进行的测试的时间...

根据我的经验,速度变慢通常是由于某些依赖性怪异或您的计算机在一整天对 CPU 温度漠不关心后过热造成的。