avajs/ava · error · AssertionError

`t.regex()` must be called with a string

Error message

`t.regex()` must be called with a string

What it means

AVA throws this fixed-message AssertionError from t.regex() when the first argument is not a string (typeof string !== 'string'). It is an improper-usage error: the assertion cannot run a regex test against a non-string value. The offending value is shown after 'Called with:'.

Source

Thrown at lib/assert.js:743

		this.false = withSkip((actual, message) => {
			assertMessage(message, 't.false()');

			if (actual === false) {
				return pass();
			}

			throw fail(new AssertionError(message, {
				assertion: 't.false()',
				formattedDetails: [formatWithLabel('Value is not `false`:', actual)],
			}));
		});

		this.regex = withSkip((string, regex, message) => {
			assertMessage(message, 't.regex()');

			if (typeof string !== 'string') {
				throw fail(new AssertionError('`t.regex()` must be called with a string', {
					assertion: 't.regex()',
					formattedDetails: [formatWithLabel('Called with:', string)],
				}));
			}

			if (!(regex instanceof RegExp)) {
				throw fail(new AssertionError('`t.regex()` must be called with a regular expression', {
					assertion: 't.regex()',
					formattedDetails: [formatWithLabel('Called with:', regex)],
				}));
			}

			if (!regex.test(string)) {
				throw fail(new AssertionError(message, {
					assertion: 't.regex()',
					formattedDetails: [
						formatWithLabel('Value must match expression:', string),
						formatWithLabel('Regular expression:', regex),

View on GitHub (pinned to bbfd946322)

Solutions

  1. Convert the value to a string before asserting: String(value), value.toString(), or JSON.stringify for objects.
  2. Await async values: t.regex(await getText(), /pattern/).
  3. Pass the specific string property (response.body.message) rather than the whole object.
  4. Guard with a typeof check or a type guard before calling t.regex() to fail fast with a clearer message.

Example fix

// before
t.regex(res, /ok/); // res is a Buffer

// after
t.regex(res.toString(), /ok/);
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof value !== 'string') {
  throw new TypeError(`t.regex() pre-check failed: expected string, got ${typeof value}`);
}

Type guard

const isString = (v) => typeof v === 'string';
if (isString(value)) t.regex(value, /pattern/);

Try / catch

try {
  t.regex(maybeString, /pattern/);
} catch (err) {
  if (err.message === '`t.regex()` must be called with a string') {
    console.error('value was:', maybeString);
  }
  throw err;
}

Prevention

When it happens

Trigger: t.regex(value, /pattern/) where value is a number, null, undefined, an object, a Buffer, or a Promise — e.g. passing an unawaited promise, a parsed JSON number, or a whole response object instead of a string field.

Common situations: Forgetting to await an async function returning a string; passing a URL object or Buffer instead of its .toString(); reading a config value typed as number; accessing the wrong property so a non-string lands in the first argument.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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