{"name":"test-each","version":"5.3.0","type":"module","exports":"./build/src/main.js","main":"./build/src/main.js","types":"./build/src/main.d.ts","scripts":{"test":"gulp test"},"description":"🤖 Repeat tests. Repeat tests. Repeat tests.","keywords":["testing","test","test-automation","data-driven","data-driven-tests","data-driven-testing","fuzzing","fuzz-testing","fuzz","cartesian","snapshot-testing","code-quality","typescript","es6","javascript","nodejs","library","iterable","foreach","functional-programming"],"license":"Apache-2.0","homepage":"https://www.github.com/ehmicky/test-each","repository":{"type":"git","url":"git+https://github.com/ehmicky/test-each.git"},"bugs":{"url":"https://github.com/ehmicky/test-each/issues"},"author":{"name":"ehmicky","email":"ehmicky@gmail.com","url":"https://github.com/ehmicky"},"directories":{"lib":"src","test":"test"},"dependencies":{"big-cartesian":"^4.2.0","is-plain-obj":"^4.1.0","normalize-exception":"^2.1.0","pretty-format":"^28.1.1"},"devDependencies":{"@ehmicky/dev-tasks":"^1.0.84","is-ci":"^3.0.1","safe-json-value":"^1.7.0"},"engines":{"node":">=14.18.0"},"gitHead":"090f84d047802870148cb2d895142b9186387d35","_id":"test-each@5.3.0","_nodeVersion":"18.8.0","_npmVersion":"8.18.0","dist":{"integrity":"sha512-PCV2a0VlnGl+DhudjCT75iiexiV6HpdFNzCPHX8NiCFdLSRYqtfqYeE7NQrHmOvoa4bWHx4OjwnNQBIUb8XiuA==","shasum":"32dabdbef2ff0d48549672046b7954d5424b0031","tarball":"http://registry-npm.ywwl.com/test-each/download/test-each-5.3.0.tgz","fileCount":11,"unpackedSize":37865,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDbX8mPsAzZ5Rws0ChthU6PRlKi1PfIo7gZLrLDJ42xGwIgQQ/2GJMPhAghg8O0PePLeAQ78TMKIjc3hiyDFxfOwqk="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjB7TRACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpCfRAAlXAZRS7s4sPJ678svNrND46b/M/YSJ4Z8GTjiDBS8xt7heij\r\nn59iA1llJMl1b5zAHyI8NIBT8ylX5dw7jVg2aD+HDAoRoOGqh5q+mprXYPyk\r\nLl3K/21RJSj6/OqkVutMSDY7PEnAqlRXZAOmjXBgRlCDaN15ICK1J027GSte\r\nGpr/5SnKa4QhkjVoMScBvcTH6hR8qUryeC0yfbUDJEcp0PshzQ7hsxSL6EKd\r\nyilz07BJfmbsC0OvtFO1UzOL6CLtOqKyHs+Kkq39paGCuO4OHwR5ftpFscRu\r\n51KE2AY9PjrFx2gRC/lVpwn97YU1tZcY7zpjeW55TRbnDY5SkMad/GSYTZ1e\r\n0AalZAiiwwJrpLUzCbmjaVUB58MfJoxYqxuUiuyti9EyAgrOVOA16s55BKtN\r\nnRoZbEenwUtkjI+cyq74gjKjPTDeCvPeC0PVdtP3yNDjAcCwvNxN+9L/1O7B\r\niWcIeGbR/Vuj9kPux+ceeK0v/A2qLWUvi212vvR+6koMcK4A/awfCZnmX5DU\r\nzOIj8LeTYjPmRL5LLL7tDMsMfiHTqO7k+0n7q52Mo0H8TU6ypsYw0H8X20Po\r\nRWAjqpthyow9y59B5+XxITXAz/ewkSG+DHi4a9GnlrcPTPDWXiI9ei6TcW62\r\nOgqdZKgh3HushGPhEB/ZwimxLM3Ef7deUSU=\r\n=Jq5m\r\n-----END PGP SIGNATURE-----\r\n","size":11925,"noattachment":false,"key":"/test-each/-/test-each-5.3.0.tgz"},"_npmUser":{"name":"ehmicky","email":"ehmicky@gmail.com"},"maintainers":[{"name":"ehmicky","email":"ehmicky@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/test-each_5.3.0_1661449424992_0.9200604901422376"},"_hasShrinkwrap":false,"readme":"<picture>\n  <source media=\"(prefers-color-scheme: dark)\" srcset=\"https://raw.githubusercontent.com/ehmicky/design/main/test-each/test-each_dark.svg\"/>\n  <img alt=\"test-each logo\" src=\"https://raw.githubusercontent.com/ehmicky/design/main/test-each/test-each.svg\" width=\"500\"/>\n</picture>\n\n[![Node](https://img.shields.io/badge/-Node.js-808080?logo=node.js&colorA=404040&logoColor=66cc33)](https://www.npmjs.com/package/test-each)\n[![Browsers](https://img.shields.io/badge/-Browsers-808080?logo=firefox&colorA=404040)](https://unpkg.com/test-each?module)\n[![TypeScript](https://img.shields.io/badge/-Typed-808080?logo=typescript&colorA=404040&logoColor=0096ff)](/src/main.d.ts)\n[![Codecov](https://img.shields.io/badge/-Tested%20100%25-808080?logo=codecov&colorA=404040)](https://codecov.io/gh/ehmicky/test-each)\n[![Minified size](https://img.shields.io/bundlephobia/minzip/test-each?label&colorA=404040&colorB=808080&logo=webpack)](https://bundlephobia.com/package/test-each)\n[![Mastodon](https://img.shields.io/badge/-Mastodon-808080.svg?logo=mastodon&colorA=404040&logoColor=9590F9)](https://fosstodon.org/@ehmicky)\n[![Medium](https://img.shields.io/badge/-Medium-808080.svg?logo=medium&colorA=404040)](https://medium.com/@ehmicky)\n\n🤖 Repeat tests. Repeat tests. Repeat tests.\n\nRepeats tests using different inputs\n([Data-Driven Testing](https://en.wikipedia.org/wiki/Data-driven_testing)):\n\n- test runner independent: works with your current setup\n- generates [test titles](#test-titles) that are descriptive, unique, for any\n  JavaScript type (not just JSON)\n- loops over every possible combination of inputs\n  ([cartesian product](#cartesian-product))\n- can use random functions ([fuzz testing](#fuzz-testing))\n- [snapshot testing](#snapshot-testing) friendly\n\n# Hire me\n\nPlease\n[reach out](https://www.linkedin.com/feed/update/urn:li:activity:7117265228068716545/)\nif you're looking for a Node.js API or CLI engineer (11 years of experience).\nMost recently I have been [Netlify Build](https://github.com/netlify/build)'s\nand [Netlify Plugins](https://www.netlify.com/products/build/plugins/)'\ntechnical lead for 2.5 years. I am available for full-time remote positions.\n\n# Example\n\n<!-- eslint-disable max-nested-callbacks -->\n\n```js\n// The examples use Ava but any test runner works (Jest, Mocha, Jasmine, etc.)\nimport test from 'ava'\n\nimport multiply from './multiply.js'\n\nimport { each } from 'test-each'\n\n// The code we are testing\n\n// Repeat test using different inputs and expected outputs\neach(\n  [\n    { first: 2, second: 2, output: 4 },\n    { first: 3, second: 3, output: 9 },\n  ],\n  ({ title }, { first, second, output }) => {\n    // Test titles will be:\n    //    should multiply | {\"first\": 2, \"second\": 2, \"output\": 4}\n    //    should multiply | {\"first\": 3, \"second\": 3, \"output\": 9}\n    test(`should multiply | ${title}`, (t) => {\n      t.is(multiply(first, second), output)\n    })\n  },\n)\n\n// Snapshot testing. The `output` is automatically set on the first run,\n// then re-used in the next runs.\neach(\n  [\n    { first: 2, second: 2 },\n    { first: 3, second: 3 },\n  ],\n  ({ title }, { first, second }) => {\n    test(`should multiply outputs | ${title}`, (t) => {\n      t.snapshot(multiply(first, second))\n    })\n  },\n)\n\n// Cartesian product.\n// Run this test 4 times using every possible combination of inputs\neach([0.5, 10], [2.5, 5], ({ title }, first, second) => {\n  test(`should mix integers and floats | ${title}`, (t) => {\n    t.is(typeof multiply(first, second), 'number')\n  })\n})\n\n// Fuzz testing. Run this test 1000 times using different numbers.\neach(1000, Math.random, ({ title }, index, randomNumber) => {\n  test(`should correctly multiply floats | ${title}`, (t) => {\n    t.is(multiply(randomNumber, 1), randomNumber)\n  })\n})\n```\n\n# Demo\n\nYou can try this library:\n\n- either directly [in your browser](https://repl.it/@ehmicky/test-each).\n- or by executing the [`examples` files](examples/README.md) in a terminal.\n\n# Install\n\n```\nnpm install -D test-each\n```\n\nThis package works in both Node.js >=18.18.0 and\n[browsers](https://raw.githubusercontent.com/ehmicky/dev-tasks/main/src/browserslist).\n\nThis is an ES module. It must be loaded using\n[an `import` or `import()` statement](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c),\nnot `require()`. If TypeScript is used, it must be configured to\n[output ES modules](https://www.typescriptlang.org/docs/handbook/esm-node.html),\nnot CommonJS.\n\n# Usage\n\n```js\nimport { each } from 'test-each'\n\nconst inputs = [\n  ['red', 'blue'],\n  [0, 5, 50],\n]\neach(...inputs, (info, color, number) => {})\n```\n\nFires `callback` once for each possible combination of `inputs`.\n\nEach `input` can be an [`array`](#cartesian-product), a\n[`function`](#input-functions) or an [`integer`](#fuzz-testing).\n\nA common use case for `callback` is to define tests (using any test runner).\n\n[`info`](#info) is an `object` whose properties can be used to generate\n[test titles](#test-titles).\n\n### Test titles\n\nEach combination of parameters is stringified as a `title` available in the\n`callback`'s [first argument](#infotitle).\n\nTitles should be included in test titles to make them descriptive and unique.\n\nLong titles are truncated. An incrementing counter is appended to duplicates.\n\nAny JavaScript type is\n[stringified](https://github.com/facebook/jest/tree/master/packages/pretty-format),\nnot just JSON.\n\nYou can customize titles either by:\n\n- defining `title` properties in `inputs` that are\n  [plain objects](https://stackoverflow.com/a/52453477/1526301)\n- using the [`info` argument](#info)\n\n<!-- eslint-disable max-nested-callbacks -->\n\n```js\nimport { each } from 'test-each'\n\neach([{ color: 'red' }, { color: 'blue' }], ({ title }, param) => {\n  // Test titles will be:\n  //    should test color | {\"color\": \"red\"}\n  //    should test color | {\"color\": \"blue\"}\n  test(`should test color | ${title}`, () => {})\n})\n\n// Plain objects can override this using a `title` property\neach(\n  [\n    { color: 'red', title: 'Red' },\n    { color: 'blue', title: 'Blue' },\n  ],\n  ({ title }, param) => {\n    // Test titles will be:\n    //    should test color | Red\n    //    should test color | Blue\n    test(`should test color | ${title}`, () => {})\n  },\n)\n\n// The `info` argument can be used for dynamic titles\neach([{ color: 'red' }, { color: 'blue' }], (info, param) => {\n  // Test titles will be:\n  //    should test color | 0 red\n  //    should test color | 1 blue\n  test(`should test color | ${info.index} ${param.color}`, () => {})\n})\n```\n\n### Cartesian product\n\nIf several `inputs` are specified, their\n[cartesian product](https://github.com/ehmicky/fast-cartesian) is used.\n\n```js\nimport { each } from 'test-each'\n\n// Run callback five times: a -> b -> c -> d -> e\neach(['a', 'b', 'c', 'd', 'e'], (info, param) => {})\n\n// Run callback six times: a c -> a d -> a e -> b c -> b d -> b e\neach(['a', 'b'], ['c', 'd', 'e'], (info, param, otherParam) => {})\n\n// Nested arrays are not iterated.\n// Run callback only twice: ['a', 'b'] -> ['c', 'd', 'e']\neach(\n  [\n    ['a', 'b'],\n    ['c', 'd', 'e'],\n  ],\n  (info, param) => {},\n)\n```\n\n### Input functions\n\nIf a `function` is used instead of an array, each iteration fires it and uses\nits return value instead. The `function` is called with the\n[same arguments](https://github.com/ehmicky/test-each#testeachinputs-callback)\nas the `callback`.\n\nThe generated values are included in [test titles](#test-titles).\n\n<!-- eslint-disable max-params -->\n\n```js\nimport { each } from 'test-each'\n\n// Run callback with a different random number each time\neach(['red', 'green', 'blue'], Math.random, (info, color, randomNumber) => {})\n\n// Input functions are called with the same arguments as the callback\neach(\n  ['02', '15', '30'],\n  ['January', 'February', 'March'],\n  ['1980', '1981'],\n  (info, day, month, year) => `${day}/${month}/${year}`,\n  (info, day, month, year, date) => {},\n)\n```\n\n### Fuzz testing\n\nIntegers can be used instead of arrays to multiply the number of iterations.\n\nThis enables [fuzz testing](https://en.wikipedia.org/wiki/Fuzzing) when combined\nwith [input functions](#input-functions) and libraries like\n[faker.js](https://github.com/marak/Faker.js),\n[chance.js](https://github.com/chancejs/chancejs) or\n[json-schema-faker](https://github.com/json-schema-faker/json-schema-faker).\n\n```js\nimport faker from 'faker'\n\n// Run callback 1000 times with a random UUID and color each time\neach(\n  1000,\n  faker.random.uuid,\n  faker.random.arrayElement(['green', 'red', 'blue']),\n  (info, randomUuid, randomColor) => {},\n)\n\n// `info.index` can be used as a seed for reproducible randomness.\n// The following series of 1000 UUIDs will remain the same across executions.\neach(\n  1000,\n  ({ index }) => faker.seed(index) && faker.random.uuid(),\n  (info, randomUuid) => {},\n)\n```\n\n### Snapshot testing\n\nThis library works well with\n[snapshot testing](https://github.com/bahmutov/snap-shot-it#use).\n\nAny library can be used\n([`snap-shot-it`](https://github.com/bahmutov/snap-shot-it),\n[Ava snapshots](https://github.com/avajs/ava/blob/master/docs/04-snapshot-testing.md),\n[Jest snapshots](https://jestjs.io/docs/en/snapshot-testing),\n[Node TAP snapshots](https://www.node-tap.org/snapshots/), etc.).\n\n<!-- eslint-disable max-nested-callbacks -->\n\n```js\nimport { each } from 'test-each'\n\n// The `output` is automatically set on the first run,\n// then re-used in the next runs.\neach(\n  [\n    { first: 2, second: 2 },\n    { first: 3, second: 3 },\n  ],\n  ({ title }, { first, second }) => {\n    test(`should multiply outputs | ${title}`, (t) => {\n      t.snapshot(multiply(first, second))\n    })\n  },\n)\n```\n\n### Side effects\n\nIf `callback`'s [parameters](#params) are directly modified, they should be\ncopied to prevent side effects for the next iterations.\n\n<!-- eslint-disable fp/no-mutation, no-param-reassign -->\n\n```js\nimport { each } from 'test-each'\n\neach(\n  ['green', 'red', 'blue'],\n  [{ active: true }, { active: false }],\n  (info, color, param) => {\n    // This should not be done, as the objects are re-used in several iterations\n    param.active = false\n\n    // But this is safe since it's a copy\n    const newParam = { ...param }\n    newParam.active = false\n  },\n)\n```\n\n### Iterables\n\n[`iterable()`](#iterableinputs) can be used to iterate over each combination\ninstead of providing a callback.\n\n<!-- eslint-disable fp/no-loops -->\n\n```js\nimport { iterable } from 'test-each'\n\nconst combinations = iterable(\n  ['green', 'red', 'blue'],\n  [{ active: true }, { active: false }],\n)\n\nfor (const [{ title }, color, param] of combinations) {\n  test(`should test color | ${title}`, () => {})\n}\n```\n\nThe return value is an\n[`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Iterables).\nThis can be converted to an array with the spread operator.\n\n<!-- eslint-disable max-nested-callbacks -->\n\n```js\nconst array = [...combinations]\n\narray.forEach(([{ title }, color, param]) => {\n  test(`should test color | ${title}`, () => {})\n})\n```\n\n# API\n\n## each(...inputs, callback)\n\n`inputs`: `Array | function | integer` (one or [several](#cartesian-product))\\\n`callback`: `(info, ...params) => void`\n\nFires `callback` with each combination of [`params`](#params).\n\n## iterable(...inputs)\n\n`inputs`: `Array | function | integer` (one or [several](#cartesian-product))\\\n_Return value_: [`Iterable<[info, ...params]>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Iterables)\n\nReturns an\n[`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Iterables)\nlooping through each combination of [`params`](#params).\n\n### info\n\n_Type_: `object`\n\n#### info.title\n\n_Type_: `string`\n\nLike [`params`](#params) but stringified. Should be used in\n[test titles](#test-titles).\n\n#### info.titles\n\n_Type_: `string[]`\n\nLike [`info.title`](#infotitle) but for each [`param`](#params).\n\n#### info.index\n\n_Type_: `integer`\n\nIncremented on each iteration. Starts at `0`.\n\n#### info.indexes\n\n_Type_: `integer[]`\n\nIndex of each [`params`](#params) inside each initial\n[`input`](#testeachinputs-callback).\n\n### params\n\n_Type_: `any` (one or [several](#cartesian-product))\n\nCombination of inputs for the current iteration.\n\n# Support\n\nFor any question, _don't hesitate_ to [submit an issue on GitHub](../../issues).\n\nEveryone is welcome regardless of personal background. We enforce a\n[Code of conduct](CODE_OF_CONDUCT.md) in order to promote a positive and\ninclusive environment.\n\n# Contributing\n\nThis project was made with ❤️. The simplest way to give back is by starring and\nsharing it online.\n\nIf the documentation is unclear or has a typo, please click on the page's `Edit`\nbutton (pencil icon) and suggest a correction.\n\nIf you would like to help us fix a bug or add a new feature, please check our\n[guidelines](CONTRIBUTING.md). Pull requests are welcome!\n\n<!-- Thanks go to our wonderful contributors: -->\n\n<!-- ALL-CONTRIBUTORS-LIST:START -->\n<!-- prettier-ignore -->\n<!--\n<table><tr><td align=\"center\"><a href=\"https://fosstodon.org/@ehmicky\"><img src=\"https://avatars2.githubusercontent.com/u/8136211?v=4\" width=\"100px;\" alt=\"ehmicky\"/><br /><sub><b>ehmicky</b></sub></a><br /><a href=\"https://github.com/ehmicky/test-each/commits?author=ehmicky\" title=\"Code\">💻</a> <a href=\"#design-ehmicky\" title=\"Design\">🎨</a> <a href=\"#ideas-ehmicky\" title=\"Ideas, Planning, & Feedback\">🤔</a> <a href=\"https://github.com/ehmicky/test-each/commits?author=ehmicky\" title=\"Documentation\">📖</a></td></tr></table>\n-->\n<!-- ALL-CONTRIBUTORS-LIST:END -->\n","publish_time":1661449425189,"_cnpm_publish_time":1661449425189,"dist-tags":{"latest":"7.0.1"}}