avajs/ava · error · AssertionError

`t.regex()` must be called with a regular expression

Error message

`t.regex()` must be called with a regular expression

What it means

AVA throws this fixed-message AssertionError from t.regex() when the second argument is not a RegExp instance (regex instanceof RegExp fails). The assertion requires an actual regular expression; strings like '/abc/' are not accepted. The offending value is shown after 'Called with:'.

Source

Thrown at lib/assert.js:750

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

			return pass();
		});

View on GitHub (pinned to bbfd946322)

Solutions

  1. Convert string patterns to RegExp: t.regex(value, new RegExp(pattern)) or pass a /literal/ regex.
  2. Check the argument order: t.regex(string, regex), not t.regex(regex, string).
  3. If the pattern crosses a vm/worker realm, verify it with Object.prototype.toString.call(p) === '[object RegExp]' and reconstruct with new RegExp(p.source, p.flags).
  4. Guard before the call: if (!(regex instanceof RegExp)) normalize or throw a clearer error.

Example fix

// before
const pattern = '^v\\d+\\.\\d+$'; // from config JSON
t.regex(version, pattern); // TypeError-style assertion

// after
t.regex(version, new RegExp(pattern));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(pattern instanceof RegExp)) {
  pattern = new RegExp(pattern); // accept string patterns from config
}

Type guard

const isRegExp = (v) => Object.prototype.toString.call(v) === '[object RegExp]';
if (!isRegExp(pattern)) pattern = new RegExp(String(pattern));

Try / catch

try {
  t.regex(value, pattern);
} catch (err) {
  if (err.message === '`t.regex()` must be called with a regular expression') {
    pattern = new RegExp(String(pattern));
    t.regex(value, pattern);
  } else throw err;
}

Prevention

When it happens

Trigger: t.regex(str, pattern) where pattern is a string ('^foo$'), a regex-like object from another realm (vm/iframe) or a minified/cloned RegExp, null/undefined, or a RegExp-like duck-typed object.

Common situations: Loading patterns from JSON/config where they arrive as strings; destructuring mix-ups putting the string in the regex slot; passing patterns across vm boundaries or worker_threads where instanceof fails; simple typo passing arguments in the wrong order.

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/18e6fe2589f758b2. Report an issue: GitHub.