0
votes

I am new to typescript and have a simple function to fetch values from the Postgres database. I need to test the method by mocking the database, I have tried various approaches provided but nothing of them worked. Some or other errors always occur. Sorry for asking it again but couldn't get a working test, might be because I am new to typescript. I have tried below solutions

below are my files

db.ts

import { Pool } from 'pg'

const pool = new Pool({
    user: `${process.env.POSTGRES_USER}`,
    database: `${process.env.POSTGRES_DB}`,
    password: `${process.env.POSTGRES_PASSWORD}`,
    port: `${process.env.POSTGRES_PORT}`,
    host: `${process.env.POSTGRES_HOST}`,

})
export default pool;

fetch.ts

import pool from "./db";
import {SO} from "./s2l";

    export async function fetch(code: string): Promise<SO[]>{
        let socalls: SO[] =[]
        const sql = 'Select sg.v1,sg.v2,sg.v3,sg.v4 from table1 sg where code = $1 and sg.r_code not in(\'ABC\', \'XYZ\', \'PQR\') order by sg.datetime asc'
        const values = [code]
        const client =  await pool.connect()
        await client.query(sql, values).then(res => {
            const data = res.rows;
            let so
            data.forEach(row => {
                so ={
                service: {
                    code: row["v1"]
                },
                rCode: row["v2"],
                dVo: row["v3"],
                aVo: row["v4"],
            };
            socalls.push(so)
        });
        }).catch(e => console.error(e))
        .then(() => client.release())
        return Promise.all(socalls);
        }

Also, I need to pass the list in the query dynamically.

Any help or links is very much appreciated, please let me know any good resources for typescript and its testing framework. Thanks in advance :-)