JEST:异步方法出错:ReferenceError: regeneratorRuntime未定义。

5

我想测试数据库连接,但不获取数据。为此,我编写了这个测试文件。

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
这个已经被讨论过了,请参见 https://dev59.com/1uk5XIcBkEYKwwoY_O6N - jonrsharpe
不太对!!!在那个问题中,解决方案是安装“babel-polifill”,而我已经安装了它。我的问题是另一个不同的问题。尽管如此,还是谢谢你的帮助!!! - José Carlos
1
这个重复问题上有六个答案,其中一个与下面的相同。请向下滚动查看。 - jonrsharpe
是的!!! 你说得对!!! 第五个答案解决了我的问题!!! - José Carlos
嗨@jonrsharpe!!! 是的,但只回答第5个问题。 - José Carlos
这并不重要,关键是你的答案已经存在。这篇文章可能作为指路标志有用,但应该指向更多答案的规范位置。例如阅读 https://stackoverflow.blog/2010/11/16/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/。 - jonrsharpe
1个回答

9

我找到了一个修复错误的方法。要解决它,你需要安装下一个插件:@babel/plugin-transform-runtime。

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

此外,您需要在.babelrc文件中配置插件:
"env": {
    "test": {
      "plugins": ["@babel/plugin-transform-runtime"]
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接