Jest 在使用 import 语句中的 meta 时出现错误。

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

Jest is giving error while using meta in import statement

问题

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 import.meta

Restarted the TS server as well but still this issue exist.

Attaching tsconfig file and jest config file for reference.

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.js

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

Please have a look at the config files and suggest what can be done to resolve this.

Error:-

● Test suite failed to run

    src/helpers/envHelper.tsx:1:25 - error TS1343: The 'import.meta' meta- 
    property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 
    'system', 'node16', or 'nodenext'.

    export const NODE_ENV = import.meta.env.VITE_NODE_ENV;

(Note: The code and configuration files are provided without translation, as requested.)

英文:

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 import.meta

Restarted the TS server as well but still this issue exist.

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.js

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;, &#39;src/(.*)&#39;: &#39;&lt;rootDir&gt;/src/$1&#39; },
};

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

Error:-

● Test suite failed to run

    src/helpers/envHelper.tsx:1:25 - error TS1343: The &#39;import.meta&#39; meta- 
    property is only allowed when the &#39;--module&#39; option is &#39;es2020&#39;, &#39;es2022&#39;, &#39;esnext&#39;, 
    &#39;system&#39;, &#39;node16&#39;, or &#39;nodenext&#39;.

    export const NODE_ENV = import.meta.env.VITE_NODE_ENV;

答案1

得分: 0

Use this code in the .test file in order to mock the import statement for meta (import.meta)

jest.mock('src/util/helpers/envVariableHelper.tsx', () => ({
NODE_ENV: 'test',
REACT_APP_BASE_URL: 'http://mocked-base-url',
}));

英文:

Use this code in the .test file in order to mock the import statement for meta (import.meta)

jest.mock(&#39;src/util/helpers/envVariableHelper.tsx&#39;, () =&gt; ({
  NODE_ENV: &#39;test&#39;,
  REACT_APP_BASE_URL: &#39;http://mocked-base-url&#39;,
}));

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

发表评论

匿名网友

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

确定