Assertions rules
no-constant-assertions
Section titled “no-constant-assertions”Reports an assertion whose result does not depend on the code under test, such as an equality of two constants or an identity check against an object created in place.
On in the recommended and all configs.
An assertion that always passes, or always fails for a reason the test cannot change, checks nothing about the code.
What to do
Section titled “What to do”Assert the value that the code under test returns. Compare a new object or array with a deep equality matcher.
Example
Section titled “Example”expect(true).toBe(true) reports constant. Write expect(isReady()).toBe(true).
no-cypress-then-assertions
Section titled “no-cypress-then-assertions”Reports a .then() callback on a chain of Cypress DOM queries, such as cy.get(selector), when the callback only asserts on the element that it receives.
On in the recommended and all configs.
Cypress runs a .then() callback once, so its assertions fail when the page has not settled yet. A .should() callback runs again until its assertions pass or the command times out.
What to do
Section titled “What to do”Replace .then() with .should().
Example
Section titled “Example”cy.get("li").then(($items) => { expect($items).to.have.length(3); }) reports retry. Write cy.get("li").should(($items) => { expect($items).to.have.length(3); }).
no-incomplete-assertions
Section titled “no-incomplete-assertions”Reports a statement that starts an assertion and stops before it asserts, such as expect(value) alone, or a chain of expect, assert, or should that ends on to, be, or an uncalled matcher.
On in the recommended and all configs.
An assertion that is never called checks nothing, and the test passes whatever the value is.
What to do
Section titled “What to do”Finish the assertion with the matcher that checks the value, and call it.
Example
Section titled “Example”expect(total).to.equal; reports call. Write expect(total).to.equal(3);.
no-inverted-assertions
Section titled “no-inverted-assertions”Reports an assertion inside an it, specify, or test case whose actual value is a literal while its expected value is not: assert.equal, strictEqual, deepEqual, closeTo, and their negations, expect(actual).to.equal, eql, and closeTo, and fail on assert, expect, and should.
On in the recommended and all configs.
A failure message names the expected and the actual value. With the arguments swapped, it shows the code result as the expected value and misleads the reader.
What to do
Section titled “What to do”Pass the value that the code computes first and the literal it should equal second.
Example
Section titled “Example”assert.equal(3, total) reports swap. Write assert.equal(total, 3).
no-mismatched-assertion-types
Section titled “no-mismatched-assertion-types”Reports a strict or deep equality assertion whose two values have primitive types that never match, such as a string and a number.
On in the recommended and all configs. It reports nothing without the type information of a TypeScript project.
Values of different primitive types never compare equal, so the assertion always fails, or always passes when negated, whatever the code under test does.
What to do
Section titled “What to do”Compare values of the same type: convert one side, such as String(count), or assert the value that the code returns.
Example
Section titled “Example”expect(count).toBe("3") with a number count reports incompatible. Write expect(count).toBe(3).
no-misplaced-assertions
Section titled “no-misplaced-assertions”Reports an assertion statement that runs in the callback of a suite such as describe, or at the top of a file that declares tests, instead of inside a test.
On in the recommended and all configs.
A suite callback runs while the runner collects the tests, and the top of a file runs when the runner loads it, so a failure there aborts the whole file instead of failing one test, or runs before the setup that the tests need.
What to do
Section titled “What to do”Move the assertion into a test, or into a hook such as beforeEach.
Example
Section titled “Example”describe("cart", () => { expect(cart.items).toHaveLength(0); }) reports misplaced. Move the assertion into an it callback.
no-self-comparing-assertions
Section titled “no-self-comparing-assertions”Reports an argument of a chai expect, should, or assert statement that repeats another argument of the same assertion token for token, in files that import chai. Literals are not reported.
On in the recommended and all configs.
An assertion that compares a value with itself always passes, so it checks nothing about the code.
What to do
Section titled “What to do”Compare the value with the expected result, written out or computed another way.
Example
Section titled “Example”expect(total).to.equal(total); reports duplicate. Write expect(total).to.equal(3);.
no-uncertain-chai-assertions
Section titled “no-uncertain-chai-assertions”Reports a chai expect or should chain that can pass for more than one reason, in files that import chai: .not.throw() with an argument, .not.include({...}), .not.property() and .not.ownPropertyDescriptor() with a value, .not.members(), .not.increase(), .not.decrease(), .not.by(), .not.finite, and .change().by().
On in the recommended and all configs.
A negated assertion with an argument also passes when the thing it names is missing for another reason. .not.throw(TypeError) passes when the code throws a different error.
What to do
Section titled “What to do”Assert the one result that the code must produce, such as .not.throw() without an argument or the exact value of a property.
Example
Section titled “Example”expect(run).to.not.throw(TypeError); reports uncertain. Write expect(run).to.not.throw();.
prefer-matcher-assertions
Section titled “prefer-matcher-assertions”Reports an equality assertion that a dedicated matcher expresses, such as a comparison with null, NaN, or a length, a boolean expression compared with true, or an awaited Playwright locator state such as isVisible().
On in the all config.
A dedicated matcher reads like the intent, and its failure message shows the value it received instead of expected true, received false.
What to do
Section titled “What to do”Apply the suggestion, or write the matcher that the message names.
Example
Section titled “Example”expect(items.length).toBe(2) reports length. Write expect(items).toHaveLength(2).
require-error-checks
Section titled “require-error-checks”Reports, inside a Mocha describe, it, or hook: a chai expect(...).to.throw() or should.throw() that names no error or only Error, and a catch parameter that nothing reads in a block that calls done.
On in the recommended and all configs.
Any error passes an assertion that expects just an error, so a test written for one failure also passes on a typo or a crash.
What to do
Section titled “What to do”Name the error type or its message in the assertion, and pass the caught error to done(error) or check it.
Example
Section titled “Example”expect(run).to.throw(); reports errorType. Write expect(run).to.throw(RangeError, "negative amount");.
require-test-assertions
Section titled “require-test-assertions”Reports a test, an it, specify, or test call with a callback, that runs no assertion, through the functions of the file that it calls and, with type information, the functions that it imports.
On in the recommended and all configs.
A test without an assertion passes whatever the code does, so it checks only that the code does not throw.
What to do
Section titled “What to do”Assert the result of the code under test.
Example
Section titled “Example”it("saves", () => { save(item); }) reports missing. Add expect(load(item.id)).to.deep.equal(item).
valid-throw-assertions
Section titled “valid-throw-assertions”Reports a toThrow or toThrowError assertion whose expect value is an async function, or, with type information, a value that is not a function, such as the result of a call.
On in the recommended and all configs.
The matcher calls the function and catches what it throws while it runs. An async function returns a rejected promise instead of throwing, and a value that is not a function has already thrown, or never will, before the matcher runs.
What to do
Section titled “What to do”Wrap the code in a function, such as expect(() => parse(text)).toThrow(). For an async function, assert the rejection: await expect(load()).rejects.toThrow().
Example
Section titled “Example”expect(parse(text)).toThrow() reports value. Write expect(() => parse(text)).toThrow().