Skip to content

Rule proposal: Put actual values first and expected values second in assertion #159

Closed
@jfmengels

Description

@jfmengels

Issuehunt badges

When you write assertions, like t(a, b), the first argument is the value to evaluate, and the second is the expected value to compare it to. But you can actually mix the two arguments and everything would work as expected, except that the error messages will be a bit more confusing because it will swap the expected and received values.

I see it every so often that the arguments are swapped. Not very often, but it can't hurt to have it IMO.

I propose a rule that requires you to put your expressions as the first argument, rather than on the right. Or in other words, as this is probably will be how we'll write the rule, put static expressions on the right.

Invalid

t.is(1, expression);
t.deepEqual({}, expression);
t.true(1 === foo); // Also handle this? Probably, as power-assert considers `1` as the value

Valid

t.is(expression, 1);
t.deepEqual(expression, {});
t.true(foo === 1);
t.is(expression, otherExpression);

Implementation side-note: We could probably use this function to help find out whether an expression is static or not. (I haven't yet published it, but I can do it soonish).

As for the name: no-expected-left, put-expected-right? :/


Note: This issue has a bounty, so it's expected that you are an experienced programmer and that you give it your best effort if you intend to tackle this. Don't forget, if applicable, to add tests, docs (double-check for typos). And don't be sloppy. Review your own diff multiple times and try to find ways to improve and simplify your code. Instead of asking too many questions, present solutions. The point of an issue bounty is to reduce my workload, not give me more. Include a 🦄 in your PR description to indicate that you've read this. Thanks for helping out 🙌 - @sindresorhus


IssueHunt Summary

ninevra ninevra has been rewarded.

Backers (Total: $40.00)

Submitted pull Requests


Tips

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions