0

I have problems to run my test suite when I used import * as './filename.js' with Jest. All the others imports run without problems so I don't think is a problem with babel configuration. I use Nextjs 10(so react17), Jest, Typescript and babel-jest.

Here is my babel.config.js

{
  "env": {
    "development": {
      "presets": ["next/babel"]
    },
    "production": {
      "presets": ["next/babel"]
    },
    "test": {
      "presets": [
        [
          "next/babel",
          {
            "preset-env": {
              "modules": "commonjs"
            }
          }
        ]
      ]
    }
  }
}

And here an example of the test that gives the error:

import React, { FC, useEffect } from 'react'
import Head from 'next/head'
import Header from '../Header/Header'
import HeaderHome from '../HeaderHome/HeaderHome'
import Footer from '../Footer/Footer'
import styles from './Layout.module.css'

import * as settingsHelper from '../../helpers/_settingsHelper'

// .... normal react component here.

And inside the _seetingsHelper.js I call my cookieHelper like so:

import * as cookieHelper from './_cookies'

function initialise () {
 // body of function here
}

error:

.../helpers/_settingsHelper.js:2
    import * as cookieHelper from './_cookies'
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

       6 | import styles from './Layout.module.css'
       7 |
    >  8 | import * as settingsHelper from '../../helpers/_settingsHelper'
         | ^
       9 |
      10 | // prettier-ignore
      11 | interface LayoutProps {

Someone already encountered this issue?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Giorgia Sambrotta
  • 1,133
  • 1
  • 15
  • 45
  • 1
    There are a lot of people who have encountered similar kind of issue with the same error as that you have mentioned above. You can check that https://stackoverflow.com/questions/58613492/how-to-resolve-cannot-use-import-statement-outside-a-module-in-jest. There is no generic solution for this problem. Mostly the root cause for the error is project specific. – Teja Goud Kandula Nov 11 '20 at 12:37
  • hey thanks! I was finding just documentation and others solution about how to install babel-jest or ts-jest. But this answer you linked me go a bit deeper in the problem. Some of their solution might be useful for me, the problematic component is indeed a .js file, the only one. Thanks @TejaGoudKandula – Giorgia Sambrotta Nov 13 '20 at 10:49
  • Glad it helped. Cheers. – Teja Goud Kandula Nov 14 '20 at 00:55

0 Answers0