【问题标题】:Unable to import module in my Jest test file无法在我的 Jest 测试文件中导入模块
【发布时间】:2020-09-03 06:36:03
【问题描述】:

我正在为我编写的少数服务编写单元测试。我的服务和组件是用 Typescript 编写的。 我的理解是它无法解析我的打字稿。 我在我的单元测试中导入了一项这样的服务。我正在使用 jest 进行单元测试。 没有导入,测试工作正常

我的

package.json

{
  "name": "web",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@date-io/core": "^1.3.6",
    "@material-ui/core": "^4.0.1",
    "@material-ui/icons": "^4.9.1",
    "@testing-library/dom": ">=5",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "7.2.1",
    "@types/jest": "^24.0.0",
    "@types/js-cookie": "^2.2.6",
    "@types/jwt-decode": "^2.2.1",
    "@types/lodash": "^4.14.150",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.0",
    "@types/react-dom": "^16.9.0",
    "@types/react-router-dom": "^5.1.4",
    "@types/reactstrap": "^8.4.2",
    "@types/recharts": "^1.8.9",
    "@typescript-eslint/eslint-plugin": "^2.28.0",
    "@typescript-eslint/parser": "^2.28.0",
    "axios": "^0.19.2",
    "bootstrap": "4.4.1",
    "clsx": "^1.1.0",
    "cross-env": "^7.0.2",
    "eslint": "6.8.0",
    "eslint-config-airbnb": "18.1.0",
    "eslint-config-prettier": "^6.10.1",
    "eslint-plugin-import": "^2.20.1",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-prettier": "^3.1.3",
    "eslint-plugin-react": "^7.19.0",
    "eslint-plugin-react-hooks": "2.5.0",
    "jquery": "3.0.0",
    "js-cookie": "^2.2.1",
    "jwt-decode": "^2.2.0",
    "lodash": "^4.17.15",
    "material-table": "1.57.2",
    "moment": "^2.24.0",
    "popper.js": "^1.16.0",
    "prettier": "^2.0.4",
    "prop-types": "^15.7.2",
    "react": "^16.8.4",
    "react-dom": "^16.8.4",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.4.1",
    "reactstrap": "^8.4.1",
    "recharts": "^1.8.5",
    "typescript": "~3.7.2"
  },
  "scripts": {
    "start": "CI=true react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "lint": "tsc --noEmit && eslint \"**/*.{js,jsx,ts,tsx}\" --fix"
  },
  "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"
    ]
  },
  "devDependencies": {}
}

tsconfig.json

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

我创建的测试

import axios from 'axios';
test("Check if Company Details exists", () => {
  expect(false).toBe(false);
});

我收到的问题

另外,很明显测试套件并没有自己运行

我尝试了不同的方法,但到目前为止没有任何帮助。

【问题讨论】:

    标签: reactjs typescript configuration jestjs package.json


    【解决方案1】:

    你有 Jest 配置文件 (https://jestjs.io/docs/en/configuration) 吗?如果不是,您应该拥有一个,这似乎与中的问题相同:

    您应该首先添加一个 Jest 配置文件(假设您的测试文件名为 Component.test.ts):

    module.exports = {
        rootDir: '..',
        testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
        transform: {
          '^.+\\.(ts|tsx)?$': 'ts-jest',
        },
    };
    

    【讨论】:

    • 感谢@Florian,但不,仍然是同样的问题,而不是添加 jest.config.js,我对 package.json 文件进行了相应的更改
    • "jest": { "rootDir": "src/**/*", "testMatch": [ "*/tests/**/.+(ts |tsx|js)', '*/?(.)+(spec|test).+(ts|tsx|js)" ], "transform": { "^.+\\ .(ts|tsx)?$": "ts-jest" } },```
    【解决方案2】:

    对 package.json 进行了相应的更改,它运行良好 @Florian 的建议实际上给出了一个想法。谢谢老哥

    
    "jest": {
        "moduleFileExtensions": [
          "js",
          "jsx",
          "tsx",
          "ts"
        ],
        "rootDir": "src/tests/",
        "transform": {
          "^.+\\.(ts|tsx)?$": "ts-jest"
        }
      },```
    

    【讨论】:

      【解决方案3】:

      解决我的问题的方法是将“YourFileName.test.js”简单地更改为“YourFileName.test.ts”。 我一直在使用 ts-jest,但使用的是 .js 扩展名...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-18
        • 2020-02-10
        • 2016-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多