avajs/ava · error · AssertionError

The `code` property of the second argument to `${assertion}`

Error message

The `code` property of the second argument to `${assertion}` must be a string or number

What it means

This is an input-validation failure inside validateExpectations, the guard that checks the expectations object supplied as the second argument to assertion helpers like t.throws() or t.notThrows(). It fires when expectations has a `code` key whose value is neither a string nor a number (e.g. a boolean, object, or symbol), leaving the assertion unable to compare it against an error's code. The assertion is rejected before execution — it is an API misuse rather than a test failure. Fix by passing expectations.code as a string or number.

Source

Thrown at lib/assert.js:125

			&& 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`, {
				assertion,
				formattedDetails: [formatWithLabel('Called with:', expectations)],
			});
		}

		if (Object.hasOwn(expectations, 'name') && typeof expectations.name !== 'string') {
			throw new AssertionError(`The \`name\` property of the second argument to \`${assertion}\` must be a string`, {
				assertion,
				formattedDetails: [formatWithLabel('Called with:', expectations)],
			});
		}

		if (Object.hasOwn(expectations, 'code') && typeof expectations.code !== 'string' && typeof expectations.code !== 'number') {
			throw new AssertionError(`The \`code\` property of the second argument to \`${assertion}\` must be a string or number`, {
				assertion,
				formattedDetails: [formatWithLabel('Called with:', expectations)],
			});
		}

		if (Object.hasOwn(expectations, 'any') && typeof expectations.any !== 'boolean') {
			throw new AssertionError(`The \`any\` property of the second argument to \`${assertion}\` must be a boolean`, {
				assertion,
				formattedDetails: [formatWithLabel('Called with:', expectations)],
			});
		}

		for (const key of Object.keys(expectations)) {
			switch (key) {
				case 'instanceOf':
				case 'is':
				case 'message':
				case 'name':

View on GitHub (pinned to bbfd946322)

Solutions

  1. Pass the code as a string ('ERR_ASSERTION') or number (errno-style)
  2. Do not wrap the code in an object or array
  3. Remove the code key if the error carries no code property
Defensive patterns

Strategy: type-guard

When it happens

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