{"record":{"id":"b95dbaf6863ebb85","repo":"avajs/ava","slug":"t-regex-must-be-called-with-a-string","errorCode":null,"errorMessage":"`t.regex()` must be called with a string","messagePattern":"`t\\.regex\\(\\)` must be called with a string","errorType":"validation","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"lib/assert.js","lineNumber":743,"sourceCode":"\n\t\tthis.false = withSkip((actual, message) => {\n\t\t\tassertMessage(message, 't.false()');\n\n\t\t\tif (actual === false) {\n\t\t\t\treturn pass();\n\t\t\t}\n\n\t\t\tthrow fail(new AssertionError(message, {\n\t\t\t\tassertion: 't.false()',\n\t\t\t\tformattedDetails: [formatWithLabel('Value is not `false`:', actual)],\n\t\t\t}));\n\t\t});\n\n\t\tthis.regex = withSkip((string, regex, message) => {\n\t\t\tassertMessage(message, 't.regex()');\n\n\t\t\tif (typeof string !== 'string') {\n\t\t\t\tthrow fail(new AssertionError('`t.regex()` must be called with a string', {\n\t\t\t\t\tassertion: 't.regex()',\n\t\t\t\t\tformattedDetails: [formatWithLabel('Called with:', string)],\n\t\t\t\t}));\n\t\t\t}\n\n\t\t\tif (!(regex instanceof RegExp)) {\n\t\t\t\tthrow fail(new AssertionError('`t.regex()` must be called with a regular expression', {\n\t\t\t\t\tassertion: 't.regex()',\n\t\t\t\t\tformattedDetails: [formatWithLabel('Called with:', regex)],\n\t\t\t\t}));\n\t\t\t}\n\n\t\t\tif (!regex.test(string)) {\n\t\t\t\tthrow fail(new AssertionError(message, {\n\t\t\t\t\tassertion: 't.regex()',\n\t\t\t\t\tformattedDetails: [\n\t\t\t\t\t\tformatWithLabel('Value must match expression:', string),\n\t\t\t\t\t\tformatWithLabel('Regular expression:', regex),","sourceCodeStart":725,"sourceCodeEnd":761,"githubUrl":"https://github.com/avajs/ava/blob/bbfd946322fdeca2b547a691d947fb4e18c0c67f/lib/assert.js#L725-L761","documentation":"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:'.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nt.regex(res, /ok/); // res is a Buffer\n\n// after\nt.regex(res.toString(), /ok/);","handlingStrategy":"type-guard","validationCode":"if (typeof value !== 'string') {\n  throw new TypeError(`t.regex() pre-check failed: expected string, got ${typeof value}`);\n}","typeGuard":"const isString = (v) => typeof v === 'string';\nif (isString(value)) t.regex(value, /pattern/);","tryCatchPattern":"try {\n  t.regex(maybeString, /pattern/);\n} catch (err) {\n  if (err.message === '`t.regex()` must be called with a string') {\n    console.error('value was:', maybeString);\n  }\n  throw err;\n}","preventionTips":["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."],"tags":["assertion","ava","type-error","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"bbfd946322fdeca2b547a691d947fb4e18c0c67f","analyzedAt":"2026-09-02T01:24:43.834Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}