简体   繁体   English

开玩笑(打字稿):SyntaxError:意外的令牌“导出”(lowdb)

[英]Jest (typescript): SyntaxError: Unexpected token 'export' (lowdb)

As the title suggests, I am having trouble with jest not allowing an export from the lowdb package within my tests.正如标题所示,在我的测试中,我遇到了不允许从lowdb package 导出的问题。 It seems to only throw this error for this single package -- the rest of my code is also using ES6 exports and my package.json file has the key type: module . It seems to only throw this error for this single package -- the rest of my code is also using ES6 exports and my package.json file has the key type: module .

What have I tried我试过什么

I'm not sure where I am going wrong and am sorry in advance if I am being dim.我不确定我哪里出错了,如果我很昏暗,请提前道歉。

This is my TS Config这是我的 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"]
}

And this is my jest.config.ts file:这是我的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;

Finally, I am invoking jest as follows:最后,我调用 jest 如下:

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

Thank you in advance.先感谢您。

EDIT: Some additional context编辑:一些额外的背景

I am pretty sure it should not affect this issue but I figure it could help to provide more context.我很确定它不应该影响这个问题,但我认为它可以帮助提供更多的上下文。 I am running jest with two different configs -- one for JS and one for TS and the repo to which it relates has some build scripts written in JS that will only ever be run in nodeJS.我正在使用两种不同的配置运行 jest——一种用于 JS,另一种用于 TS,与之相关的 repo 有一些用 JS 编写的构建脚本,这些脚本只能在 nodeJS 中运行。

The JS config is as follows (has no issues): 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;

And it is invoked as follows:它的调用方式如下:

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

I too ran into similar issue.我也遇到了类似的问题。 Here is how I fixed by tweaking jest.config.js:这是我通过调整 jest.config.js 修复的方法:

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

We want the babel-jest to parse the js files, and the lowDB and one of its dependency -steno.我们希望 babel-jest 解析 js 文件,以及 lowDB 及其依赖项之一 -steno。

Next, we need to ensure the babel.config.js to contain the following (we need to configure babel with @babel/plugin-transform-modules-commonjs plugin to correctly parse import/exports and should use importInterop:'node'.接下来,我们需要确保 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",
          },
        ],
      ],
    },
  },
};

Ensure you have installed all necessary dev dependencies:确保您已安装所有必要的开发依赖项:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM