Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest 25.1.0 Environment setup: SyntaxError: Cannot use import statement outside a module #9622

Closed
Shooshte opened this issue Feb 28, 2020 · 6 comments

Comments

@Shooshte
Copy link

πŸ› 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 
@Mowgly777
Copy link

experiencing the same issue

@Mowgly777
Copy link

Hey, just to let you know, this solution got me up and running #9395 (comment)

@ThomasCarca
Copy link

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",

@SimenB
Copy link
Member

SimenB commented Apr 21, 2020

Transforming custom environment will be fixed by #8751, which will come in Jest 26.

@SimenB SimenB closed this as completed Apr 21, 2020
kegsay added a commit to matrix-org/syts that referenced this issue Apr 26, 2020
@jarod-legault
Copy link

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.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants