相关疑难解决方法(0)

如何使用 jest 在 node.js 中模拟 postgresql (pg)

我是 node.js 的新手。我正在 node.js 中为 postgresql 使用pgpg-native无服务器应用程序编写代码。我需要为它编写单元测试。我无法pg使用 jest 或 sinon来模拟客户端。

我的实际代码是这样的

const { Client } = require('pg');
export const getAlerts = async (event, context) => {

  const client = new Client({
    user: process.env.DB_USER,
    host: process.env.DB_HOST,
    database: process.env.DB_DATABASE,
    password: process.env.DB_PASSWORD,
    port: process.env.PORT
  });

  await client.connect();

  try {
    const result = await client.query(`SELECT * FROM public.alerts;`);
    console.log(result.rows);
    client.end();
    return success({ message: `${result.rowCount} item(s) returned`, data: result.rows, status: true });

  } catch (e) {
    console.error(e.stack);
    client.end();
    return …
Run Code Online (Sandbox Code Playgroud)

postgresql unit-testing node.js sinon jestjs

4
推荐指数
3
解决办法
1万
查看次数

标签 统计

jestjs ×1

node.js ×1

postgresql ×1

sinon ×1

unit-testing ×1