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 or `undefined`

What it means

validateExpectations guard: t.throws/t.throwsAsync was called with an explicit null as its second argument, which is indistinguishable from 'no expectations' — only undefined is accepted for that case. The input at fault is the second (expectations) argument being null.

Source

Thrown at lib/assert.js:78

	return new AssertionError('The assertion message must be a string', {
		assertion,
		formattedDetails: [formatWithLabel('Called with:', message)],
	});
}

export function getAssertionStack(constructorOpt = getAssertionStack) {
	const {stackTraceLimit: limitBefore} = Error;
	Error.stackTraceLimit = Number.POSITIVE_INFINITY;
	const temporary = {};
	Error.captureStackTrace(temporary, constructorOpt);
	Error.stackTraceLimit = limitBefore;
	return temporary.stack;
}

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)],
		});

View on GitHub (pinned to bbfd946322)

Solutions

  1. Omit the second argument instead of passing null when there are no expectations
  2. Pass undefined explicitly if the call site requires an argument
  3. Pass a real expectations object, e.g. {instanceOf: Error}
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/assert.js:78 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/ba83fd9e7bc42d3b. Report an issue: GitHub.