【问题标题】:Unable to create a Pool instance with constructor in jest test无法在开玩笑测试中使用构造函数创建池实例
【发布时间】:2020-03-06 01:13:23
【问题描述】:

当我运行一个笑话测试时,当我需要池时创建一个池实例,它返回一个 _pg.Pool is not a constructor 错误。

我试过查看 StackOverflow:pg.Pool is not a constructor

这仍然不起作用。

但是,当我运行代码时,我能够创建一个池实例,该错误仅在 Jest 中显示。

节点代码:

import { Pool } from 'pg'

const pool = new Pool({configs})

export default pool

错误日志:

● Test suite failed to run

    TypeError: _pg.Pool is not a constructor

    > 6 | const pool = new Pool({
        |              

      at Object.<anonymous> (src/resources/connection.js:6:14)
      at Object.require (src/routes/api.js:2:20)
      at Object.<anonymous> (src/__tests__/integration/user.test.js:8:1)

旁注:代码是https://node-postgres.com/api/pool中文档的副本

我不希望发生错误,因为 pg.Pool 是一个带有构造函数的类。

【问题讨论】:

  • 我也有同样的问题。你有没有解决它,@L。阿尔斯?
  • @heniotierra 我当时没有设法修复它,我不再处理该代码,但感谢您的帮助!

标签: node.js jestjs node-postgres


【解决方案1】:

如果有人遇到同样的问题,我通过安装已合并到主 pg 包中的 pg-pool 来解决它,然后导入 pg-pool 的 Pool 而不是 pg 的。

参考:https://github.com/brianc/node-postgres/tree/master/packages/pg-pool

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 2017-10-20
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    相关资源
    最近更新 更多