javascript - Next.js 和 Jest : SyntaxError: Cannot use import statement outside a module

标签 javascript typescript jestjs next.js babel-jest

我正在使用 TypeScript 开发 Next.js 项目,为了测试我使用 Jest 和 React 测试库。但是,我遇到了 语法错误:无法在模块外使用 import 语句 对于我导入的组件重新炒作 .
据我了解,Jest 不支持 ES6,因此 node_modules 可能需要进行转换。这可以使用 transformIgnorePatterns 进行配置.例如,如果 rehype-raw使用 "transformIgnorePatterns": ["node_modules/(?!rehype-raw)/"] 导致此错误应该允许转换 rehype-raw但没有其他模块。从而解决这个错误。
但是,这对我不起作用。但是我知道为什么以及如何解决这个问题。我发现没有建议的解决方案可以解决这个问题。我已附上我的 错误输出 , jest.config.js babel.rc 下面的文件。
错误输出

 FAIL  test/pages/companies/[id].test.tsx                  
  ● Test suite failed to run

    Jest encountered an unexpected token

    [...]

    Details:

    /path/frontend-job/node_modules/rehype-raw/index.js:7
    import {raw} from 'hast-util-raw'
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      3 | import Image from 'next/image';
      4 | import { Remark } from 'react-remark';
    > 5 | import rehypeRaw from 'rehype-raw';
        | ^
      6 | import rehypeSanitize from 'rehype-sanitize';
      7 | import { logError } from '@utils/logger';
      8 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (components/markdown/JobMarkdown.tsx:5:1)
jest.config.js
const { resolve } = require('path');

module.exports = {
  roots: ['<rootDir>'],
  moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json', 'node'],
  setupFiles: ['<rootDir>/test/setup.js'],
  testPathIgnorePatterns: ['<rootDir>[/\\\\](node_modules|.next)[/\\\\]'],
  transform: {
    '^.+\\.(ts|tsx)$': 'babel-jest',
  },
  transformIgnorePatterns: [
    'node_modules/(?!rehype-raw)/',
  ],
  watchPlugins: [
    'jest-watch-typeahead/filename',
    'jest-watch-typeahead/testname',
  ],
  moduleNameMapper: {
    // Force mocks: https://github.com/facebook/jest/issues/4262
    '@api/axios': '<rootDir>/test/__mocks__/axios.js',
    // Normal module aliases
    '\\.(css|less|sass|scss)$': 'identity-obj-proxy',
    '\\.(gif|ttf|eot|svg|png)$': '<rootDir>/test/__mocks__/fileMock.js',
    '^@test/(.*)$': resolve(__dirname, './test/$1'),
    '^@test/faker/(.*)$': resolve(__dirname, './test/faker/$1'),
    '^@components/(.*)$': resolve(__dirname, './components/$1'),
    '^@pages/(.*)$': resolve(__dirname, './pages/$1'),
    '^@utils/(.*)$': resolve(__dirname, './utils/$1'),
    '^@api/(.*)$': resolve(__dirname, './api/$1'),
    '^@store/(.*)$': resolve(__dirname, './store/$1'),
  },
  testEnvironment: 'jsdom',
};
babel.rc
{
  "presets": ["next/babel"]
}

最佳答案

Next 已经对 Jest 提供了开箱即用的支持,我建议您按照步骤 provided in the docs .

关于javascript - Next.js 和 Jest : SyntaxError: Cannot use import statement outside a module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70916761/

相关文章:

javascript - mapbox.js 没有完全在 Bootstrap Modal 中呈现

javascript - 响应式菜单问题

javascript - 如何仅使用 Angular6 在单击的按钮上设置事件类?

node.js - 30 天后自动删除 MongoDB 文档

reactjs - React Native + Appium iOS elementByAccessibilityId.text() 方法

javascript - Jest renderIntoDocument 不起作用

javascript - 获取相应 tr 中的当前索引

javascript - 如果 url 包含此文件夹 + 随机存档

javascript - 将类型转换为不同类型时出现 typescript 错误

javascript - Jest/expect "toEquals"因未定义的属性而失败