Jest 25.1.0 Environment setup: SyntaxError: Cannot use import statement outside a module
See original GitHub issue🐛 Bug Report
Jest ^25.1.0 does not transform specified config files using babel-jest. According to this issue the functionality should be supported. Instead I am getting the following error message:
● Test suite failed to run
/Users/shooshte/Work/PersonalProjects/untitled-combat-game/API/src/__tests__/config/environment.js:1 import { MongoClient } from "mongodb"; ^^^^^^ SyntaxError: Cannot use import statement outside a module
To Reproduce
Steps to reproduce the behavior:
jest.config.js:
module.exports = {
transform: {
"^.+\\.(t|j)s$": "babel-jest"
},
globalSetup: "./src/__tests__/config/setup.js",
globalTeardown: "./src/__tests__/config/teardown.js",
testEnvironment: "./src/__tests__/config/environment.js",
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"]
};
./src/__tests__/config/setup.js
import { MongoClient } from "mongodb";
import NodeEnvironment from "jest-environment-node";
class MongoEnvironment extends NodeEnvironment {
async setup() {
const serverOptions = {
dbUrl: process.env.dbUrl,
dbName: process.env.dbName,
environment: process.env.environment,
port: process.env.port
};
if (!this.global.connection) {
this.connection.database = await MongoClient.connect(dbUrl, {
poolSize: 50,
useNewUrlParser: true,
useUnifiedTopology: true,
wtimeout: 2500
});
this.global.database = connection.database(serverOptions.dbName);
}
}
async teardown() {
await this.global.connection.close();
await super.teardown();
}
runScript(script) {
return super.runScript(script);
}
}
export default MongoEnvironment;
Run npx jest --config jest.config.js --verbose --watch
Expected behavior
I would expect that the code would get run without errors.
envinfo
System:
OS: macOS 10.15.3
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Binaries:
Node: 13.7.0 - /usr/local/bin/node
Yarn: 1.21.1 - /usr/local/bin/yarn
npm: 6.13.7 - /usr/local/bin/npm
npmPackages:
jest: ^25.1.0 => 25.1.0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:31
- Comments:6
Top Results From Across the Web
SyntaxError: Cannot use import statement outside a module ...
I suspect the issue is Jest is not configured to run Node code using "type: module" (ECMAScript Modules).
Read more >How to resolve "Cannot use import statement outside a ...
I solved the problem by using below jest config, after reading Logan Shoemaker's answer. module.exports = { verbose: true, setupFilesAfterEnv: [ ...
Read more >syntaxerror cannot use import statement outside a module ...
The TypeScript jest error "Cannot use import statement outside module" occurs when jest is misconfigured in a TypeScript project. To solve the error,...
Read more >Cannot use import statement outside a module - YouTube
Are you using imports in a node.js application and running into the SyntaxError : Cannot use import statement outside a module error?
Read more >syntaxerror: cannot use import statement outside a module jest
The export statement is used when creating JavaScript modules to export live bindings to functions, objects, or primitive values from the module ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hey, just to let you know, this solution got me up and running https://github.com/facebook/jest/issues/9395#issuecomment-583799300
Now it is expected in Jest 27 according to @SimenB here. That’s too bad. I’ve spent hours trying to get my custom environment working with imports.