如何使用proxyquire

时间:2018-02-27 16:59:41

标签: node.js sinon stub sinon-chai proxyquire

我需要嘲笑&mkdirp-promise'暴露构造函数的节点模块,如下所示

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);
        });

我能够首次使用proxiquire模拟它: -

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,
    });

现在我想用第二个测试用例

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

我无法做到。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:-1)

Proxyquirejest不兼容。

您需要使用rewiremock之类的模拟库。

请详细说明look at this answer

REPL example