jest says cannot import outside a module

Viewed 4847

I'm attempting make a few functions using the Test Driven Development (TDD)

I am writing in javascript.

checkTransparency(urlString)
maketransparent(urlString)
are two functions of mine I'm trying to test and develop which is located in a file called transcript.js. These uses the inkscape and graphicsmagick npm. I checked checkTransparent works in some other project of mine, but I'm trying to make sure I can just copy paste this transparent.js into another project and use it elsewhere as well.

My folder structure of the project are the following :

+ node_modules
+ src
--- transparent.js
+ test
--- transparent.spec.js
+ package.json
+ package-lock.json
+ jest.config.js

I am using jest as my test framework. The problem is when I run jest (or npm test)
I get the following:

FAIL test/transparent.spec.js
● Test suite failed to run

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

\\..............\transparent\test\transparent.spec.js:4 <FEW DETAILS OMITTED HERE DELIBERATELY>
import { checkTransparency, makeTransparent } from "../src/transparent"; // const transparent = require("../src/transparent");
^^^^^^

SyntaxError: Cannot use import statement outside a module

  at Runtime._execModule (C:/Users/Kjeong/AppData/Local/Yarn/Data/global/node_modules/jest-runtime/build/index.js:988:58)

Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 0.862s Ran all test suites.

my jest.config.js:

module.exports = {
  testEnvironment: "node",
  moduleDirectories: ["node_modules", "src", "transparent"],
  moduleFileExtensions: [
    "js",
    "json",
    "jsx",
    "ts",
    "tsx",
    "node"
  ],
  clearMocks: true,

}

I've tried the following exports to get this thing working:

export function checkTransparency(urlString) { ... }
export function makeTransparent(urlString) {... }
module.exports = {
     checkTransparency: checkTransparency,
     makeTransparent: makeTransparent,
};
2 Answers

In your package.json, using configuration like following could solve your problem:

{
    "name": "<blah blah>",
    "version": "1.0.0",
    "description": "",
    "type": "module",
    "scripts": {
        "start": "node server.js",
        "test": "node --experimental-vm-modules node_modules/.bin/jest"
    },
}

If you really want to use import keyword then you probably need to follow these explanations. Otherwise why not just require ?

const { checkTransparency, makeTransparent } = require('../src/transparent')

Hope this helps :)

Related