Search code examples
node.jstypescriptmocha.jschaisinon

How do I stub a module method's callback with sinon?


I'm using the mv module to move files, an older module that still gets a lot of downloads and has been helpful in dealing with moving files between docker volumes and also outside of docker. I created a method to treat mv method as a promise.

When unit testing, I'm struggling to mock mv when used within a method.

import mv from 'mv';

export class CmdUtils {
  /**
   * Move a file to a new location. Using mv, this will attempt a mv command, however
   * this could fail with docker volumes and will fallback to the mv module streaming
   * data to the new location as needed.
   * @param filePath - Full path to file
   * @param destinationFilePath - Full path to file's new destination
   */
  public static moveFile = (filePath: string, destinationFilePath: string): Promise<void> => {
    return new Promise((resolve, reject) => {
      mv(filePath, destinationFilePath, err => {
        if (!err) {
          resolve();
        } else {
          console.error('moveFile err', err);
          reject(err);
        }
      });
    });
  }
}

Here's a starting test with gaps:

import mv from 'mv';
import { CmdUtils } from './cmd-utils';

describe("CmdUtils: moveFile", () => {
  it("should successfully move a file using mv module", async () => {
    const sandbox = sinon.createSandbox();
    try {
      const moveFromPath = '/full/path/to/file.jpg';
      const moveToPath = '/full/path/to/destination/file.jpg';

      // Starting attempt at a replacement callback
      const mvCallback = (err: Error) => {
        if (!err) {
          Promise.resolve();
        } else {
          console.error('moveFile err', err.message);
          Promise.reject(err);
        }
      }

      // Stub mv
      sandbox.stub(mv)
      // What am I missing here?

      // Move file
      await CmdUtils.moveFile(moveFromPath, moveToPath);

      // expect no errors... (will revisit this)
    } finally {
      sandbox.restore();
    }
  });
});

How do I mock a successful file move? I'm seeing similar questions, yet not finding an answer just yet.


Solution

  • class CmdUtil {
      static moveFileCb(resolve, reject, err) {
        if (!err) {
          resolve('RESOLVED');
        } else {
          reject(err);
        }
      }
    
      static moveFile() {
        return new Promise((resolve, reject) => {
          mv('test', 'test', CmdUtil.moveFileCb.bind(this, resolve, reject))
        })
      }
    }
    

    then the test could be something similar to what you have where you now stub moveFileCb. With that stub, you can check/assert what the callback was called with, how many times it was called, etc.

    Side Note: I didn't write a test using bind. That might throw a wrench into the solution.