I have a function that tries to parse a parameter as a JSON object. If this fails, backup is used instead.
parsing-code.js
function parseCode(code) { try { usingJSONFallback(code); } catch() { usingStringFallback(code); } } function usingJSONFallback(code) { JSON.parse(code);
main.js
//Some code... parseCode('hello world!');
I do not see any problems in this code. However, when I try to add some unit tests (using Jasmine 2.3) for the "catch" case, Jasmine catches the JSON parsing error on its own and aborts the test:
For example, for the Jasmine test, for example:
describe('parseCode', function() { it('Parses a string', function() { var code = 'My code without JSON'; expect(parseCode(code)).toThrow(); }); });
or even a test like:
describe('usingJSONFallback', function() { it('Throw an error if there is a string', function() { var code = 'My code without JSON'; expect(usingJSONFallback(code)).toThrow(); }); });
In both cases, the test fails and returns:
SyntaxError: Unable to parse JSON string
I read about throwing controlled exceptions with throw Error(...) , but finally this is not suitable for my case. Any suggestions on how to use jasmine in this case?
source share