new for unit testing and the concept of spies, stubs and layouts.
I want to test the verify method from password.js from the code below, but I cannot stub use the hash function in the test file.
Since verify uses the hash function, and the hash function is exported, I have to stub use the hash function to return a fixed response, rather than actually calling hash . Since I am not trying to test the hash function.
Problem: The created stub for the hash function is not called when testing verify .
Side question 1: Should I focus on checking the logic of the function itself, and not on other called functions?
The main question: (answers) How to drown out a module function that is called in the same module?
Side question 2:. How can I perform stubbing hash if it is not exported, but remains only inside the module?
the code
password.js
function hash (password, salt) {
password.test.js
import test from 'ava'; import sinon from 'sinon'; import passwordModule from './password'; test('verify - should verify password', function * (t) { const password = 'test-password'; const salt = null; const hash = 'my-hash'; const hashStub = sinon.stub(passwordModule, 'hash', (password, salt) => Promise.resolve({hash, salt})); const verified = yield passwordModule.verify(password, hash, salt); t.true(verified); hashStub.restore(); });
Customization
- Node v6.2.0
- Ava v0.15.2
- Sinon "v1.17.4
Tests and modules are broadcast using babel. But the module does not use the export of ES6 modules, as it is used in node env without forwarding.
I translate all the code during testing so that it becomes a future proof, and env preservation can be used both for the external interface and for the backend code, where the translation code is full.