{"record":{"id":"21cff671b5945d9f","repo":"avajs/ava","slug":"validateexpectations-error-for-t-throws","errorCode":null,"errorMessage":"(validateExpectations error for t.throws())","messagePattern":"\\(validateExpectations error for t\\.throws\\(\\)\\)","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"lib/assert.js","lineNumber":417,"sourceCode":"\t\t\t// Since arrow functions do not support 'arguments', we are using rest\n\t\t\t// operator, so we can determine the total number of arguments passed\n\t\t\t// to the function.\n\t\t\tlet [fn, expectations, message] = args;\n\n\t\t\tassertMessage(message, 't.throws()');\n\n\t\t\tif (typeof fn !== 'function') {\n\t\t\t\tthrow fail(new AssertionError('`t.throws()` must be called with a function', {\n\t\t\t\t\tassertion: 't.throws()',\n\t\t\t\t\timproperUsage: {assertion: 'throws'},\n\t\t\t\t\tformattedDetails: [formatWithLabel('Called with:', fn)],\n\t\t\t\t}));\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\texpectations = validateExpectations('t.throws()', expectations, args.length, experiments);\n\t\t\t} catch (error) {\n\t\t\t\tthrow fail(error);\n\t\t\t}\n\n\t\t\tlet retval;\n\t\t\tlet threw = false;\n\t\t\tlet actual = null;\n\t\t\ttry {\n\t\t\t\tretval = fn();\n\t\t\t\tif (isPromise(retval)) {\n\t\t\t\t\t// Here isPromise() checks if something is \"promise like\". Cast to an actual promise.\n\t\t\t\t\tPromise.resolve(retval).catch(noop);\n\t\t\t\t\tthrow fail(new AssertionError(message, {\n\t\t\t\t\t\tassertion: 't.throws()',\n\t\t\t\t\t\tformattedDetails: [formatWithLabel('Function returned a promise. Use `t.throwsAsync()` instead:', retval)],\n\t\t\t\t\t}));\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tthrew = true;\n\t\t\t\tactual = error;","sourceCodeStart":399,"sourceCodeEnd":435,"githubUrl":"https://github.com/avajs/ava/blob/bbfd946322fdeca2b547a691d947fb4e18c0c67f/lib/assert.js#L399-L435","documentation":"When the expectations argument of t.throws() is invalid, AVA's validateExpectations() throws its own descriptive error (e.g. about bad constructor/regex/message shapes); t.throws catches it and re-throws it via fail() so it is recorded as a failed assertion. The bracketed message is a placeholder — the real text comes from validateExpectations.","triggerScenarios":"t.throws(fn, expectations, ...) where expectations is not undefined/function/regex/string/object of the allowed shape (e.g. passing a number, or {message: 42}), or passing >3 args.","commonSituations":"Passing an Error instance instead of a constructor; passing a string expectation where a regex is required; upgrading AVA and using a no-longer-supported expectations form.","solutions":["Read the underlying validateExpectations message in the error output for the exact offending expectation.","Pass a constructor (e.g. TypeError), RegExp, string, or a valid object like {instanceOf, message, name}.","Use {instanceOf: ErrorClass} instead of passing an Error instance."],"exampleFix":"// before\nt.throws(fn, new TypeError('boom'));\n// after\nt.throws(fn, { instanceOf: TypeError, message: 'boom' });","handlingStrategy":"validation","validationCode":"function isValidExpectation(e) {\n  if (e == null || typeof e === 'function' || typeof e === 'string' || e instanceof RegExp) return true;\n  if (typeof e !== 'object') return false;\n  return ['instanceOf','message','name','code','is','truthy'].some(k => k in e);\n}","typeGuard":"const validThrowsExpectation = (e) =>\n  e === undefined || typeof e === 'function' || typeof e === 'string' ||\n  e instanceof RegExp || (typeof e === 'object' && e !== null &&\n    !Array.isArray(e));","tryCatchPattern":"try {\n  t.throws(fn, expectations);\n} catch (err) {\n  if (/expectation/i.test(err.message)) {\n    t.fail('Invalid t.throws() expectations: ' + err.message);\n  } else { throw err; }\n}","preventionTips":["Pass constructors (Error subclasses), RegExp, or {instanceOf, message} — never Error instances.","Keep the argument order: fn, expectations, message.","Re-check expectation shapes after AVA version upgrades."],"tags":["assertion","ava","invalid-argument","usage-error"],"backgroundTag":"invalid-assertion-expectations","analyzedSha":"bbfd946322fdeca2b547a691d947fb4e18c0c67f","analyzedAt":"2026-09-02T01:24:43.834Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}