while this issue seems very simple to fix and with some posts stating a similar issue, I followed everything possible before coming here to ask. This issue still persists.
The error I encounter
I have a TS monorepo with some packages, one of which is @repo/db-client
. I import it in my node fastify backend, where I have some tests. For the sake of simplicity and showing what the issue is, let’s say this is my test:
import { expect, test } from "@jest/globals";
import { userSessionTable } from "@repo/db-client";
test("Test if I can access this table", () => {
expect(userSessionTable).toBeTruthy();
});
The @repo/db-client
package exports ESM, by using "type": "module"
in its package.json. Same goes for the backend, that also is "type": "module"
.
Following is the error, seemingly easy to fix:
D:Projectsprojpackagesdb-clientdistindex.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from "./database.js";
^^^^^^
SyntaxError: Unexpected token 'export'
1 | import { expect, test } from "@jest/globals";
> 2 | import { userSessionTable } from "@repo/db-client";
| ^
3 |
My jest.config.js
/** @type {import('ts-jest').JestConfigWithTsJest} **/
export default {
preset: "ts-jest",
testEnvironment: "node",
transform: {
"^.+.(t|j)s?$": ["ts-jest", { useESM: true }],
},
moduleNameMapper: {
"^(\..*)\.jsx?$": "$1",
"^~(.+)\.js$": "<rootDir>/src/$1",
},
};
packages/db-client/package.json
{
"name": "@repo/db-client",
"version": "1.0.0",
"private": true,
"type": "module",
"exports": {
".": "./dist/index.js"
},
"scripts": {
"build": "tsc"
//...
}
packages/db-client/tsconfig.json
{
"extends": "@repo/typescript-config/library.json",
"compilerOptions": {
"outDir": "./dist",
"sourceRoot": "./src",
"baseUrl": "./",
"paths": {
"~/*": ["./src/*"]
},
// "@repo/typescript-config/library.json"
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"incremental": false,
"isolatedModules": true,
"lib": ["es2022", "DOM", "DOM.Iterable"],
"module": "NodeNext",
"moduleDetection": "force",
"moduleResolution": "NodeNext",
"noUncheckedIndexedAccess": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "ESNext"
}
}
apps/backend/package.json
{
"name": "backend",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "nodemon -r dotenv/config --watch src src/server.ts",
"start": "node -r dotenv/config dist/_app.js",
"test": "jest",
"build": "node build.js"
//...
What I tried
I already tried following the fixes in these posts:
- Jest gives an error: “SyntaxError: Unexpected token export”
- https://github.com/aws-amplify/amplify-js/issues/11435
- https://github.com/react-dnd/react-dnd/issues/3443
- https://www.reddit.com/r/typescript/comments/tkvsgk/typescript_jest_unexpected_token_export/
- …
In summary, I tried setting transformIgnorePatterns, switching from ts-jest to babel or esbuild-jest, using “ts-jest/presets/js-with-ts”, ensuring useESM: true
, using extensionsToTreatAsEsm
, and many more.
What the issue seems to be
The issue at hand seems to simply be that Jest can handle ESM, but not the ESM of the dependencies. The db-client package is built, and its index.js is exported from /dist (package.json -> exports -> ".": ["./dist/index.js"]
). That index.js contains an export statement, causing the termination.
I am forced to export only ESM in that package. So I am required to find a fix without having to restructuring the built of my package(s).