The question is formulated this way as I'm able to step into tested code in IDE and the actual code runs as expected, but the test fails.
There's a javascript file attached to testing HTML with <script src="../example.js"></script>
and its content looks like:
function myFunc(myId) {
console.log(myId);
}
function someOtherFunc(arg) {
if (arg == 0)
myFunc("#id_0");
else
myFunc("#id_1");
}
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * *
* SECTION: exports needed by jest testing framework
* * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
if (typeof exports !== 'undefined') {
module.exports = {
myFunc,
someOtherFunc,
};
}
And the following is a simplified content of testing module example.test.js
with one of (many) unsuccessful ways to test that call:
const fs = require('fs');
const path = require('path');
const html = fs.readFileSync(path.resolve(__dirname, './example.html'), 'utf8');
const jquery = require('../../static/js/jquery-2.2.4.min.js');
window.$ = jquery;
window.jQuery = jquery;
const mockSend = jest.fn();
global.WebSocket = jest.fn();
global.WebSocket.mockImplementation(() => {
return {
send: mockSend,
};
});
const example = require('../example.js');
jest
.dontMock('fs');
beforeEach(() => {
document.documentElement.innerHTML = html.toString();
});
afterEach(() => {
jest.resetModules();
});
describe('someOtherFunc function', function () {
it('calls myFunc', function () {
const spyFunc = jest.spyOn(example, "myFunc");
const result = example.someOtherFunc(0);
expect(spyFunc).toHaveBeenCalledWith("#id_0")
});
});
I pasted this mockSend
part here as I've managed to test that successfully with expect(mockSend).toHaveBeenCalledWith("...");
. That send
method is also inside another function and I test a calling function in jest module.
However, I can't find a way to make this 'calls myFunc' test to pass.
Aucun commentaire:
Enregistrer un commentaire