Perform a unit test in NodeJs. With mocha and chai;

0

I am performing a unit test on a function, and what I want is to simulate the communication with the DTA (that does not communicate with the database); For example: This is the function to which I want to perform the test ...

public add(a, b): Promise<any> {
    return new Promise((resolve, reject) => {
        this.queueDta.add(a, b)
        .then(() => resolve({status: "created"}))
        .catch(() => reject({code: "QUE01"}));
    });
}

And then when doing the test, I want the add function that is connected to the DTA, to be executed but in a false way and return true; Create a stub to the DTA to modify the function, but take them differently, therefore, add the information to the database.

context("addToQueue", () => {
    let data;
    it("ADD", async () => {
        const queueBns2 = new queueBns.QueueBns();
        const queueDta2 = new queueDta.QueueDta();
        let result2;

        data = sandbox.stub(queueDta2, "add").value(true);
        result2  = await queueBns2.add(1, 2);
          //-- Aca irian los expect, para validar la funcion ---
    });
});

I was looking for ways to instantiate the class in another way but I do not know, in what way I can modify that function and that the BNS uses the false function. Thank you for your cooperation. I'm pending:).

    
asked by Johan Esneyder Rojas 17.08.2018 в 16:51
source

1 answer

0

I found the solution and it is as follows:

My test file in this case is called

  

QueueBns.spec.ts

My imports are:

declare var require: any;
import * as chai from "chai";
import "mocha";
import "reflect-metadata";

const sinon = require("sinon");
const expect = chai.expect;
let sandbox;
import * as sinonChai from "sinon-chai";
import { QueueBns, QueueDta } from "..";

const chaiIsPromised = require("chai-as-promised");
chai.use(chaiIsPromised);
chai.use(sinonChai);

And the test is as follows:

describe("QueueBns", () => {

afterEach(() => { sandbox.restore(); });

before(() => { sandbox = sinon.sandbox.create(); });

let instance2;

context("addToQueue", () => {
    let data;
    it("ADD", async () => {
        instance2 = new QueueBns();
        let result2;
        data = sandbox.stub(QueueDta.prototype, "add").resolves({nada: "Johan"});
        result2  = await instance2.add(1, 2);

        console.log("result2", result2); // El resultado es: {nada: "Johan"}
    });
});

});

  

QueueBns.ts

import {injectable} from "inversify";
import {QueueDta} from "../data/QueueDta";
@injectable()
class QueueBns {
private queueDta: QueueDta;

constructor() {
    this.queueDta = new QueueDta();
}

public add(a, b): Promise<any> {
    return new Promise((resolve, reject) => {
        this.queueDta.add(a, b)
        .then((data) => resolve(data))
        .catch(() => reject({code: "QUE01"}));
    });
 }
}
export {QueueBns};
  

QueueDta.ts

import { db } from "../../../app.config";
export class QueueDta {
    public add(a, b): Promise<any> {
        return new Promise<any>((resolve, reject) => {
            db.collection("suma")
            .add({a, b})
            .then(() => resolve(true))
            .catch((error) => reject(error));
        });
    }
}

As such we can observe in the test, that we managed to modify the result of the add function found in QueueDta.ts

I hope you can use them if you encounter the same problem:).

    
answered by 17.08.2018 в 18:11