{"record":{"id":"385d1da86fd18471","repo":"denoland/deno","slug":"expected-the-second-argument-to-assertsnapshot-t","errorCode":null,"errorMessage":"Expected the second argument to assertSnapshot() to be an options object or a message string","messagePattern":"Expected the second argument to assertSnapshot\\(\\) to be an options object or a message string","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"cli/js/40_test_snapshot.js","lineNumber":319,"sourceCode":"}\n\nfunction getErrorMessage(message, options) {\n  return typeof options.msg === \"string\" ? options.msg : message;\n}\n\n/**\n * Implementation of `Deno.TestContext.assertSnapshot()`. `tContext` is the\n * test context object the method was called on.\n */\nexport function assertSnapshot(\n  tContext,\n  actual,\n  options = { __proto__: null },\n) {\n  if (typeof options === \"string\") {\n    options = { __proto__: null, msg: options };\n  } else if (typeof options !== \"object\" || options === null) {\n    throw new TypeError(\n      \"Expected the second argument to assertSnapshot() to be an options object or a message string\",\n    );\n  }\n\n  const context = getSnapshotContext(options);\n  const testName = options.name ?? getFullTestName(tContext);\n  const count = (MapPrototypeGet(context.counts, testName) ?? 0) + 1;\n  MapPrototypeSet(context.counts, testName, count);\n  const name = `${testName} ${count}`;\n\n  if (!ArrayPrototypeIncludes(context.updateQueue, name)) {\n    ArrayPrototypePush(context.updateQueue, name);\n  }\n\n  const serializer = options.serializer ?? serialize;\n  const actualSnapshot = serializer(actual);\n  if (typeof actualSnapshot !== \"string\") {\n    throw new TypeError(\"Snapshot serializer must return a string\");","sourceCodeStart":301,"sourceCodeEnd":337,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/cli/js/40_test_snapshot.js#L301-L337","documentation":"Thrown by assertSnapshot() (cli/js/40_test_snapshot.js), the implementation behind t.assertSnapshot() in Deno tests. Besides the value under test, the call accepts either an options object ({ name, serializer, msg, ... }) or a plain string used as the failure message. Anything else as that argument is a TypeError.","triggerScenarios":"Calling t.assertSnapshot(value, opts) where opts is a boolean, number, symbol, or a non-object non-string — e.g. passing a message as a template-literal coercion attempt (true), passing options through a variable that is undefined-adjacent (e.g. a misnamed flag), or passing an array where the string|object union is expected (arrays pass the object check and are treated as an options object).","commonSituations":"Refactoring from assertEquals(value, expected, \"msg\") and passing a non-string third-arg habit into assertSnapshot; building options conditionally and letting a falsy sentinel (false/0) leak through; API drift from other snapshot libs whose signature is (value, nameStringOrObject).","solutions":["Pass an options object: t.assertSnapshot(value, { msg: \"context\" }) or t.assertSnapshot(value, { name: \"my snapshot\", serializer })","Or pass the message directly as a string: t.assertSnapshot(value, \"my message\")","If options come from a variable, default it: opts ?? {}"],"exampleFix":"// before\nawait t.assertSnapshot(result, true);\n\n// after\nawait t.assertSnapshot(result, { msg: \"rendered output\" });","handlingStrategy":"type-guard","validationCode":"function normalizeSnapshotOptions(arg) {\n  if (typeof arg === \"string\") return { msg: arg };\n  if (typeof arg === \"object\" && arg !== null) return arg;\n  return {}; // or throw your own clearer error before calling the API\n}","typeGuard":"/** @param {unknown} arg */\nfunction isSnapshotOptionsOrMessage(arg) {\n  return typeof arg === \"string\" ||\n    (typeof arg === \"object\" && arg !== null);\n}","tryCatchPattern":null,"preventionTips":["Always pass an options object literal at call sites — never a coerced truthy value","Default optional third args with `?? {}` when threading options through helpers"],"tags":["testing","snapshot","typeerror","api-misuse"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}