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
like
is a partialdeepEqual
, selecting from theactual
(first argument) just those properties that exist in theexpected
(second argument). How would we do that ifexpected
contains a regular expression value?
Maybe:
-
If
expected.prop
is aRegExp
andactual.prop
is astring
:-
Check if
actual.prop
matchesexpected.prop
-
If
false
, fail the test. The current descriptors (string vs regex) can stay as-is:
- str: 'string', + str: /string/,
-
-
If
expected.prop
is aRegExp
andactual.prop
is aRegExp
:- No change. Only pass if the two regexes are the same.
-
If
expected.prop
is astring
andactual.prop
is 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.