Skip to content

Stubs

Stubs

Use ks.stub() to create mock functions.

ks.stub(Object, MethodName, MockReturnValue);
import Kensa from 'kensajs';
import exampleModuleStub from './lib/example';
import { add } from './lib/add';
let ks = Kensa();
// Normally
// Inside add function, exampleModuleStub.testFunction is called
ks.test({
title: 'Test Item',
input: () => add(1, 1),
expect: 2,
});
// Stubbing testFunction to return 5
ks.stub(exampleModuleStub, 'testFunction', 5);
ks.test({
title: 'Test Item',
input: () => add(1, 1),
expect: 5,
});
// Clearing stubs (if not cleared, it carries over to the next test)
// If changing the return value, clear it first then re-stub.
ks.clearStub();
// Stubbing testFunction to return 6
ks.stub(exampleModuleStub, 'testFunction', 6);
// Test with a simple value
ks.test({
title: '1,Simple Value Test',
input: () => add(1, 1),
expect: 6,
});
ks.run();
実行結果
Terminal window
Test Item
Test Item
Test Item
TOTAL: 3, PASS: 3, FAIL: 0