1

I need to mock 'mkdirp-promise' node module which exposes a constructor function as below

mkdirpPromise(dirPath)
        .then(() => {
            console.log('ABCDEFGH');
            resolve(dirPath);
        })
        .catch((error) => {
            console.log('HeABCDEFGHre');
            const details = error.message;
            const err = customError.failed_create_downloads_directory()
                .withDetails(details);
            reject(err);
        });

Im able to mock it using proxiquire as below for the first time:-

let mkdirpPromiseMock = sinon.stub().rejects();

 const sthreeDownloadMock =
    proxyquire('./../../modules/sThreeDownload', {
        joi: joiMock,
        fs: fsMock,
        '@monotype/core-error': {
            errors: {
                ApiError: customErrorMock,
            },
        },
        'aws-sdk': awsSDK,
        'mkdirp-promise': mkdirpPromiseMock,
        path: pathMock,
    });

Now i want to override mkdirpPromiseMock in 2nd test case with

mkdirpPromiseMock = sinon.stub().resolves();

which im not able to. Any help is appreciated.

iAviator
  • 1,310
  • 13
  • 31
  • That should have worked fine, but "which im not able to" is not really providing any details with which we could help you. A simplified case that is runnable and shows the problem would help – oligofren Mar 28 '18 at 14:01

1 Answers1

0

Proxyquire is not compatible with jest.

You need to use a mocking library like rewiremock.

Please have a look at this answer which goes into detail.

REPL example

Menelaos Kotsollaris
  • 5,776
  • 9
  • 54
  • 68