Jest(TypeScript):SyntaxError:Unexpected token 'export'(lowdb)

3
正如标题所述,我在使用jest测试时遇到了问题,它不允许从lowdb包中导出。它似乎对这个单一的包抛出此错误 - 我的其余代码也使用ES6导出,并且我的package.json文件有键type: module我尝试过的方法

我不确定我错在哪里,如果我很迟钝,我提前道歉。

这是我的TS配置
{
  "compilerOptions": {
    "rootDir": "src",
    "outDir": "dist",
    "esModuleInterop": true,
    "allowJs": true,
    "target": "ES6",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "module": "ES6",
    "baseUrl": "src",
    "declaration": true,
    "allowSyntheticDefaultImports": true,
    "paths": {
      "@services/*": ["services/*"],
      "@constants/*": ["constants/*"],
      "@typeDefs/*": ["typeDefs/*"],
      "@config/*": ["config/*"],
      "@utils/*": ["utils/*"],
      "@assets/*": ["assets/*"]
    }
  },
  "include": ["src/**/*.ts"],
  "exclude": ["rollup.config.ts", "jest.config.ts"]
}

这是我的jest.config.ts文件:

import type { Config } from "@jest/types";
import { pathsToModuleNameMapper } from "ts-jest";
import { compilerOptions } from "./tsconfig.json";
// Sync object
const config: Config.InitialOptions = {
  verbose: true,
  roots: ["<rootDir>"],
  preset: "ts-jest",
  testEnvironment: "node",
  transform: {
    "^.+\\.ts$": "ts-jest",
    "^.+\\.js$": "babel-jest",
  },
  testRegex: ["^.+\\.test\\.ts$"],
  moduleDirectories: ["src", "node_modules"],
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
};
export default config;

最后,我按照以下方式调用jest:

"test:ts": "jest --config=jest.config.ts",

提前感谢您。

编辑:一些附加信息

我很确定它不应该影响这个问题,但我想提供更多上下文可能有所帮助。我正在使用两个不同的配置运行jest——一个用于JS和另一个用于TS,而与之相关的存储库具有一些仅在nodeJS中运行的JS构建脚本。

JS配置如下(没有问题):

// Sync object
/** @type {import('@jest/types').Config.InitialOptions} */
const config = {
  verbose: true,
  testEnvironment: "jest-environment-node",
  transformIgnorePatterns: ["/node_modules/"],
  transform: {},
  testRegex: ["^.+\\.test\\.js$"],
};

export default config;

下面是调用的方式:

"test:js": "yarn node --experimental-vm-modules $(yarn bin jest) --config=jest.config.js",
1个回答

0

我也遇到了类似的问题。以下是我通过调整jest.config.js文件来解决它的方法:

  module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  transformIgnorePatterns: ["/node_modules/(?!lowdb|steno)"],
  transform: {
    "^.+\\.(js)?$": require.resolve("babel-jest"),
  },
};

我们希望babel-jest解析js文件,以及lowDB和其依赖项之一-steno。
接下来,我们需要确保babel.config.js包含以下内容(我们需要使用@babel/plugin-transform-modules-commonjs插件配置babel以正确解析import/exports,并应使用importInterop:'node'。
module.exports = {
  env: {
    test: {
      presets: [
        [
          "@babel/preset-env",
          {
            targets: {
              node: "current",
            },
            modules: "commonjs",
          },
        ],
      ],
      plugins: [
        [
          "@babel/plugin-transform-modules-commonjs",
          {
            importInterop: "node",
          },
        ],
      ],
    },
  },
};

请确保您已安装所有必要的开发依赖项:

 npm i -D babel-jest @babel/core    
 npm i -D @babel/preset-env     
 npm i -D @babel/plugin-transform-modules-commonjs  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接