avajs/ava · error · AssertionError

The second argument to `${assertion}` must be an expectation

Error message

The second argument to `${assertion}` must be an expectation object, `null` or `undefined`

What it means

validateExpectations guard: the second argument to the assertion was provided, is not null/undefined, but is not a valid expectations object — it is a function, string, RegExp, non-object, array, or an object with no keys. The input at fault is the expectations argument of throws-style assertions.

Source

Thrown at lib/assert.js:93

function validateExpectations(assertion, expectations, numberArgs) { // eslint-disable-line complexity
	if (numberArgs === 1 || expectations === null || expectations === undefined) {
		if (expectations === null) {
			throw new AssertionError(`The second argument to \`${assertion}\` must be an expectation object or \`undefined\``, {
				assertion,
				formattedDetails: [formatWithLabel('Called with:', expectations)],
			});
		}

		expectations = {};
	} else if (
		typeof expectations === 'function'
		|| typeof expectations === 'string'
		|| expectations instanceof RegExp
		|| typeof expectations !== 'object'
		|| Array.isArray(expectations)
		|| Object.keys(expectations).length === 0
	) {
		throw new AssertionError(`The second argument to \`${assertion}\` must be an expectation object, \`null\` or \`undefined\``, {
			assertion,
			formattedDetails: [formatWithLabel('Called with:', expectations)],
		});
	} else {
		if (Object.hasOwn(expectations, 'instanceOf') && typeof expectations.instanceOf !== 'function') {
			throw new AssertionError(`The \`instanceOf\` property of the second argument to \`${assertion}\` must be a function`, {
				assertion,
				formattedDetails: [formatWithLabel('Called with:', expectations)],
			});
		}

		if (
			Object.hasOwn(expectations, 'message')
			&& typeof expectations.message !== 'string'
			&& !(expectations.message instanceof RegExp)
			&& !(typeof expectations.message === 'function')
		) {
			throw new AssertionError(`The \`message\` property of the second argument to \`${assertion}\` must be a string, regular expression or a function`, {

View on GitHub (pinned to bbfd946322)

Solutions

  1. Wrap the expectation in an object: {message: '...'} rather than passing a bare string/RegExp
  2. Include at least one known key (instanceOf, message, name, code, is, or anyOf)
  3. Do not pass arrays or functions as the expectations argument
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/assert.js:93 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of avajs/ava@bbfd946322 (2026-09-02). Data as JSON: /api/errors/c76c6ed95147e0f7. Report an issue: GitHub.