Вам нужно использовать модуль proxyquire , чтобы заглушить модуль adm-zip
.
Например,
index.js
:
const AdmZip = require('adm-zip');
async function main() {
const filePath = 'some file path';
const outputPath = './dist';
const zip = new AdmZip(filePath);
const response = await zip.extractAllTo(outputPath, true);
}
module.exports = main;
index.test.js
:
const sinon = require('sinon');
const proxyquire = require('proxyquire');
describe('60595390', () => {
it('should extract all', async () => {
const admZipInstance = {
extractAllTo: sinon.stub(),
};
const admZipStub = sinon.stub().callsFake(() => admZipInstance);
const main = proxyquire('./', {
'adm-zip': admZipStub,
});
await main();
sinon.assert.calledWithExactly(admZipStub, 'some file path');
sinon.assert.calledWithExactly(admZipInstance.extractAllTo, './dist', true);
});
});
Результаты модульного теста со 100% покрытием:
60595390
✓ should extract all (2951ms)
1 passing (3s)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------