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
- Convert the value to a string before asserting: String(value), value.toString(), or JSON.stringify for objects.
- Await async values: t.regex(await getText(), /pattern/).
- Pass the specific string property (response.body.message) rather than the whole object.
- 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
- Await async string producers before passing them to t.regex().
- Call .toString() on Buffers/URL objects or pass the specific string property of a response.
- Check argument order — the string comes first: t.regex(string, regex).
- Coerce with String(value) or JSON.stringify(value) when a structured value's textual form is what you're testing.
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
- `t.notRegex()` must be called with a string
- `t.throws()` must be called with a function
- `t.throwsAsync()` must be called with a function or promise
- `t.notThrows()` must be called with a function
- `t.regex()` must be called with a regular expression
AI-assisted analysis of avajs/ava@bbfd946322 (2026-09-02).
Data as JSON: /api/errors/b95dbaf6863ebb85.
Report an issue: GitHub.