Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

78 lines
1.9 KiB
Plaintext

== Why is this an issue?
Many assertion functions have specific parameters for the expected and actual values. Swap them, and your test will still have the same outcome (succeed/fail when it should) but the error messages will be confusing.
This rule raises an issue when the "expected" argument of an assertion function is a hard-coded value and the "actual" argument is not.
This rule currently supports Chai assertions.
=== Noncompliant code example
[source,javascript]
----
const assert = require('chai').assert;
const expect = require('chai').expect;
const should = require('chai').should();
it("inverts arguments", function() {
assert.equal(42, aNumber); // Noncompliant
expect(42).to.equal(aNumber); // Noncompliant
should.fail(42, aNumber); // Noncompliant
});
----
=== Compliant solution
[source,javascript]
----
const assert = require('chai').assert;
const expect = require('chai').expect;
const should = require('chai').should();
it("inverts arguments", function() {
assert.equal(aNumber, 42);
expect(aNumber).to.equal(42);
should.fail(aNumber, 42);
});
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
If a Chai function gets actual and expected values as arguments:
* Swap these 2 arguments so they are in the correct order: actual value, expected value.
If expect(actual).someMethod(expected) is used:
* Swap this argument with the one given to "expect".
=== Highlighting
If a Chai function gets actual and expected values as arguments:
* Primary: second argument to swap
* Secondary: first argument to swap
** message: 'Other argument to swap.'
If expect(actual).someMethod(expected) is used:
* Primary: The argument of the assertion function (ex: "equal")
* Secondary: The argument given to "expect"
** no message
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]