Closed pszabop closed 4 years ago
t.doesNotThrow
needs a function
test('ensure JSON.parse can be tested for throwing or not', async function(t) {
t.doesNotThrow(function noThrow() {
JSON.parse('{}')
}, undefined, 'JSON.parse should not throw on an empty brackets');
});
JSON.parse()
is a function according to console.log(typeof JSON.parse)
The odd part is intentionally giving JSON.parse()
bad input does throw, which means it was executed.
The workaround works, I added this to my tape hello world
test where I document workarounds for interesting test cases.
/*
* @see https://github.com/substack/tape/issues/512
*
*/
test('ensure JSON.parse can be tested to not throw', async function(t) {
t.doesNotThrow(function() { JSON.parse('{}') }, undefined, 'JSON.parse should not throw on an empty brackets');
});
test('ensure JSON.parse can be tested to throw', async function(t) {
t.throws(function () { JSON.parse('') }, undefined, 'JSON.parse should throw on an empty string');
});
@pszabop that's not a workaround, that's what's necessary. What you pass to doNotThrow
is a function that may or may not throw - JSON.parse
is a function, but that's not what you passed. You passed JSON.parse(something)
, which is either an object, or, it's already thrown an exception.
This is true in every single test runner, period - a "throw" or "does not throw" assertion can only be tested against the only thing in the language that can be later invoked (to throw or not), a function.
The following test fails complaining that JSON.parse is not a function. I'm quite sure JSON.parse is a function, I printed out
typeof JSON.parse
and gotfunction
.Yet if I put illegal JSON in the test executes the JSON.parse and fails as expected.
How does one test that some result is parseable JSON?
node version: v8.11.1 tape version: 5.0.0