avajs / ava

Node.js test runner that lets you develop with confidence 🚀
MIT License
20.73k stars 1.4k forks source link

Allow regexes in `t.like` #3309

Open tommy-mitchell opened 7 months ago

tommy-mitchell commented 7 months ago

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: 'sindresorhus@gmail.com',
});
novemberborn commented 7 months ago

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?

Saberian1992 commented 6 months ago

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?

tommy-mitchell commented 2 months ago

Maybe:

novemberborn commented 2 months ago

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.