js测试在测试tsx文件时不起作用

时间:2020-10-19 09:54:19

标签: reactjs typescript jestjs

我面临的一个问题是我不太了解,所以请您尝试帮助我进行修复和理解。

几天以来我一直在测试以稳定我的应用程序。我的应用程序由ReactJs和typeScript组成,但我的测试仅以Js编写(因为我认为那对键入测试不是很有用)

因此,对于ts测试,它可以很好地通过12个套件测试中的9个,但是对tsx文件进行了1​​2个套件测试中的3个,然后就不起作用了……就像开玩笑一样,请注意了解tsx文件,对吗? >

我有这样的错误:

●测试套件无法运行

SyntaxError: /Users/***/function.spec.js: Unexpected token, expected "," (44:16)

  42 |     }
  43 |     wrapper = mount(
> 44 |       <Provider store={store}>
     |                 ^
  45 |         <HookWrapper />
  46 |       </Provider>
  47 |     );

  at Object._raise (../../../../../usr/local/lib/node_modules/jest/node_modules/@babel/parser/src/parser/error.js:60:45)

或其他类似的东西:

●测试套件无法运行

SyntaxError: /Users/***/index.spec.js: Unexpected token, expected "," (32:34)

  30 |   it("Run file/index is ran", async () => {
> 32 |     wrapper = shallow(<ComponentFile {...minProps} />);
     |                                   ^
  33 |     expect(wrapper).toBeTruthy();
  34 |   });
  35 |

  at Object._raise (../../../../../usr/local/lib/node_modules/jest/node_modules/@babel/parser/src/parser/error.js:60:45)

jest.config.js

module.exports = {
  verbose: true,
  collectCoverage: true,
  coveragePathIgnorePatterns: [],
  forceCoverageMatch: [" */*.spec.js"],
  testEnvironment: "jsdom",
  moduleFileExtensions: ["ts", "tsx", "js"],
};

我花了一天的时间来了解什么是错的,但无所事事...我尝试了很多玩笑的配置,因为我认为问题出在其中,但我没有成功

1 个答案:

答案 0 :(得分:0)

自整整2天以来,我搜索了很多有关发生的情况,并找出了问题所在! 我不确定这对每个人都会有帮助,但对我来说却有用。

我想说的是,我对安装程序的感觉很棒(问题出在这里),但我太关注babel.config.js / babel.config.js / tsconfig.js /了。 .config.js ...但事实是我认为steupTests.js默认是在npm测试/编译时被jest发现的(无论如何),但我完全错了,最后我将其包含在jest.config.js中setupFilesAfterEnv: ["./src/setupTests.js"],现在我所有的套件测试都通过了

我给你我的配置:

jest.config.js

module.exports = {
    clearMocks: true,

    coverageDirectory: "coverage",
  
    coveragePathIgnorePatterns: ["/node_modules/"],
  
    moduleNameMapper: {
      "\\.(css|less)$": "identity-obj-proxy",
    },
  
    testMatch: [
      // "**/__tests__/**/*.[jt]s?(x)",
      "**/?(*.)+(spec|test).[tj]s?(x)",
    ],
  
    testPathIgnorePatterns: ["/node_modules/"],

    verbose: true,
    setupFilesAfterEnv: ["./src/setupTests.js"],
  
  };
  

tsconfig.js:

{
  "compilerOptions": {
    "target": "es5",
    "module": "esnext",
    "types": ["jest", "node"],
    "noEmit": true,
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "react"
  },
  "include": ["src"]
}

babel.config.js

// babel.config.js
module.exports = {
  presets: [
    [
      "@babel/preset-env",
      {
        targets: {
          node: "current",
        },
      },
    ],
    "@babel/preset-typescript",
    "@babel/preset-react",
  ],
};

希望能对你们中的一些人有所帮助!