Skip to content

Assertions rules

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.

Assert the value that the code under test returns. Compare a new object or array with a deep equality matcher.

expect(true).toBe(true) reports constant. Write expect(isReady()).toBe(true).

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.

Replace .then() with .should().

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); }).

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.

Finish the assertion with the matcher that checks the value, and call it.

expect(total).to.equal; reports call. Write expect(total).to.equal(3);.

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.

Pass the value that the code computes first and the literal it should equal second.

assert.equal(3, total) reports swap. Write assert.equal(total, 3).

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.

Compare values of the same type: convert one side, such as String(count), or assert the value that the code returns.

expect(count).toBe("3") with a number count reports incompatible. Write expect(count).toBe(3).

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.

Move the assertion into a test, or into a hook such as beforeEach.

describe("cart", () => { expect(cart.items).toHaveLength(0); }) reports misplaced. Move the assertion into an it callback.

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.

Compare the value with the expected result, written out or computed another way.

expect(total).to.equal(total); reports duplicate. Write expect(total).to.equal(3);.

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.

Assert the one result that the code must produce, such as .not.throw() without an argument or the exact value of a property.

expect(run).to.not.throw(TypeError); reports uncertain. Write expect(run).to.not.throw();.

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.

Apply the suggestion, or write the matcher that the message names.

expect(items.length).toBe(2) reports length. Write expect(items).toHaveLength(2).

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.

Name the error type or its message in the assertion, and pass the caught error to done(error) or check it.

expect(run).to.throw(); reports errorType. Write expect(run).to.throw(RangeError, "negative amount");.

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.

Assert the result of the code under test.

it("saves", () => { save(item); }) reports missing. Add expect(load(item.id)).to.deep.equal(item).

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.

Wrap the code in a function, such as expect(() => parse(text)).toThrow(). For an async function, assert the rejection: await expect(load()).rejects.toThrow().

expect(parse(text)).toThrow() reports value. Write expect(() => parse(text)).toThrow().