Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assert.rejects and assert.not.rejects methods for promises #132

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ export function throws(blk, exp, msg) {
}
}

export async function rejects(blk, exp, msg) {
if (!msg && typeof exp === 'string') {
msg = exp; exp = null;
}

try {
await blk();
assert(false, false, true, 'rejects', false, 'Expected promise to reject', msg);
} catch (err) {
if (err instanceof Assertion) throw err;

if (typeof exp === 'function') {
assert(exp(err), false, true, 'rejects', false, 'Expected promise to reject matching exception', msg);
} else if (exp instanceof RegExp) {
assert(exp.test(err.message), false, true, 'rejects', false, `Expected promise to reject matching exception \`${String(exp)}\` pattern`, msg);
}
}
}

// ---

export function not(val, msg) {
Expand Down Expand Up @@ -158,3 +177,21 @@ not.throws = function (blk, exp, msg) {
}
}
}

not.rejects = async function (blk, exp, msg) {
if (!msg && typeof exp === 'string') {
msg = exp; exp = null;
}

try {
await blk();
} catch (err) {
if (typeof exp === 'function') {
assert(!exp(err), true, false, 'not.rejects', false, 'Expected promise not to reject matching exception', msg);
} else if (exp instanceof RegExp) {
assert(!exp.test(err.message), true, false, 'not.rejects', false, `Expected function not to reject promise exception matching \`${String(exp)}\` pattern`, msg);
} else if (!exp) {
assert(false, true, false, 'not.rejects', false, 'Expected promise not to reject', msg);
}
}
}
171 changes: 170 additions & 1 deletion test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ throws('should be a function', () => {
throws('should throw if function does not throw Error :: generic', () => {
try {
$.throws(() => 123);
assert.unreachable('Function threw when it shouldn’t have');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.unreachable('Function threw when it shouldn’t have');

} catch (err) {
assert.is(err.message, 'Expected function to throw');
isError(err, '', false, true, 'throws', false); // no details (true vs false)
Expand All @@ -375,6 +376,7 @@ throws('should throw if function does not throw Error :: generic', () => {
throws('should throw if function does not throw matching Error :: RegExp', () => {
try {
$.throws(() => { throw new Error('hello') }, /world/);
assert.unreachable('Function threw correct pattern when it should have thrown incorrect one');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.unreachable('Function threw correct pattern when it should have thrown incorrect one');

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this in because I was confused when I changed the test to $.throws(() => { throw new Error('world') }, /world/); and it still passed. Wouldn’t the same thing happen if there was a regression in throws itself that would go unnoticed without this check?

Copy link
Author

@aral aral Aug 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I think this is – and the one above; same issue – are the only two I’m confused on. Accepted all other changes and happy to go with what you think is best for on these.)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion helper is saying: given X function, an error (a) should be thrown and (b) the thrown error should match {this condition}. The condition can be defined via a function, a RegExp, or a string.

So this test has X function, which happens to throw new Error('hello'), and the assertion is saying X must throw an Error that matches the /world/ pattern.

In the original test, this fails (obv because "hello" does not match /world) and so the assertions within the catch block run, and correctly find that the "Expected function to throw exception matching /world/ pattern" message appears, among others.

When you change the X to be:

$.throws(() => { throw new Error('world') }, /world/);

This doesnt fail the assertion, because "world" does match the /world pattern. The assertion passed. So in this case, the checks within the catch block never run.

Testing negated fail conditions can be a trip, haha

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda feel like it could be helpful to make that work via

$.throws(() => throw new Error('world') }).matches(new Error('world'));

which maybe that doesn't make sense.. but it'd be nice to be able to chain throws() with match() rather than having a regex over err.message 🤔

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks but the current API is much preferred and has prior art from many existing test runners and/or assertion libraries. It would also add a bunch of additional changes for the same (but more verbose) result.

} catch (err) {
assert.is(err.message, 'Expected function to throw exception matching `/world/` pattern');
isError(err, '', false, true, 'throws', false); // no details
Expand Down Expand Up @@ -418,6 +420,91 @@ throws.run();

// ---

const rejects = suite('rejects');

rejects('should be a function', () => {
assert.type($.rejects, 'function');
});

rejects('should throw if function does not reject Error :: generic', async () => {
try {
await $.rejects(() => {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), 1);
});
});
assert.unreachable('Promise rejected when it shouldn’t have');
} catch (err) {
assert.is(err.message, 'Expected promise to reject');
isError(err, '', false, true, 'rejects', false); // no details (true vs false)
}
});

rejects('should throw if function does not reject matching Error :: RegExp', async () => {
try {
await $.rejects(() => {
return new Promise((resolve, reject) => {
setTimeout(() => reject('hello'), 1)
});
}, /world/);
assert.unreachable('Promise rejected with correct pattern when it should have with incorrect one');
} catch (err) {
assert.is(err.message, 'Expected promise to reject matching exception `/world/` pattern');
isError(err, '', false, true, 'rejects', false); // no details
}
});

rejects('should throw if function does not reject matching Error :: Function', async () => {
try {
await $.rejects(
() => {
return new Promise((resolve, reject) => {
reject(new Error())
})
},
(err) => err.message.includes('foobar')
);
assert.unreachable('Promise should have rejected without matching exception');
} catch (err) {
assert.is(err.message, 'Expected promise to reject matching exception');
isError(err, '', false, true, 'rejects', false); // no details
}
});

rejects('should not reject if promise does reject Error :: generic', async () => {
await assert.not.rejects(
async () => await $.rejects(() => {
return new Promise ((resolve, reject) => setTimeout(() => reject(), 1));
})
, 'should not reject if function does reject Error :: generic');
});

rejects('should not reject if promise does reject matching Error :: RegExp', async () => {
await assert.not.rejects(
async () => await $.rejects(
() => {
return new Promise((resolve, reject) => setTimeout(() => reject(new Error('hello')), 1));
},
/hello/
)
);
});

rejects('should not reject if function does reject matching Error :: Function', async () => {
await assert.not.rejects(
async () => await $.rejects(
() => {
return new Promise((resolve, reject) => setTimeout(() => reject(new Error('foobar')), 1))
},
(err) => err.message.includes('foobar')
)
);
})

rejects.run();

// ---

const not = suite('not');

not('should be a function', () => {
Expand Down Expand Up @@ -793,7 +880,7 @@ notMatch.run();
const notThrows = suite('not.throws');

notThrows('should be a function', () => {
assert.type($.throws, 'function');
assert.type($.not.throws, 'function');
lukeed marked this conversation as resolved.
Show resolved Hide resolved
});

notThrows('should not throw if function does not throw Error :: generic', () => {
Expand Down Expand Up @@ -848,3 +935,85 @@ notThrows('should throw if function does throw matching Error :: Function', () =
});

notThrows.run();

// ---

const notRejects = suite('not.throws');

notRejects('should be a function', () => {
assert.type($.not.rejects, 'function');
});

notRejects('should not reject if function does not reject Error :: generic', async () => {
await assert.not.rejects(
async () => await $.not.rejects(() => new Promise((resolve, reject) => setTimeout(() => resolve(123), 1)))
);
});

notRejects('should not reject if function does not reject matching Error :: RegExp', async () => {
await assert.not.rejects(
async () => {
await $.not.rejects(() => {
return new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('hello')), 1);
})
}, /world/);
}
);
});

notRejects('should not reject if function does not reject matching Error :: Function', async () => {
await assert.not.rejects(async () => {
await $.not.rejects(() => {
return new Promise ((resolve, reject) => {
setTimeout(() => reject(new Error('hello')), 1);
})
}, (err) => err.message.includes('world'));
});
});

notRejects('should reject if function does reject Error :: generic', async () => {
try {
await $.not.rejects(() => {
return new Promise((resolve, reject) => {
setTimeout(() => reject(new Error()), 1);
});
});
assert.unreachable('Function resolved when we were expecting it to reject');
} catch (err) {
assert.is(err.message, 'Expected promise not to reject');
isError(err, '', true, false, 'not.rejects', false); // no details
}
});

notRejects('should reject if function does reject matching Error :: RegExp', async () => {
try {
await $.not.rejects(() => {
return new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('hello')), 1)
})
}, /hello/);
} catch (err) {
assert.is(err.message, 'Expected function not to reject promise exception matching `/hello/` pattern');
isError(err, '', true, false, 'not.rejects', false); // no details
}
});

notRejects('should reject if function does reject matching Error :: Function', async () => {
try {
await $.not.rejects(
() => {
return new Promise((resolve, reject) => {
setTimeout(() => reject(new Error()), 1)
})
},
(err) => err instanceof Error
);
assert.unreachable('Expected the function to reject with Error instance but it rejected with other value')
} catch (err) {
assert.is(err.message, 'Expected promise not to reject matching exception');
isError(err, '', true, false, 'not.rejects', false); // no details
}
});

notRejects.run();