yarn测试(jest)没有发现create-rect应用程序的测试



我对JavaScript中的自动化测试相当陌生,希望在我的工作中包括Jest。

我在create-rect应用程序中遵循了一个简单的Jest测试指南。

当我尝试用纱线测试运行我的测试时,它在观察模式下启动,没有任何错误,然后给我:

No tests found, exiting with code 0

我的测试文件在src/*.spec.ts中。我试过*.test.js,把它放在src/__test__/(*.js|*.ts(.中

我不明白,为什么它没有找到任何测试

所以我创建了一个全新的react应用程序:

yarn create react-app test --typescript
cd .test
yarn test

它给了我一个完全相同的错误(没有发现测试(,一个全新的create-rect应用程序。尝试过打字和不打字。

我在全局范围内安装了jest,并尝试在项目文件夹中仅使用jest作为命令进行测试。它找到了它应该找到的测试,但它在第一个JSX语句中有语法错误

4 |
5 | test('renders learn react link', () => {
> 6 |   const { getByText } = render(<App />);
|                                ^
7 |   const linkElement = getByText(/learn react/i);
8 |   expect(linkElement).toBeInTheDocument();
9 | });


fresh create react应用程序中的包.json对我来说是这样的:

{
"name": "test",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^24.0.0",
"@types/node": "^12.0.0",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1",
"typescript": "~3.7.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}



我现在使用Windows10。我在描述我的问题时发现了很多错误,但由于微图案的原因,Jest 22/23似乎有这样的问题,而我正在使用Jest 25.2.1和新安装。所提供的解决方案对我都不起作用。
由于我是一个完全不懂开玩笑的人,我希望能得到一些帮助。

这表明您的测试中存在语法错误,因为您的测试文件中有jsx,名称为.ts,但它应该是.tsx

@Cookie我记不清问题到底是什么了,但也许我目前的setupTests.ts可以在这里提供帮助:

import '@testing-library/jest-dom/extend-expect';
module.exports = {
// The root of your source code, typically /src
// `<rootDir>` is a token Jest substitutes
roots: ['<rootDir>/src'],
// Jest transformations -- this adds support for TypeScript
// using ts-jest
transform: {
'^.+\.tsx?$': 'ts-jest',
},
// Runs special logic, such as cleaning up components
// when using React Testing Library and adds special
// extended assertions to Jest
setupFilesAfterEnv: [
'@testing-library/react/cleanup-after-each',
'@testing-library/jest-dom/extend-expect',
],
// Test spec file resolution pattern
// Matches parent folder `__tests__` and filename
// should contain `test` or `spec`.
testRegex: '(/__tests__/.*|(\.|/)(test|spec))\.tsx?$',
// Module file extensions for importing
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
};

我所有的测试都在src/__tests__中。

package.json中向测试脚本添加参数对我有帮助:

"test": "react-scripts test --testMatch '**/*.test.js' '**/*.test.ts' '**/*.test.jsx' '**/*.test.tsx'"

最新更新