简体   繁体   中英

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

{
  "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:

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:

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

The JS config is as follows (has no issues):

// 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:

  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.

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

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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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