avajs/ava · error · AssertionError

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

Error message

The `any` property of the second argument to `${assertion}` must be a boolean

What it means

Thrown by validateExpectations in lib/assert.js:132 when an expectation object passed as the second argument to a throws assertion (e.g. t.throws / t.throwsAsync in AVA) has an `any` property whose value is not a boolean. The `any: true` option opt-in allows the assertion to accept thrown values that are not native Error instances. This is an improper-usage error: the assertion can never run, so the library fails fast with an AssertionError instead.

Source

Thrown at lib/assert.js:132

			});
		}

		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':
				case 'code':
				case 'any': {
					continue;
				}

				default: {
					throw new AssertionError(`The second argument to \`${assertion}\` contains unexpected properties`, {

View on GitHub (pinned to bbfd946322)

Solutions

  1. Change the value of `any` to a real boolean: `t.throws(fn, {any: true})` or omit the property entirely.
  2. If the value comes from user input or config, coerce it explicitly: `any: Boolean(cfgValue)` or `any: cfgValue === 'true'`.
  3. If you meant 'accept any thrown value' without the non-error relaxation, remove `any` and use other keys like `is`, `name`, or `message`.

Example fix

// before
t.throws(() => parse(raw), { any: 'true' });

// after
t.throws(() => parse(raw), { any: true });
Defensive patterns

Strategy: validation

Validate before calling

function assertValidExpectations(expectations) {
  if (expectations && Object.hasOwn(expectations, 'any') && typeof expectations.any !== 'boolean') {
    throw new TypeError('expectations.any must be a boolean');
  }
}

Type guard

function hasValidAny(e) {
  return e == null || !('any' in e) || typeof e.any === 'boolean';
}

Try / catch

try {
  t.throws(fn, {any: true});
} catch (error) {
  if (error.name === 'AssertionError' && /must be a boolean/.test(error.message)) {
    // fix expectations object before re-running
  }
  throw error;
}

Prevention

When it happens

Trigger: Calling t.throws(fn, {any: 1}), t.throws(fn, {any: 'true'}), or t.throwsAsync(promise, {any: null}) — i.e. `Object.hasOwn(expectations, 'any')` is true but `typeof expectations.any !== 'boolean'`.

Common situations: Typing `any: 'true'` or `any: 1` by hand; building the expectations object dynamically from config/CLI flags where a string 'true' isn't converted; TypeScript users bypassing types with `as any`; copying examples of older APIs where truthiness was tolerated.

Related errors


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