"Jest failed to parse a file" ... "SyntaxError: Cannot use import statement outside a module"

See original GitHub issue

Today i started a new project from scratch using JEST and to my surprised i encountered this error when trying to create a server using MongoMemoryServer on my beforeEach hook.

Do i have to tweak something on jest config to make it work? I have used this setup before with an older version of mongodb-memory-server and it worked like a charm… Something must have changed either on mongodb-memory-server or i haven’t noticed

jest.config

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  roots: ["<rootDir>/src"],
  // the following line is needed in order to grab modules from the
  // src folder without the need to write them relatively
  moduleDirectories: ["node_modules", "src"],
};

typescript test file

...
describe("POST /my-end-point", () => {
  beforeAll(async () => {
    mongod = await MongoMemoryServer.create();
    // const uri = mongod.getUri();
    // await ConnectMongoose(uri);
  });
  ...
}

error

… " Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax." … … “/home/…/node_modules/mongodb-memory-server-core/node_modules/mongodb/src/bson.ts:1 ({“Object.<anonymous>”:function(module,exports,require,__dirname,__filename,jest){import type {”“”

image

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

4reactions
hemscommented, Jul 25, 2022
  • change the option from just src to <rootdir>/src

this seems to solve the situation!

2reactions
hasezoeycommented, Jul 21, 2022

i have tried the reproduction-repo and can reproduce it, i could also single it down to the jest option moduleDirectories, which when manually set seems like jest will search all specified paths for all possible file extension (seemingly regardless what nested package.json files specify)

things you could try to workaround this issue:

  • change the option from just src to <rootdir>/src
  • replace the option with option modulePaths: ["<rootdir>/src"]
  • disable the option and use tsconfig paths
  • disable the option and use relative paths

Note: i have never used such path mapping before, so i dont know if the listed workaround will work properly, i also have never used this jest option, so i also dont know how to handle that properly but i know that this does not seem to be a problem with MMS

Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: Cannot use import statement outside a module ...
This is a React, Typescript, Webpack project. I am trying to test a module. But Jest won't transform the module to plain javascript...
Read more >
SyntaxError: Cannot use import statement outside a ... - GitHub
I suspect the issue is Jest is not configured to run Node code using "type: module" (ECMAScript Modules).
Read more >
TypeScript Jest: Cannot use import statement outside module
The TypeScript jest error "Cannot use import statement outside module" occurs when we misconfigure jest in a TypeScript project and run test files...
Read more >
Cannot use import statement outside a module [React ...
In this article, we talked about the SyntaxError: Cannot use import statement outside a module error in TypeScript and JavaScript. This error ......
Read more >
cannot use import statement outside a module - ItsJavaScript
How to fix SyntaxError: cannot use import statement outside a module? · Solution 1 – Add “type”: “module” to package.json · Solution 2...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found