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
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
I would expect that the code would get run without errors.
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
experiencing the same issue
Hey, just to let you know, this solution got me up and running https://github.com/facebook/jest/issues/9395#issuecomment-583799300
Any news on this ? Still having the following similar issue :
import { default as AmplifyCore, I18n } from 'aws-amplify';
^^^^^^
SyntaxError: Cannot use import statement outside a module
import { withOAuth } from 'aws-amplify-react-native'
^
versions :
"aws-amplify": "^3.0.8",
"aws-amplify-react-native": "^4.0.4",
Transforming custom environment will be fixed by #8751, which will come in Jest 26.
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.
Most helpful comment
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.