npx jest test/test.ts
produces:
...
XXX/src/declarations/package_manager/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { Actor, HttpAgent } from "@dfinity/agent";
^^^^^^
SyntaxError: Cannot use import statement outside a module
...
Note that src/declarations/package_manager/index.js
is a generated JS file that uses ES imports (and I cannot change that). This file is indirectly (through another module) imported from test/test.ts
.
jest.config.js
:
/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
preset: 'ts-jest/presets/default-esm', // Use this preset to handle ES modules
testEnvironment: "node",
transform: {
"^.+.tsx?$": ["ts-jest",{useESM: true}],
},
extensionsToTreatAsEsm: ['.ts'],
// moduleNameMapper: {
// // If you're using non-ESM packages, you might need to map them correctly
// '^(\\.{1,2}/.*)\\.js$': '$1',
// },
};
tsconfig.json
:
{
"compilerOptions": {
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"lib": ["ES2018", "DOM"], /* Specify library files to be included in the compilation. */
"allowJs": true, /* Allow javascript files to be compiled. */
"jsx": "react", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"moduleResolution": "nodenext",
"module": "nodenext",
"skipLibCheck": true,
},
"include": ["src/**/*.ts", "./src/custom.d.ts"],
}
How to make my .ts
tests to use .js
files with import
inside these .js
without an error?
npx jest test/test.ts
produces:
...
XXX/src/declarations/package_manager/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { Actor, HttpAgent } from "@dfinity/agent";
^^^^^^
SyntaxError: Cannot use import statement outside a module
...
Note that src/declarations/package_manager/index.js
is a generated JS file that uses ES imports (and I cannot change that). This file is indirectly (through another module) imported from test/test.ts
.
jest.config.js
:
/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
preset: 'ts-jest/presets/default-esm', // Use this preset to handle ES modules
testEnvironment: "node",
transform: {
"^.+.tsx?$": ["ts-jest",{useESM: true}],
},
extensionsToTreatAsEsm: ['.ts'],
// moduleNameMapper: {
// // If you're using non-ESM packages, you might need to map them correctly
// '^(\\.{1,2}/.*)\\.js$': '$1',
// },
};
tsconfig.json
:
{
"compilerOptions": {
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"lib": ["ES2018", "DOM"], /* Specify library files to be included in the compilation. */
"allowJs": true, /* Allow javascript files to be compiled. */
"jsx": "react", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"moduleResolution": "nodenext",
"module": "nodenext",
"skipLibCheck": true,
},
"include": ["src/**/*.ts", "./src/custom.d.ts"],
}
How to make my .ts
tests to use .js
files with import
inside these .js
without an error?
1 Answer
Reset to default -1Make a babel.config.cjs
file at the root and add the below as the content:
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current'
}
}
]
]
};