ts-jest: "Cannot use import statement outside a module" when importing a pnpm workspace module (ESM) inside a source
🐛 Bug Report
When I try to run a test for a package inside of a pnpm workspace that uses another package that is compiled to ESM I receive such error:
Details:
/home/v1rtl/Coding/tinyhttp/packages/send/dist/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { format, parse } from 'es-content-type';
^^^^^^
SyntaxError: Cannot use import statement outside a module
> 1 | export * from '@tinyhttp/send'
| ^
2 | export * from './cookie'
3 | export {
4 | setContentType,
at Runtime.createScriptFromCode (node_modules/.pnpm/jest-runtime@27.0.0-next.8/node_modules/jest-runtime/build/index.js:1472:14)
at Object.<anonymous> (packages/res/src/index.ts:1:1)
To Reproduce
Steps to reproduce the behavior:
- Install pnpm and create a pnpm workspace with such configs:
.npmrc:
link-workspace-packages = true
shamefully-hoist = false
shared-workspace-shrinkwrap = true
access = public
pnpm-workspace.yaml:
packages:
- 'packages/*'
- Install ts-jest and others:
pnpm i -WD typescript tslib jest ts-jest @types/jest @jest/globals - Create
jest.config.mjsconfig:
export default {
testEnvironment: 'node',
preset: 'ts-jest/presets/default-esm',
globals: {
'ts-jest': {
useESM: true
}
},
transform: {
'^.+\\.(t|j)sx?$': 'ts-jest'
},
testRegex: '(/tests/.*|\\.(test))\\.(ts|tsx|js)$',
moduleFileExtensions: ['ts', 'js', 'json']
}
- Create 2 workspace packages under “packages” folder:
packages /
a
b
- Set these
package.jsonfields for both projects:
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.js"
},
"./package.json": "./package.json",
"./": "./"
}
- Import one module from another:
// packages/b/src/index.ts
import { add } from '@workspace/a'
export { add }
- Create a test file:
// lib.test.ts
import { add } from './packages/b/src'
import { describe, it } from '@jest/globals'
describe('suite', () => {
it('case', () => void expect(add(1, 2)).toBe(3))
})
Expected behavior
The test runs as if all files were CommonJS
Link to repo (highly encouraged)
https://github.com/talentlessguy/ts-jest-esm-pnpm-workspaces-repro
Debug log:
https://gist.github.com/talentlessguy/f37f04c3736b2cfaebbe4f5568c46bab
envinfo
System:
OS: 5.10.30-1-MANJARO
Node version: 15.14
Npm packages:
jest: 27.0.0-next.8
ts-jest: 27.0.0-next.11
typescript: 4.2.4
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 21
I landed here looking for a solution to a
SyntaxError: Unexpected tokenerror I was facing when importing an ESM-compiled NPM module from a test file in a PNPM monorepo.I was trying to use the following Jest config in one of my monorepo’s packages (along with
allowJs: truein the package’stsconfig.json):… but Jest kept on throwing a
SyntaxErrorin the following file:In case anyone lands here like I did, I managed to get it to work by replacing the ignore pattern with:
I assume this has something to do with how PNPM symlinks modules. 🤷♂️
I think I got it working
looks like TS+ESM+Jest support is not perfect yet
and it treats types as real imports. that was the issue… ah and also adding the node flag
Should I close this issue and open another one such as “Type imports are treated as module imports”?
to summarize, huge thanks for such instant assistance, I might set up an example repo with using jest, ts-jest and ts with latest node so other ppl don’t face such issues
@ahnpnl now it’s another error 😕
@ahnpnl oh oops forgot to include it.
Still doesn’t work though: