Jestjs 使用“时,Jest返回错误”;导入*为..”;

Jestjs 使用“时,Jest返回错误”;导入*为..”;,jestjs,next.js,babel-jest,Jestjs,Next.js,Babel Jest,当我在Jest中使用import*as./filename.js'时,我在运行测试套件时遇到问题。 所有其他导入都运行正常,所以我认为babel配置没有问题。 我使用nextjs10(soreact17)、Jest、Typescript和babeljest 这是我的babel.config.js { "env": { "development": { "presets": ["next/babel&qu

当我在Jest中使用
import*as./filename.js'
时,我在运行测试套件时遇到问题。 所有其他导入都运行正常,所以我认为babel配置没有问题。 我使用nextjs10(soreact17)、Jest、Typescript和babeljest

这是我的babel.config.js

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

下面是给出错误的测试示例:

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.
\u seetingsHelper.js
中,我这样称呼我的cookieHelper:

import * as cookieHelper from './_cookies'

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

错误:

.../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 {

有人已经遇到过这个问题?

有很多人遇到过类似的问题,并且与您上面提到的错误相同。你可以检查一下。这个问题没有通用的解决方案。大多数错误的根本原因是特定于项目的。嘿,谢谢!我正在寻找关于如何安装babel jest或ts jest的文档和其他解决方案。但是你给我的答案让我更深入地了解这个问题。他们的一些解决方案可能对我有用,有问题的组件实际上是一个.js文件,是唯一的一个。谢谢@Tejagoudkandula很高兴它起了作用。干杯