Reputation: 999
I am using dynamic imports in my nodejs code to import some modules. Ex:
const initialize = (await import('./init')).default;
await initialize();
But when i run jest
, it throws the following error:
import sequelize from 'sequelize';
^^^^^^
SyntaxError: Cannot use import statement outside a module
I am assuming that ts-jest
is not transforming/transpiling the code that is imported dynamically.
My jest.config.js
file has:
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
rootDir: './src/test',
globalSetup: '../setup/test_setup.ts',
};
Please advice on how to resolve this
Upvotes: 0
Views: 1027
Reputation: 999
The error was occurring when the globalSetup
code was being run. Not sure if ts-jest
doesn't transpile globalSetup
code.
To fix the issue, I added this to the top of the test_setup.ts
file (which has the globalSetup code):
require('ts-node').register({ transpileOnly: true });
(https://github.com/kulshekhar/ts-jest/issues/411#issuecomment-850944698)
You need to install ts-node
for this to work.
Upvotes: 2