Search code examples
google-cloud-platformgoogle-cloud-functionsgoogle-cloud-storagemocha.jssinon

How to mock the response for below code using sinon , mocha chai


Can anyone help me in writing one sample test scenarios?

storage is a library (google Cloud) finally below the line of code will return me an array consist of filename and Date.

function abc(){
   const files = [];
   files = await storage.bucket(bucketName).getFiles();
   return files;
}

Solution

  • Here is the unit test solution:

    index.ts:

    import { Storage } from "@google-cloud/storage";
    const storage = new Storage();
    
    export async function abc() {
      const bucketName = "xxx-dev";
      const files = await storage.bucket(bucketName).getFiles();
      return files;
    }
    
    export async function xyz(res) {
      const bucketName = "xxx-dev";
      return storage
        .bucket(bucketName)
        .file(res.fileName)
        .createReadStream();
    }
    

    index.spec.ts:

    import { abc, xyz } from "./";
    import { Storage } from "@google-cloud/storage";
    import sinon from "sinon";
    import { expect } from "chai";
    
    describe("59373281", () => {
      afterEach(() => {
        sinon.restore();
      });
      it("abc should pass", async () => {
        const getFilesStub = sinon.stub().resolves(["file1", "file2"]);
        const bucketStub = sinon.stub(Storage.prototype, "bucket").callsFake(() => {
          return { getFiles: getFilesStub } as any;
        });
        const actual = await abc();
        expect(actual).to.be.deep.eq(["file1", "file2"]);
        sinon.assert.calledWith(bucketStub, "xxx-dev");
        sinon.assert.calledOnce(getFilesStub);
      });
    
      it("xyz should pass", async () => {
        const fileStub = sinon.stub().returnsThis();
        const createReadStreamStub = sinon.stub();
        const bucketStub = sinon.stub(Storage.prototype, "bucket").callsFake(() => {
          return {
            file: fileStub,
            createReadStream: createReadStreamStub,
          } as any;
        });
        const mRes = { fileName: "jestjs.pdf" };
        await xyz(mRes);
        sinon.assert.calledWith(bucketStub, "xxx-dev");
        sinon.assert.calledWith(fileStub, "jestjs.pdf");
        sinon.assert.calledOnce(createReadStreamStub);
      });
    });
    

    Unit test result with 100% coverage:

      59373281
        ✓ abc should pass
        ✓ xyz should pass
    
    
      2 passing (46ms)
    
    ---------------|----------|----------|----------|----------|-------------------|
    File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
    ---------------|----------|----------|----------|----------|-------------------|
    All files      |      100 |      100 |      100 |      100 |                   |
     index.spec.ts |      100 |      100 |      100 |      100 |                   |
     index.ts      |      100 |      100 |      100 |      100 |                   |
    ---------------|----------|----------|----------|----------|-------------------|
    

    package versions:

    "@google-cloud/storage": "^4.1.3",
    "sinon": "^7.5.0",
    "mocha": "^6.2.2",
    "chai": "^4.2.0",
    

    Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/59373281