ava
ava copied to clipboard
Allow regexes in `t.like`
The message property in t.throws is really ergonomic - it can be a string, RegExp, or even a function. t.like should be just as ergonomic with strings:
const output = foo();
t.like(output, {
name: 'foo',
source: /foo comes from/,
});
A motivating example is trying to convert this test from npm-user to use t.like:
const user = await npmUser('sindresorhus');
t.like(user, {
name: 'Sindre Sorhus',
avatar: /npm-avatar/,
email: '[email protected]',
});
Currently like is a partial deepEqual, selecting from the actual (first argument) just those properties that exist in the expected (second argument). How would we do that if expected contains a regular expression value?
Currently
likeis a partialdeepEqual, selecting from theactual(first argument) just those properties that exist in theexpected(second argument). How would we do that ifexpectedcontains a regular expression value?
Maybe:
-
If
expected.propis aRegExpandactual.propis astring:-
Check if
actual.propmatchesexpected.prop -
If
false, fail the test. The current descriptors (string vs regex) can stay as-is:
- str: 'string', + str: /string/, -
-
If
expected.propis aRegExpandactual.propis aRegExp:- No change. Only pass if the two regexes are the same.
-
If
expected.propis astringandactual.propis aRegExp:- No change. The test should still fail.
I agree that this looks useful, and your logic makes sense, but I don't immediately see how this could be implemented. Don't let that stop you though 😉
It may also technically be a breaking change, in that if you've written an existing assertion expecting a regular expression, and your actual value changes to a string, that happens to match… the test passes when it shouldn't. OK that seems unlikely.