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
- Adding type module -- Jest: SyntaxError: Unexpected token 'export'
- Adding
transformIgnorePatterns: ["<rootDir>/node_modules/(?!lowdb)"]-- Jest setup "SyntaxError: Unexpected token export" - Add a transform for
jswhich usesbabel-jest-- Jest encountered an unexpected token - SyntaxError: Unexpected token 'export' - Update my
tsconfigto outputcommonJSmodules - Update my
tsconfigtoallowJs:trueand update my jesttransformsto parse JS withts-jest
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",