2023-05-03 11:06:20 +02:00
== Why is this an issue?
2020-12-21 15:38:52 +01:00
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.
2021-02-02 15:02:10 +01:00
2020-12-21 15:38:52 +01:00
This rule raises an issue when the "expected" argument of an assertion function is a hard-coded value and the "actual" argument is not.
2021-02-02 15:02:10 +01:00
2020-12-21 15:38:52 +01:00
This rule currently supports Chai assertions.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-12-21 15:38:52 +01:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2020-12-21 15:38:52 +01:00
----
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
});
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-12-21 15:38:52 +01:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2020-12-21 15:38:52 +01:00
----
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);
});
----
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== 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
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]