首页 > 解决方案 > Mocha/Chai 测试失败,因为 oracledb 节点模块耗时太长

问题描述

我有一个初始化 oracledb 连接池的节点 Web 服务。这是我的项目结构

应用程序.ts

import { ConnectionDB } from "./dBConnections/DBConnection";
async function initilizeApp(){
try{
    console.log("Initializing Application");

    await Promise.all([ ConnectionDB.initilize() ]);

DBConnection.ts

const oracledb = require('oracledb');

export class ConnectionDB{

private constructor(){}

static async initilize(){

    try{
        await oracledb.createPool({
            user: USERNAME,
            password: PASSWORD,
            connectString: CONNECTION_STRING,
            poolMin: 20,
            poolMax: 20,
            poolIncrement: 0
        });

在我的某些模块的代码中,我直接从这样的方法调用 SQL 查询

测试控制器.ts

export class TestControllers{

async getSomething(test: IGetSome){

    query = `SELECT * from table;`;

    let getS = await ConnectionDB.executeSimpleQuery(query, bind);

现在,当我尝试运行大致这样的测试时

测试类.ts

import {expect} from 'chai';
import {TestController} from '../src/controllers/TestController';

describe("Sample test ", () => {
it('Simple test', async () => {

  var x = await getSomething(req);

我收到这个错误

ERROR:ConnectionDB.executeSimpleQuery:Error: NJS-047: poolAlias "default" not found in the connection pool cache

有时我看到对查询的调用在 2000 毫秒或类似的时间后超时。

所以我的问题是两部分。

1> 有没有办法在测试套件开始运行之前建立数据库连接?我尝试了一些随机的事情,包括在 mocha.opts 中添加设置脚本,但似乎没有任何帮助。2> 如果我无法弄清楚数据库连接部分,有没有办法可以模拟这些具有查询的方法,这样我就可以避免运行数据库部分?

如果我要模拟数据库部分,我不确定模块导入将如何工作,因为每当我从测试中导入模块方法并运行它时,它都会默认运行查询。不确定如何为我的代码库编写详尽的测试用例。

标签: node.jsunit-testingmocha.jsintegration-testingchai

解决方案


推荐阅读