{"name":"test-each","version":"1.5.1","main":"build/src/main.js","scripts":{"test":"gulp test"},"husky":{"hooks":{"pre-push":"gulp check --full"}},"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","continuous-integration","es6","javascript","nodejs","library","iterable","foreach","functional-programming"],"license":"Apache-2.0","homepage":"https://git.io/fj4Nb","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":{"doc":"docs","lib":"src","test":"test"},"dependencies":{"core-js":"^3.1.4","fast-cartesian":"^1.3.0","pretty-format":"^24.8.0"},"devDependencies":{"ava":"^2.1.0","gulp":"^4.0.2","gulp-shared-tasks":"^0.30.17","husky":"^3.0.0","is-ci":"^2.0.0"},"engines":{"node":">=8.12.0"},"gitHead":"af24bf4bf82155a31b325ec42b634e7213be5cf8","_id":"test-each@1.5.1","_nodeVersion":"12.6.0","_npmVersion":"6.9.0","dist":{"integrity":"sha512-lurGth3/kYYAr37lAPmZhCLBYyIWQKm136Q1RswwM/ig2FiPWAJWbbLhJVJxquwynaDWqI/gteSBU59KsxxqLQ==","shasum":"80fcfa8b4b2d80efedd086781b073f900036bda9","tarball":"http://registry-npm.ywwl.com/test-each/download/test-each-1.5.1.tgz","fileCount":29,"unpackedSize":59633,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdI1gSCRA9TVsSAnZWagAAqkwP/2S1qvqfSgM8baIB/m94\nFPRGq+6ZuxhWEb6vUUFFYiZZ8bEnLJbMBZktVoN4qDizBqqZZVYqsI+qRDCx\nPjyb7srnejqIrjZaGTznKZpXihEaWjCXqDmZQ7l3GC7Veg9yihL5IARdua4f\nTxCIfZLbHsJnitMtKYrV5W7joLOV6NMyo9jcYYMNfxs9BdId7qC4+71+ZQIQ\nr/b8MdMAfA36U1/dzNcxJEQ/DKjyU4XlUNW8df/ehMkXkOp2+TtAu9HVqs4E\nw1GRHso4eD+R1xCUBijZULK4sLkAOueipDLvPv6ezU0VL4nawjlwRIjM8REP\ntSHxcrN9ueqMXasEelMA0mMegOiigkuAn2e224x5F2TRmIe1YpYRh6eleG5H\nfNHhQ6ELTv8Se8KSfIqxejwDK0qLK4yTgzRKsjVwzdWSBDKuSYQraibZlSLm\n8G7NoRE6Zhx5M9zeZqVY+KVtdPKHQzGbDnpA+6ppbzRTEWRaRQCSYxKrPknF\nBYBN4kofnCTzEQYW8rbIFvrD2oESlWtVOLtY31B4UX0kf+mhhSMC3b47vDUE\nO1PrGb4iTuKCAbZKAqxAAiA2hUZsB4HtZSFBU3Gg0zQ4zECxZb8sTu8vbcMq\nNJKUWBhpfEL6XP7FIT0m7YB0Ih0eA+GjFPn5qvJZUdKV7YlJgilEtYfw1K7Y\nii8a\r\n=IHoX\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCzknAB7C96y2Mw5+YbRgxCpz7kwdSOJEKBhDG5/uP43QIgHBHiR0P7U+9lHp2vnWnI2wMW3pANLapQNYaGUaFceOE="}],"size":18967,"noattachment":false,"key":"/test-each/-/test-each-1.5.1.tgz"},"maintainers":[{"name":"ehmicky","email":"ehmicky@gmail.com"}],"_npmUser":{"name":"ehmicky","email":"ehmicky@gmail.com"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/test-each_1.5.1_1562597394051_0.6470913889290384"},"_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":1562597394208,"_cnpm_publish_time":1562597394208,"dist-tags":{"latest":"7.0.1"}}