{"record":{"id":"3cdc72ee4a956d39","repo":"avajs/ava","slug":"t-notregex-must-be-called-with-a-string","errorCode":null,"errorMessage":"`t.notRegex()` must be called with a string","messagePattern":"`t\\.notRegex\\(\\)` must be called with a string","errorType":"validation","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"lib/assert.js","lineNumber":773,"sourceCode":"\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),\n\t\t\t\t\t],\n\t\t\t\t}));\n\t\t\t}\n\n\t\t\treturn pass();\n\t\t});\n\n\t\tthis.notRegex = withSkip((string, regex, message) => {\n\t\t\tassertMessage(message, 't.notRegex()');\n\n\t\t\tif (typeof string !== 'string') {\n\t\t\t\tthrow fail(new AssertionError('`t.notRegex()` must be called with a string', {\n\t\t\t\t\tassertion: 't.notRegex()',\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.notRegex()` must be called with a regular expression', {\n\t\t\t\t\tassertion: 't.notRegex()',\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.notRegex()',\n\t\t\t\t\tformattedDetails: [\n\t\t\t\t\t\tformatWithLabel('Value must not match expression:', string),\n\t\t\t\t\t\tformatWithLabel('Regular expression:', regex),","sourceCodeStart":755,"sourceCodeEnd":791,"githubUrl":"https://github.com/avajs/ava/blob/bbfd946322fdeca2b547a691d947fb4e18c0c67f/lib/assert.js#L755-L791","documentation":"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.","triggerScenarios":"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.","commonSituations":"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()).","solutions":["Convert the value to a string first: t.notRegex(String(value), /pattern/) or buffer.toString() for Buffers.","Check the variable is defined and a string before the assertion (e.g. guard on fs output or parsed JSON).","Verify argument order: the string must be the first argument, the RegExp the second — swapping them triggers this error.","If you need to assert on non-strings, use the appropriate assertion like t.deepEqual() or t.is() instead."],"exampleFix":"// before\nconst body = fs.readFileSync('out.txt'); // Buffer\nt.notRegex(body, /ERROR/);\n// after\nt.notRegex(body.toString('utf8'), /ERROR/);","handlingStrategy":"type-guard","validationCode":"if (typeof value !== 'string') { throw new TypeError('t.notRegex() requires a string; got ' + typeof value); }\nt.notRegex(value, /pattern/);","typeGuard":"function isString(v) { return typeof v === 'string'; }","tryCatchPattern":"try {\n  t.notRegex(value, /pattern/);\n} catch (err) {\n  if (err.name === 'AssertionError' && /must be called with a string/.test(err.message)) {\n    throw new Error('Test bug: t.notRegex() first argument must be a string, got: ' + typeof value);\n  }\n  throw err;\n}","preventionTips":["Always pass strings explicitly: wrap unknown values with String(value) before regex assertions.","Convert Buffers with .toString() before regex matching.","Enable TypeScript and type test arguments as string to catch non-strings at compile time.","Double-check argument order: string first, RegExp second."],"tags":["assertion","type-error","ava","argument-validation"],"backgroundTag":"assertion-invalid-argument-type","analyzedSha":"bbfd946322fdeca2b547a691d947fb4e18c0c67f","analyzedAt":"2026-09-02T01:24:43.834Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}