{"name":"@tapjs/snapshot","version":"4.3.9","keywords":["tapjs plugin"],"author":{"url":"https://blog.izs.me","name":"Isaac Z. Schlueter","email":"i@izs.me"},"license":"BlueOak-1.0.0","_id":"@tapjs/snapshot@4.3.9","maintainers":[{"name":"ljharb","email":"ljharb@gmail.com"},{"name":"isaacs","email":"i@izs.me"}],"homepage":"https://github.com/tapjs/tapjs/tree/main/src/snapshot#readme","bugs":{"url":"https://github.com/tapjs/tapjs/issues"},"tap":{"typecheck":false,"coverage-map":"map.js"},"dist":{"shasum":"54dd822ad518ee478a893101e0c1993f54191d39","tarball":"http://registry-npm.ywwl.com/@tapjs/snapshot/download/@tapjs/snapshot-4.3.9.tgz","fileCount":37,"integrity":"sha512-lqRvxsd4GfkYJhAsIuN2fcuE/UmFoDgOKwzwMR3itXZrBsBaYdWCiGGEfeCZTLMxVaemwbOrDR+xrkHcj1I2Xg==","signatures":[{"sig":"MEUCIQDYocbuW6DG7F7TpDJVFRq/ZiAMLfVlWj8OhEYE8AEBFQIgBVcv9pMfLpcq/EFxAMwUSXDqlwYTyjA13SuRORqc+y4=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":123857,"size":22388,"noattachment":false,"key":"/@tapjs/snapshot/-/@tapjs/snapshot-4.3.9.tgz"},"main":"./dist/commonjs/index.js","tshy":{"main":true,"exports":{".":"./src/index.ts","./package.json":"./package.json"},"selfLink":false},"type":"module","types":"./dist/commonjs/index.d.ts","module":"./dist/esm/index.js","engines":{"node":"20 || >=22"},"exports":{".":{"import":{"types":"./dist/esm/index.d.ts","default":"./dist/esm/index.js"},"require":{"types":"./dist/commonjs/index.d.ts","default":"./dist/commonjs/index.js"}},"./package.json":"./package.json"},"funding":{"url":"https://github.com/sponsors/isaacs"},"gitHead":"0ab7f3cdf804a5d481a7ab78ffc00aee600c7092","scripts":{"snap":"tap","test":"tap","format":"prettier --write . --log-level warn --ignore-path ../../.prettierignore --cache","prepare":"tshy","presnap":"npm run prepare","pretest":"npm run prepare","typedoc":"typedoc --tsconfig tsconfig/esm.json ./src/*.ts"},"_npmUser":{"name":"isaacs","email":"i@izs.me"},"repository":{"url":"git+ssh://git@github.com/tapjs/tapjs.git","path":"src/snapshot","type":"git"},"_npmVersion":"11.12.1","description":"A default tap plugin that adds snapshot functionality.","directories":{},"_nodeVersion":"26.0.0","dependencies":{"tcompare":"9.3.2","trivial-deferred":"^2.0.0","is-actual-promise":"^1.0.1"},"_hasShrinkwrap":false,"peerDependencies":{"@tapjs/core":"4.5.7"},"_npmOperationalInternal":{"tmp":"tmp/snapshot_4.3.9_1778712823554_0.6917271592764278","host":"s3://npm-registry-packages-npm-production"},"readme":"# `@tapjs/snapshot`\n\nA default tap plugin that adds snapshot functionality.\n\n## USAGE\n\nThis plugin is installed with tap by default. If you had\npreviously removed it, you can `tap plugin add @tapjs/snapshot` to\nbring it back.\n\nSnapshots are a way to make extremely comprehensive assertions\nabout a value, and easily keep tests up to date. This is a\npowerful technique for testing many kinds of programs (especially\nthose that generate output), but it comes with some caveats.\n\n## Basics of Output Testing\n\nConsider a test program like this:\n\n```js\n// tagger.mjs\nexport default (tag: string, contents: string) =>\n  `<${tag}>${contents}</${tag}>`\n```\n\nWe might have a test like this:\n\n```js\n// tagger.test.mjs\nimport t from 'tap'\nimport tagger from './tagger.mjs'\nt.equal(tagger('tagName', 'content'), '<tagName>content</tagName>')\n```\n\nThis is good for a couple of reasons:\n\n1. It's clear reading our test what the expected output is.\n2. We're unlikely to create a test without thinking carefully about\n   what _exactly_ it's testing.\n\nHowever, managing strings like this can become extremely tedious,\nespecially in cases where the output is long, or there are many cases\nto test. If we make an _intentional_ change to the output, then we\nneed to manually update a large number of large strings, possibly\nscattered throughout the test suite. The inevitable result is\nthat we either make the tests less comprehensive, or even worse,\ntreat some as [\"known\nfailures\"](https://node-tap.org/basics/#note-about-%22expected-failures%22-and-%22run-until-good%22-testing).\n\n## Testing Output with Snapshots\n\nWe could also write our test file like this:\n\n```js\n// tagger.test.mjs\nimport t from 'tap'\nimport tagger from './tagger.mjs'\nt.matchSnapshot(tagger('tagName', 'content'), 'output')\n```\n\nBut wait, where is the expected output?\n\nTo create the snapshot file, run this command:\n\n```\n$ tap tagger.test.mjs --snapshot\n PASS  test.js 1 OK 344ms\n\n\n  🌈 SUMMARY RESULTS 🌈\n\n\nSuites:   1 passed, 1 of 1 completed\nAsserts:  1 passed, of 1\nTime:     422ms\n```\n\nBy setting `TAP_SNAPSHOT` in the environment or passing the `--snapshot`\ncommand line flag, we tell tap to write the output to a special file, and\ntreat the assertion as automatically passing.\n\nSnapshots will be generated by default if the npm script being run is named\n`snap` or `snapshot`, so you can use this pattern to test and snapshot most\nprojects:\n\n```json\n{\n  \"name\": \"my-project\",\n  \"version\": \"1.2.3\",\n\n  \"devDependencies\": {\n    \"tap\": \"15\"\n  },\n  \"scripts\": {\n    \"test\": \"tap\",\n    \"snap\": \"tap\"\n  }\n}\n```\n\nThen, you can use `npm test` to run your tests, or `npm run snap` to\nupdate snapshot files.\n\n## Snapshot Files\n\nThe generated file is designed to be human-readable, but you\nshould not edit it directly.\n\n```\n$ cat tap-snapshots/tagger.test.mjs.test.cjs\n/* IMPORTANT\n * This snapshot file is auto-generated, but designed for humans.\n * It should be checked into source control and tracked carefully.\n * Re-generate by setting TAP_SNAPSHOT=1 and running tests.\n * Make sure to inspect the output below.  Do not ignore changes!\n */\n'use strict'\nexports[`tagger.test.mjs TAP > output 1`] = `\n<tagName>content</tagName>\n```\n\nIf the argument passed to `t.matchSnapshot()` isn't a string,\nthen it will be converted to a string using\n[tcompare.format](https://tapjs.github.io/tapjs/modules/tcompare.html).\nThis is typically pretty easy for humans to understand, but of\ncourse if you prefer to use `JSON.stringify` or something else,\nyou can do so easily enough. The\n[t.formatSnapshot](https://tapjs.github.io/tapjs/classes/_tapjs_test.index.Test.html#formatSnapshot)\ncan be used to customize this for an entire test.\n\n## Caveats\n\n### Track Changes\n\n**Important: you should check the snapshot file into source control!**\n\nWhen there are changes to it, inspect the diff and make sure that nothing\nunexpected happened to change your output.\n\nIf you don't check this file into source control, then a significant part\nof your test is not checked in. This prevents people from collaborating on\nyour project.\n\nIf you accept changes to it without care, then you can obscure unintended\nchanges. (Though, even if this happens, `git bisect` can track down the\nsource of the change quite quickly, so it's not the end of the world if\nthere are occasional mistakes.)\n\n### Strip Variables from Output with `t.cleanSnapshot`\n\nIf your output includes data that is known to change from one run to the\nnext, then these changes should be stripped before matching against a\nsnapshot.\n\nThis includes process IDs, time stamps, and many other system details.\n\nConsider this function:\n\n```js\n// msgtime.mjs\nexport const msgTime = msg => {\n  return msg + ' time=' + Date.now()\n}\n```\n\nSince the output will obviously be slightly different every time the\nfunction is tested, we need to strip out the time value.\n\nThe best way to do this is with a\n[`t.cleanSnapshot`](https://tapjs.github.io/tapjs/classes/_tapjs_test.index.Test.html#cleanSnapshot)\nfunction. This function takes the formatted snapshot as a string,\nand returns a string to be saved or compared against. The default\ncleaner is an identity function that returns its input without\nany changes.\n\nA test that uses this method:\n\n```js\n// msgtime.test.mjs\nimport t from 'tap'\n\n// This must be assigned *before* running tests\n// It is passed down to child tests when they are created\nt.cleanSnapshot = s => s.replace(/ time=[0-9]+$/g, ' time={time}')\n\nconst output = msgTime('this is a test')\nt.matchSnapshot(output, 'add timestamp to message')\n```\n\nWhen run with `--snapshot`, it generates this snapshot\nfile:\n\n```js\n/* IMPORTANT\n * This snapshot file is auto-generated, but designed for humans.\n * It should be checked into source control and tracked carefully.\n * Re-generate by setting TAP_SNAPSHOT=1 and running tests.\n * Make sure to inspect the output below.  Do not ignore changes!\n */\n'use strict'\nexports[`msgtime.test.js TAP > add timestamp to message 1`] = `\nthis is a test time={time}\n`\n```\n\n## Custom Formatting\n\nSometimes just modifying the string is not enough, or a special data type\nshould be stringified differently.\n\nBy default, tap uses\n[`tcompare.format`](https://tapjs.github.io/tapjs/functions/tcompare.index.format-1.html)\nto convert all non-string values into strings for saving and\ncomparing.\n\nTo override this and provide your own behavior, set a function to\n[`t.formatSnapshot`](https://tapjs.github.io/tapjs/classes/_tapjs_test.index.Test.html#cleanSnapshot).\nLike `t.cleanSnapshot`, child tests will copy their parent test's\nvalue at their time of creation.\n\nAn example of using `t.formatSnapshot`:\n\n```js\nimport t from 'tap'\nimport { stringify } from 'tap-yaml'\nt.formatSnapshot = object => stringify(object)\n\n// now all my snapshot files will be in yaml!\nt.matchSnapshot({ foo: ['bar', 'baz'] })\n```\n\nThis will produce the following snapshot\nfile:\n\n```js\n/* IMPORTANT\n * This snapshot file is auto-generated, but designed for humans.\n * It should be checked into source control and tracked carefully.\n * Re-generate by setting TAP_SNAPSHOT=1 and running tests.\n * Make sure to inspect the output below.  Do not ignore changes!\n */\n'use strict'\nexports[`yaml.test.js TAP > must match snapshot 1`] = `\nfoo:\n  - bar\n  - baz\n\n`\n```\n\n## Advanced Customization (Warning: footguns!)\n\nBy default, the snapshot file that TAP uses is set based on the test\nfilename and any arguments passed to it. This prevents having two test\nsuites clobber one another's snapshots.\n\nIf you would like to specify a different filename for your snapshots, you\ncan do so by setting the\n[`t.snapshotFile`](https://tapjs.github.io/tapjs/classes/_tapjs_test.index.Test.html#snapshotFile)\nfield. If you set this field in a child test, then the _parent_\ntest (and all of its other child tests) will still use its old\nsnapshot file, but snapshots in the child test (and all its\nchildren) will go to the new file.\n\nBut, be advised, this is advanced usage and exposes you to some hazards!\n\n- When generating snapshots, the file is _overwritten_, so if two tests use\n  the same snapshot file, they will _not_ be merged. The last one will win.\n  Make sure that every snapshot file you use is unique to the entire test\n  run.\n- Snapshot files should always be in a folder called `tap-snapshot`, and\n  should always end in `.test.cjs`. This pattern ensures that tap can load\n  them properly, and that they will not be confused with normal test files.\n\nExample of using a separate snapshot file for subtests:\n\n```js\n// file 'test/snappy.js'\nimport t from 'tap'\nimport { resolve } from 'node:path'\n\nt.matchSnapshot('this writes to ./tap-snapshots/test/snappy.js.test.cjs')\n\nt.test('foo', async t => {\n  // this writes to ./test/tap-snapshots/foo.test.cjs'\n  // either path or file URL or file URL string is fine, but\n  // it'll always store it as the actual path.\n  t.snapshotFile = new URL(\n    '../tap-snapshots/foo.test.cjs',\n    import.meta.url,\n  )\n  t.matchSnapshot('foo')\n})\n\nt.test('bar', async t => {\n  // this writes to ./test/tap-snapshots/bar.test.cjs'\n  t.snapshotFile = new URL(\n    '../tap-snapshots/bar.test.cjs',\n    import.meta.url,\n  )\n  t.matchSnapshot('bar')\n})\n\nt.test('baz', async t => {\n  // this one doesn't set it, so it uses the parent's snapshot object,\n  // which writes to ./tap-snapshots/test/snappy.js.test.cjs\n  t.matchSnapshot('baz')\n})\n```\n\n## EVEN MORE Advanceder Customization (you're on your own!)\n\nThis module works by using a [Snapshot\nProvider](https://tapjs.github.io/tapjs/classes/_tapjs_snapshot.provider.SnapshotProviderDefault.html),\nwhich is a class that implements read, save, and snap methods.\n\nYou can write your own one of those, and pass the custom class in\nthe `snapshotProvider` option to a test.\n\nWith this, you can store snapshots anywhere you like, if for some\nreason putting them in a js file is not your desire.\n","publish_time":1778712823709,"_cnpm_publish_time":1778712823709,"dist-tags":{"latest":"4.3.13","pre":"1.0.0"}}