如何在使用Jest时读取绝对路径?

huangapple go评论66阅读模式
英文:

How to read absolute path when using Jest?

问题

我有一个使用Vite框架的React应用程序,我想使用Jest编写测试用例。但问题是,Jest无法读取导入语句中使用的绝对路径。

以下是参考的tsConfig文件和jest配置文件。

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "lib": ["dom", "dom.iterable", "esnext"],
    "types": ["./types/fin", "./types/heap", "node", "jest", "vite/client", "vite-plugin-svgr/client"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": false,
    "jsx": "react-jsx",
    "noImplicitAny": false,
    "baseUrl": ".",
    "paths": {
      "src/*": ["src/*"]
    }
  },
  "include": ["src", "src/config/authConfig.ts"],
  "exclude": ["node_modules", "build"]
}

jest.config.json

module.exports = {
  preset: 'ts-jest',
  collectCoverage: true,
  collectCoverageFrom: ['./src/**'],
  testEnvironment: 'node',
  moduleNameMapper: { '^.+\\.(css|less|gif|jpg|jpeg|svg|png)$': 'module.exports = {};' },
};

请查看配置文件并建议可以在其中执行的操作。

错误:

● Test suite failed to run

Cannot find module 'src/pages/page1' from 'src/components/Modules/Module1.tsx'

Require stack:
  'src/components/Modules/Module1.tsx'
  'src/components/Modules/Module1.test.tsx'

  1 | import { useState, useEffect, useContext, useRef } from 'react';
> 2 | import { dummy } from 'src/pages/page1';
    | ^
  3 | import { dummy2 } from 'src/pages/page';

  at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:427:11)
  at Object.require (src/components/Modules/Module1.tsx:2:1)
  at Object.<anonymous> (src/components/Modules/Module1.tsx.test.tsx:2:1)
英文:

I have a react application with Vite framework in which I want to write the test cases using Jest.
But the issue is jest is unable to read absolute path used in the import statements.

Attaching tsConfig file and jest config file for reference.

tsconfig.json

    {
  &quot;compilerOptions&quot;: {
    &quot;target&quot;: &quot;ESNext&quot;,
    &quot;lib&quot;: [&quot;dom&quot;, &quot;dom.iterable&quot;, &quot;esnext&quot;],
    &quot;types&quot;: [&quot;./types/fin&quot;, &quot;./types/heap&quot;, &quot;node&quot;, &quot;jest&quot;, &quot;vite/client&quot;, &quot;vite-plugin-svgr/client&quot;],
    &quot;allowJs&quot;: true,
    &quot;skipLibCheck&quot;: true,
    &quot;esModuleInterop&quot;: true,
    &quot;allowSyntheticDefaultImports&quot;: true,
    &quot;strict&quot;: true,
    &quot;forceConsistentCasingInFileNames&quot;: true,
    &quot;noFallthroughCasesInSwitch&quot;: true,
    &quot;module&quot;: &quot;esnext&quot;,
    &quot;moduleResolution&quot;: &quot;node&quot;,
    &quot;resolveJsonModule&quot;: true,
    &quot;isolatedModules&quot;: true,
    &quot;noEmit&quot;: false,
    &quot;jsx&quot;: &quot;react-jsx&quot;,
    &quot;noImplicitAny&quot;: false,
    &quot;baseUrl&quot;: &quot;.&quot;,
    &quot;paths&quot;: {
      &quot;src/*&quot;: [&quot;src/*&quot;]
    }
  },
  &quot;include&quot;: [&quot;src&quot;, &quot;src/config/authConfig.ts&quot;],
  &quot;exclude&quot;: [&quot;node_modules&quot;, &quot;build&quot;]
}

jest.config.json

module.exports = {
  preset: &#39;ts-jest&#39;,
  collectCoverage: true,
  collectCoverageFrom: [&#39;./src/**&#39;],
  testEnvironment: &#39;node&#39;,
  moduleNameMapper: { &#39;^.+\\.(css|less|gif|jpg|jpeg|svg|png)$&#39;: &#39;module.exports = {};&#39; },
};

Please have a look on the config files and suggest what can done in this.

Error:-

● Test suite failed to run

    Cannot find module &#39;src/pages/page1&#39; from &#39;src/components/Modules/Module1.tsx&#39;

    Require stack:
      &#39;src/components/Modules/Module1.tsx&#39;
      &#39;src/components/Modules/Module1.test.tsx&#39;

      1 | import { useState, useEffect, useContext, useRef } from &#39;react&#39;;
    &gt; 2 | import { dummy } from &#39;src/pages/page1&#39;;
        | ^
      3 | import { dummy2 } from &#39;src/pages/page&#39;;

      at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:427:11)
      at Object.require (src/components/Modules/Module1.tsx:2:1)
      at Object.&lt;anonymous&gt; (src/components/Modules/Module1.tsx.test.tsx:2:1)

答案1

得分: 0

在我的情况下,为了解决这个问题,我只是在 jest.config.js 中添加了以下内容:

moduleNameMapper: { '^.+\\.(css|less|gif|jpg|jpeg|svg|png)$': 'module.exports = {};', 'src/(.*)': '<rootDir>/src/$1' },

请注意我添加的最后一个 src 部分。

英文:

To solve this issue in my case I just added this in jest.config.js

moduleNameMapper: { &#39;^.+\\.(css|less|gif|jpg|jpeg|svg|png)$&#39;: &#39;module.exports = {};&#39;, &#39;src/(.*)&#39;: &#39;&lt;rootDir&gt;/src/$1&#39; },

Look at the last src part which I have added.

huangapple
  • 本文由 发表于 2023年5月17日 19:54:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定