Как правило, от вас ожидают насмешки вызовов API для используемых вами модулей.
Чтобы привести пример с использованием node-printer
, в jasmine
:
const printer = require('node-printer');
const myModule = require('module i am testing');
describe('printSomething', function () {
it('prints something and resolves when it completes', function (done) {
// Mock the printDirect method on node-printer, to immediately
// call the success callback. You might need to mock additional
// methods if you make multiple API calls in one method.
spyOn(printer, 'printDirect').andCallFake(options => {
options.success(12345);
});
myModule.printSomething('blah').then(result => {
// To be sure your module is calling the module with the expected
// arguments, you can check the actual arguments passed.
expect(printer.printDirect.calls.allArgs()).toEqual([[{
data: 'the exact data',
printer: 'PRINTERNAME',
success: jasmine.any(Function)
}]]);
done();
}).catch(done.fail);
});
});
Вы можете создать аналогичные примеры практически в любой тестовой среде (jest, mocha + sinon и т. Д.).