avajs/ava · error · AssertionError

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

Error message

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

What it means

In AVA's assertion library, t.notRegex() asserts that a string does NOT match a regular expression. Before performing the check, it validates its first argument; if the first argument is not a string, this AssertionError is thrown immediately. This is a usage (programming) error, not a failed assertion, because t.notRegex() cannot check non-string values.

Source

Thrown at lib/assert.js:773

			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();
		});

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

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

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

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

View on GitHub (pinned to bbfd946322)

Solutions

  1. Convert the value to a string first: t.notRegex(String(value), /pattern/) or buffer.toString() for Buffers.
  2. Check the variable is defined and a string before the assertion (e.g. guard on fs output or parsed JSON).
  3. Verify argument order: the string must be the first argument, the RegExp the second — swapping them triggers this error.
  4. If you need to assert on non-strings, use the appropriate assertion like t.deepEqual() or t.is() instead.

Example fix

// before
const body = fs.readFileSync('out.txt'); // Buffer
t.notRegex(body, /ERROR/);
// after
t.notRegex(body.toString('utf8'), /ERROR/);
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof value !== 'string') { throw new TypeError('t.notRegex() requires a string; got ' + typeof value); }
t.notRegex(value, /pattern/);

Type guard

function isString(v) { return typeof v === 'string'; }

Try / catch

try {
  t.notRegex(value, /pattern/);
} catch (err) {
  if (err.name === 'AssertionError' && /must be called with a string/.test(err.message)) {
    throw new Error('Test bug: t.notRegex() first argument must be a string, got: ' + typeof value);
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling t.notRegex() with a first argument whose typeof is not 'string' — e.g. t.notRegex(undefined, /x/), t.notRegex(null, /a/), t.notRegex(123, /1/), or t.notRegex(buffer, /x/) passing a Buffer/object instead of a string.

Common situations: Developers pass a value read from JSON, an optional variable that turned out undefined, a number from config parsing, or a Buffer from fs.readFileSync directly into t.notRegex() without converting it (e.g. with String(...) or buffer.toString()).

Related errors


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