ReferenceError:在Jest环境被拆除后,您正在尝试“导入"文件:在loadCjsDefault [英] ReferenceError: You are trying to `import` a file after the Jest environment has been torn down: at loadCjsDefault

查看:353
本文介绍了ReferenceError:在Jest环境被拆除后,您正在尝试“导入"文件:在loadCjsDefault的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试使用Jest构建具有汇总功能的捆绑软件,每当我使用async/await时都会抛出错误.我不知道怎么了我尝试了不同的方法,但都无法正常工作.

I am testing building a bundle with rollup using jest which throws error whenever i use async/await. I have no idea what's wrong. I tried different soultions and it's not working.


ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.

      at loadCjsDefault (node_modules/@babel/core/lib/config/files/module-types.js:77:18)
      at loadCjsOrMjsDefault (node_modules/@babel/core/lib/config/files/module-types.js:49:16)
          at loadCjsOrMjsDefault.next (<anonymous>)
      at readConfigJS (node_modules/@babel/core/lib/config/files/configuration.js:190:47)
          at readConfigJS.next (<anonymous>)

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.

      at _parser (node_modules/@babel/core/lib/parser/index.js:9:16)
      at parser (node_modules/@babel/core/lib/parser/index.js:54:18)
          at parser.next (<anonymous>)
      at normalizeFile (node_modules/@babel/core/lib/transformation/normalize-file.js:93:38)
          at normalizeFile.next (<anonymous>)

  ●  Cannot log after tests are done. Did you forget to wait for something async in your test?
    Attempted to log "TypeError: (0 , _parser(...).parse) is not a function
        at parser (D:\projects\js\published\builderz\node_modules\@babel\core\lib\parser\index.js:54:34)
        at parser.next (<anonymous>)
        at normalizeFile (D:\projects\js\published\builderz\node_modules\@babel\core\lib\transformation\normalize-file.js:93:38)
        at normalizeFile.next (<anonymous>)
        at run (D:\projects\js\published\builderz\node_modules\@babel\core\lib\transformation\index.js:31:50)
        at run.next (<anonymous>)
        at Object.transform (D:\projects\js\published\builderz\node_modules\@babel\core\lib\transform.js:27:41)
        at transform.next (<anonymous>)
        at evaluateSync (D:\projects\js\published\builderz\node_modules\gensync\index.js:244:28)
        at Object.sync (D:\projects\js\published\builderz\node_modules\gensync\index.js:84:14) {
      code: 'PLUGIN_ERROR',
      plugin: 'babel',
      hook: 'transform',
      id: 'D:\\projects\\js\\published\\builderz\\test\\samples\\pure\\src\\index.js',
      watchFiles: [
        'D:\\projects\\js\\published\\builderz\\test\\samples\\pure\\src\\index.js'
      ]
    }".

这是测试文件

import { resolve } from "path";
import builderz from "../src";

jest.useFakeTimers();

describe("production", () => {
  it("test pure js", async () => {
    await builderz({
      isSilent: true,
      paths: [resolve(__dirname, "./samples/pure")]
    });

    // update it later of cource:
    expect(true).toEqual(true);
  });
});

还有babel.config

And babel.config

module.exports = api => {
  api.cache(true);
  return {
    presets: [
      [
        "@babel/preset-env",
        {
          targets: {
            node: "current"
          }
        }
      ]
    ]
  };
};

推荐答案

在package.json中的 jest 键中添加"testEnvironment":"jsdom" >

Add "testEnvironment": "jsdom" into jest key in package.json

"jest": {
  "testEnvironment": "jsdom",
  "preset": "react-native",
  ...

这篇关于ReferenceError:在Jest环境被拆除后,您正在尝试“导入"文件:在loadCjsDefault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆