简体   繁体   English

yarn test(jest) 发现没有使用 create-react-app 的测试

[英]yarn test(jest) finds no tests with create-react-app

I am fairly new to automated testing in JavaScript and wanted to include Jest in my work.我对 JavaScript 中的自动化测试还很陌生,想在我的工作中加入 Jest。

I followed a simple guide for Jest-Testing in create-react-app.我遵循了 create-react-app 中 Jest-Testing 的简单指南。

When I try to run my test with yarn test , it starts in watch mode without any errors and then gives me:当我尝试使用yarn test运行我的测试时,它以监视模式启动,没有任何错误,然后给我:

No tests found, exiting with code 0

My testfile is in src/*.spec.ts.我的测试文件在 src/*.spec.ts 中。 I tried *.test.js, put it in src/__test__/(*.js | *.ts).我试过 *.test.js,把它放在 src/__test__/(*.js | *.ts) 中。

I could not figure out, why it does not find any test.我想不通,为什么它没有找到任何测试。

So I created a completely new react app:所以我创建了一个全新的 React 应用程序:

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

And it gave me the exact same error (no tests found) with a plain new create-react-app.它给了我完全相同的错误(没有找到测试),一个普通的新 create-react-app。 Tried it with and without typescript.尝试使用和不使用 typescript。

I installed jest globally and tried to test with just jest as command in my project folder.我在全球范围内安装了 jest,并尝试在我的项目文件夹中仅使用jest作为命令进行测试。 It finds the tests like it should, but it has syntax errors with the first JSX statement:它像它应该的那样找到测试,但是它的第一个 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 | });


The package.json from the fresh create-react-app looks like this for me:新鲜的 create-react-app 中的package.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"
    ]
  }
}



I am on Windows10 right now.我现在在 Windows10 上。 I found many errors describing my problem but it seems there was such a thing with Jest 22/23 because of micropatterns, while I am on Jest 25.2.1 and a fresh install.我发现了很多描述我的问题的错误,但由于微模式,Jest 22/23 似乎有这样的事情,而我正在使用 Jest 25.2.1 和全新安装。 None of the provided solutions worked for me.所提供的解决方案都不适合我。
Since I am completely new to jest I would appreciate some help.由于我是开玩笑的新手,因此我将不胜感激。

It shows that you have syntax errors in your test, because your test file has jsx inside of it and is named .ts , but it should be .tsx它表明你的测试中有语法错误,因为你的测试文件里面有 jsx 并且被命名为.ts ,但它应该是.tsx

@Cookie I don't remember exactly what the problem itself was in the end, but maybe my current setupTests.ts can help out here: @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'],
};

All my tests are in src/__tests__ .我所有的测试都在src/__tests__中。

Adding arguments to test script in package.json helped for me:将 arguments 添加到package.json中的测试脚本对我有帮助:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM