avajs/ava · error · AssertionError

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

Error message

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

What it means

t.notRegex() requires its second argument to be a RegExp instance. If the second argument is not an RegExp (e.g. a string pattern, undefined, or null), AVA throws this AssertionError instead of performing the assertion. This is a usage error because the assertion is meaningless without a regular expression to test against.

Source

Thrown at lib/assert.js:780

					],
				}));
			}

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

			return pass();
		});

View on GitHub (pinned to bbfd946322)

Solutions

  1. Pass a real regex literal or RegExp instance: t.notRegex(str, /pattern/) or t.notRegex(str, new RegExp(pattern)).
  2. If you have a pattern string, wrap it: new RegExp(patternString) — but escape user input properly.
  3. Log/inspect the variable to see why it is undefined or not a RegExp before the assertion call.
  4. Check argument order — the RegExp must be the second argument; swapping the string and regex triggers this error.

Example fix

// before
t.notRegex(output, 'ERROR:');
// after
t.notRegex(output, /ERROR:/);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(regex instanceof RegExp)) { throw new TypeError('t.notRegex() requires a RegExp; got ' + typeof regex); }
t.notRegex(value, regex);

Type guard

function isRegExp(v) { return v instanceof RegExp; }

Try / catch

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

Prevention

When it happens

Trigger: Calling t.notRegex(string, x) where x is not an RegExp: passing a pattern string like t.notRegex('foo', 'bar'), passing undefined because the regex variable was never assigned, or passing a regex-like object created with Object.create(RegExp.prototype).

Common situations: Developers coming from other frameworks pass a string pattern instead of a regex literal; a regex built with new RegExp(...) from dynamic input ends up undefined due to an earlier error or bad config; TypeScript types were bypassed with `any`.

Related errors


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