当前位置: 首页 > 知识库问答 >
问题:

JEST:异步方法中的错误:ReferenceError:未定义regeneratorRuntime[重复]

华献
2023-03-14

我试图测试连接到数据库没有得到数据。为此,我制作了这个测试文件。

import { PGConnection } from "../../../db";

test('two plus two is four', () => {
    expect(2 + 2).toBe(4);
});

test("Test connection to DDBB", () => {
    const db = new PGConnection();

    let result = db.test();
    expect(result).toBe("Connection to database has been established succesfully");


});

我有一个类,在其中创建与数据库的连接:

import Sequelize from "sequelize";
//It's mandatory to import dotenv in each file where we can use enviroment variables
import config from "dotenv";
config.config(); 

//console.log("Usuario DDBB: " + loader.FEB_CONNECTION_USER);


class PGConnection{
    constructor(){
        this.db = this.setConnection();
    }

    setConnection(){
        /*console.log("host: " + process.env.FEB_CONNECTION_HOST + "\n" + 
        "port: " + process.env.FEB_CONNECTION_PORT + "\n" + 
        "user: " + process.env.FEB_CONNECTION_USER + "\n" +
        "password: " + process.env.FEB_CONNECTION_PASSWORD + "\n" + 
        "database: " + process.env.FEB_CONNECTION_DDBBNAME);   */      
        return(
            new Sequelize(
                process.env.DDBB_NAME, 
                process.env.DDBB_USER,
                process.env.DDBB_PSWD, {
                    host: process.env.DDBB_HOST, 
                    port: process.env.DDBB_PORT,
                    define: {
                        freezeTableName: true, /**Don't add 's to the end of each table/model */
                        timestamps: false,  /**Don't add fields createdAt and updatedAt */
        /*                 charset: 'utf8',
                        dialectOptions: {
                            collate: 'utf8_general_ci'
                        },     */            
                    },
                    dialect: "postgres",
                    //Remove operatorAliases due to an error when we update to sequelize 6.0.0
                    //operatorsAliases: false,
                    pool: {
                        max: 5,
                        min: 0,
                        acquire: 30000,
                        idle: 10000
                    }
                }
            )
        );
    }

    async test(){
        try{
            console.log("host: " + process.env.FEB_CONNECTION_HOST + 
            "port: " + process.env.FEB_CONNECTION_PORT + 
            "user: " + process.env.FEB_CONNECTION_USER +
            "password: " + process.env.FEB_CONNECTION_PASSWORD + 
            "database: " + process.env.FEB_CONNECTION_DDBBNAME)
            await this.db.authenticate();
            console.log("Connection to database has been established succesfully");  
            return ("Connection to database has been established succesfully");
            //this.closeConnection();  
        }catch (err){
            console.error("Unable to connect to database: " + err);
            return("Unable to connect to database: " + err);
        }
     }    

    closeConnection(){
        this.db.close();
        console.log("Connection to database has been closed!!!")
    }
}

module.exports.PGConnection = PGConnection;

这门课效果很好。但是,当我试图进行测试时,我得到了这个错误:

 FAIL  src/server/tests/test.spec.js
  ● Test suite failed to run

    ReferenceError: regeneratorRuntime is not defined

      65 | 
      66 |     closeConnection(){
    > 67 |         this.db.close();
         |                                                   ^
      68 |         console.log("Connection to database has been closed!!!")
      69 |     }
      70 | }

我的package.json是:

{
  "name": "",
  "version": "1.0.0",
  "description": "Code to build an API for projects, users and tasks",
  "main": "",
  "scripts": {
    "dev": "babel-node src/server/server.js",
    "test": "jest ./server/tests"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "helmet": "^3.22.0",
    "morgan": "^1.10.0",
    "pg": "^8.2.1",
    "pg-hstore": "^2.3.3",
    "sequelize": "^5.21.11"
  },
  "devDependencies": {
    "@babel/cli": "^7.10.1",
    "@babel/core": "^7.10.2",
    "@babel/node": "^7.10.1",
    "@babel/polyfill": "^7.10.1",
    "@babel/preset-env": "^7.10.2",
    "babel-loader": "^8.1.0",
    "jest": "^26.0.1",
    "webpack": "^4.43.0"
  }
}

我做错了什么?

共有1个答案

弓俊晖
2023-03-14

我找到了一个解决方案,可以修复错误。要修复它,你必须安装下一个插件:@Babel/plugin-transver-runtime

npm install @babel/plugin-transform-runtime --save-dev

另外,你必须在. babelrc文件中配置插件:

"env": {
    "test": {
      "plugins": ["@babel/plugin-transform-runtime"]
    }
}
 类似资料:
  • 问题内容: 我有一个应用程序,它是节点后端和React前端。 当我尝试构建/运行节点应用程序时出现以下错误。 节点: 错误: dist / index.js:314 regeneratorRuntime.mark(function _callee(productId){^ ReferenceError:未定义regeneratorRuntime .babelrc webpack.config.js

  • 问题内容: 标题几乎可以解释我所面临的问题。我正在尝试测试具有某种状态的组件,并且试图为该组件提供存储以获取所需的组件。当我使用Jest运行组件的测试时,出现以下错误: 我已经通过了一些读数确定,这是造成或者没有被正确适用于玩笑。但是,我尝试安装这两个软件包并重新运行,但结果没有变化。阅读Jest Github问题页面(删除babel- polyfill的自动包含#2755 )后,我发现Jest从

  • 这个标题很大程度上解释了我面临的问题。我试图测试一个具有某种状态的组件,并试图将我的存储提供给该组件以获取它需要的内容。当我使用Jest运行组件的测试时,我得到以下错误: 通过一些阅读,我确定这是由或未正确应用于Jest造成的。但是,我已经尝试安装这两个软件包并重新运行,结果没有变化。在阅读Jest Github问题页面(删除babel polyfill#2755的自动包含)后,我发现自版本19起

  • 问题内容: 我正在Ubuntu平台中使用jquery,javascript,php。在页面中,我通过jquery发送一个ajax请求到php文件并获取响应文本。该程序已在Windows-(Wamp)平台和联机环境中成功运行。但是在Ubuntu中,我遇到了JavaScript错误。代码如下… 在Ubuntu中,我收到此错误, “ Uncaught ReferenceError:$未定义” 。该错误显

  • ...我已经设法在一个非常小的设置中重现了它(与相同问题上的类似问题相比),也注意到了一些奇怪的行为,这取决于是否使用了作用域。 以下代码起作用: 但是,当移除作用域并将放在全局作用域上时,它将失败: 更奇怪的是,如果文件被删除/重命名,那么无论作用域是否存在,代码都会成功。顺便说一句,无论是范围还是封装生成器的IIFE都没有区别。 最小的github repo在这里演示了这种行为。 我的问题是:

  • 错误在最后一行,我想知道到底出了什么问题,以及如何修复它,谢谢!