Maybe i don't understand the documentation, my test is not working.
I'll test an expected error from a function:
// file_to_test.js
...
myFunctionToTest: function(param1, param2 = "AA-BB") {
const idx1 = param2.indexOf("AA");
if(idx1 === -1) {
throw new Error('test message error');
}
}
here comes the test:
// test.js
sap.ui.define(["path/to/my/script"], function(myScriptToTest) {
...
QUnit.test('should thrown error', assert => {
assert.throws(
function() {
myScriptToTest.myFunctionToTest('blabla', 'blubblub'); // test is broken at this line
},
function(error) {
return error.toString() === 'test message error';
},
'Error thrown'
}
})
})
The return (Browser-)output:
1. Error thrown
Expected: function( a ){
[code]
}
Result: Error("test message error")
Diff: function( a ){
[code]
}Error("test message error")
Source: ...
How to setup the test correctly?
Your QUnit code looks fine, but there is a mistake in how you've written your JavaScript logic.
It is actually true that
error.toString()does not equaltest message error. What you're looking for iserror.messageand noterror.toString().Consider the following:
See also: