Skip to content Skip to sidebar Skip to footer

How Do I Call A Resolve In The Callback Of A Mocked Function?

Here is the function I am trying to test: index.js: import ThirdParty from 'third-party'; function Main(){} Main.prototype.getStuff = function(){ return new Promise((resolve,

Solution 1:

Here is the unit test solution:

index.js:

importThirdPartyfrom"./third-party";

exportdefaultfunctionMain() {}

Main.prototype.getStuff = function() {
  returnnewPromise((resolve, reject) => {
    this.getOtherStuff().then(data => {
      const tpinstance = newThirdParty();
      tpinstance.createThing().nestedFunction(null, () => {
        const goodstuff = data;
        resolve({ newdata: goodstuff });
      });
    });
  });
};

Main.prototype.getOtherStuff = function() {
  returnnewPromise((resolve, reject) => {
    resolve();
  });
};

third-party.js:

exportdefaultfunctionThirdParty() {}

ThirdParty.prototype.createThing = function() {
  console.log("real create thing");
  returnthis;
};

ThirdParty.prototype.nestedFunction = function(arg, cb) {
  console.log("real nested function");
};

index.spec.js:

importMainfrom"./";
importThirdPartyfrom"./third-party";

jest.mock("./third-party.js", () => {
  const mThirdParth = {
    createThing: jest.fn().mockReturnThis(),
    nestedFunction: jest.fn()
  };
  return jest.fn(() => mThirdParth);
});

describe("Main", () => {
  describe("#getStuff", () => {
    afterEach(() => {
      jest.restoreAllMocks();
      jest.resetAllMocks();
    });
    it("should pass", async () => {
      jest
        .spyOn(Main.prototype, "getOtherStuff")
        .mockResolvedValueOnce({ data: "value" });

      let callback;
      const tpinstance = newThirdParty();
      tpinstance.createThing().nestedFunction.mockImplementation((arg, cb) => {
        callback = cb;
        cb();
      });
      const instance = newMain();
      const pending = instance.getStuff();
      console.log(pending);
      const actual = await pending;
      expect(actual).toEqual({ newdata: { data: "value" } });
      expect(Main.prototype.getOtherStuff).toBeCalledTimes(1);
      expect(tpinstance.createThing).toHaveBeenCalled();
      expect(tpinstance.createThing().nestedFunction).toBeCalledWith(
        null,
        callback
      );
    });
  });
});

Unit test result with coverage report:

 PASS  src/stackoverflow/59150545/index.spec.js
  Main
    #getStuff
      ✓ should pass (12ms)

  console.log src/stackoverflow/59150545/index.spec.js:31
    Promise { <pending> }

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |    84.62 |      100 |    71.43 |    83.33 |                   |
 index.js |    84.62 |      100 |    71.43 |    83.33 |             18,19 |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.529s

Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59150545

Post a Comment for "How Do I Call A Resolve In The Callback Of A Mocked Function?"