javascript - Jest 无法使用 Typescript 导入(SyntaxError : Cannot use import statement outside a module)

标签 javascript typescript webpack jestjs babeljs

当我使用 Typescript 运行 Jest 测试时,在 node_modules 库中调用的外部 TS 文件导入时出现以下错误:

SyntaxError: Cannot use import statement outside a module

我确信我缺少一个配置,但是它是什么?感谢您的帮助。

这是我的配置:

  • tsconfig.json
{
  "compilerOptions": {
    "sourceMap": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "allowJs": true,
    "jsx": "react",
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "baseUrl": "./",
    "paths": {
      "*": ["src/*"]
    }
  },
  "strict": true,
  "module": "node",
  "compileOnSave": false,
  "include": ["src", "tests"]
}
  • jest.config.js

    module.exports = {
      roots: ['<rootDir>'],
      preset: 'ts-jest',
      testRegex: 'tests/.*\\.test.(js|jsx|ts|tsx)$',
      transform: {
        '^.+\\.tsx?$': 'ts-jest',
      },
      moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
      moduleDirectories: ['node_modules', 'src'],
      setupFilesAfterEnv: ['<rootDir>/tests/jest.setup.ts'],
      collectCoverage: true,
      collectCoverageFrom: ['src/**/*.{js{,x},ts{,x}}', '!src/index.tsx'],
    }

  • webpack.config.js

    /* eslint-disable @typescript-eslint/no-var-requires */
    const path = require('path')
    const webpack = require('webpack')
    
    module.exports = {
      entry: './src/index.tsx',
      mode: 'development',
      module: {
        rules: [
          {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            options: { presets: ['@babel/env'] },
          },
          {
            test: /\.tsx?$/,
            exclude: /node_modules/,
            loader: 'ts-loader',
          },
          {
            test: /\.css$/,
            exclude: /node_modules/,
            use: ['style-loader', 'css-loader'],
          },
        ],
      },
      resolve: {
        extensions: ['.js', '.jsx', '.ts', '.tsx'],
        alias: { 'react-dom': '@hot-loader/react-dom' },
      },
      output: {
        path: path.resolve(__dirname, 'dist/'),
        publicPath: '/dist/',
        filename: 'app.js',
      },
      devServer: {
        contentBase: path.join(__dirname, 'public/'),
        port: 3000,
        publicPath: 'http://localhost:3000/dist/',
        hotOnly: true,
      },
      plugins: [new webpack.HotModuleReplacementPlugin()],
    }

最佳答案

我遇到了同样的问题,请检查导入中的所有路径是否正确 就我而言,我有 import NavbarCollapse from "react-bootstrap/esm/NavbarCollapse"; 而不是 import NavbarCollapse from "react-bootstrap/NavbarCollapse";

关于javascript - Jest 无法使用 Typescript 导入(SyntaxError : Cannot use import statement outside a module),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63958367/

相关文章:

javascript - 如何找到 td 类并检查它是否包含类然后我需要显示弹出窗口?

reactjs - TS2339 : Property 'props' does not exist on type 'Home'

.htaccess - 使用 webpack 构建应用程序后 React 路由不起作用

javascript - 在 Vue-CLI 3 项目的 i18n 标签中添加 yaml-loader

javascript - Angular 2 : import external js file into component

javascript - 如果整个元素都在 View 中,则元素位置为 :fixed?

javascript - 通过js从php页面更改图像的src

javascript - 如何在 ASP.NET MVC 中使用预定义设置创建 .xml 文件?

reactjs - 仅在条件为真时测试显示按钮

node.js - mongoose.connect 错误 TS2531 : Object is possibly 'null'