在测试运行期间隐藏编译警告

Jef*_*ege 4 reactjs jestjs visual-studio-code

我们正在 vscode 中构建一个 React 应用程序,使用 jest 来运行单元测试。

\n\n

当我们运行测试时,我们会看到 node_modules 中文件的编译器警告。

\n\n
PASS  src/components/Common/IonicWrappers/__tests__/KorSelect.test.tsx\nPASS  src/components/Common/IonicWrappers/__tests__/KorHeader.test.tsx\n\xe2\x97\x8f Console\n\nconsole.warn node_modules/react-dom/cjs/react-dom.development.js:11494\nWarning: componentWillReceiveProps has been renamed, and is not recommended for use.\n
Run Code Online (Sandbox Code Playgroud)\n\n

我们使用 运行测试npm test,其中 package,json 包含:

\n\n
"scripts": {\n    "start": "react-scripts start",\n    "build": "react-scripts build",\n    "test-react": "react-scripts test --transformIgnorePatterns \\"node_modules/(?!(@ionic|ionicons|capacitor-data-storage-sqlite))\\"",\n    "test": "npm run test-react",\n    [...]\n},\n
Run Code Online (Sandbox Code Playgroud)\n\n

.vscode/settings.json 是:

\n\n
{\n    "jest.pathToJest": "--runInBand --no-cache --watchAll=false --transformIgnorePatterns=\'node_modules/(?!(@ionic|ionicons|capacitor-data-storage-sqlite))\'",\n    "jest.runAllTestsFirst": false,\n    "jest.enableInlineErrorMessages": false,\n    "jest.pathToConfig": "./node_modules/.bin/react-scripts",\n    "jestrunner.jestCommand": "npm test --runInBand --no-cache --watchAll=false --transformIgnorePatterns=\'node_modules/(?!(@ionic|ionicons|capacitor-data-storage-sqlite))\'"\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在这些警告很有价值,我们确实需要知道node_modules中有我们可能想要考虑更新的包,但我不需要在每次测试运行时都看到它们。

\n\n

而且它们使得很难看到测试的实际结果。

\n\n

无论如何,有什么配置可以抑制它们吗?

\n

Jef*_*ege 7

https://jestjs.io/docs/en/cli#silent

- 沉默的

防止测试通过控制台打印消息。