diff --git a/README.md b/README.md index a2fae3b..bf28e1a 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,44 @@ Find the implementation in [src/index.js](./src/index.js) - [it-each-spec.js](./cypress/integration/it-each-spec.js) uses the `it.each` helper to generate multiple `it` tests given a data array - [describe-each-spec.js](./cypress/integration/describe-each-spec.js) uses `describe.each` helper to create `describe` blocks for each item in the given data array - [mocha-each-spec.js](cypress/integration/mocha-each-spec.js) uses 3rd party [mocha-each](https://github.com/ryym/mocha-each) to generate `it` tests for each data item + +## Small print + +Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2021 + +- [@bahmutov](https://twitter.com/bahmutov) +- [glebbahmutov.com](https://glebbahmutov.com) +- [blog](https://glebbahmutov.com/blog) +- [videos](https://www.youtube.com/glebbahmutov) +- [presentations](https://slides.com/bahmutov) +- [cypress.tips](https://cypress.tips) + +License: MIT - do anything with the code, but don't blame me if it does not work. + +Support: if you find any problems with this module, email / tweet / +[open issue](https://github.com/bahmutov/cypress-each/issues) on Github + +## MIT License + +Copyright (c) 2021 Gleb Bahmutov <gleb.bahmutov@gmail.com> + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/cypress/expected.json b/cypress/expected.json index 7e0179c..2e06f58 100644 --- a/cypress/expected.json +++ b/cypress/expected.json @@ -24,5 +24,9 @@ "adds null and 10 then returns NaN": "passed", "adds {} and [] then returns NaN": "passed" } + }, + "format": { + "I am a person": "passed", + "I am a person at 42": "passed" } } diff --git a/cypress/integration/format-spec.js b/cypress/integration/format-spec.js new file mode 100644 index 0000000..b6790f1 --- /dev/null +++ b/cypress/integration/format-spec.js @@ -0,0 +1,23 @@ +/// + +import '../..' + +describe('format', () => { + const person = { + name: 'Joe', + } + + // it should only use the number of arguments + // in the string format, and not all available arguments + it.each([['person', person, 42]])('I am a %s', (name, who, life) => { + expect(name).to.equal('person') + expect(who).to.equal(person) + expect(life).to.equal(42) + }) + + it.each([['person', 42, person]])('I am a %s at %d', (name, life, who) => { + expect(name).to.equal('person') + expect(who).to.equal(person) + expect(life).to.equal(42) + }) +}) diff --git a/src/index.js b/src/index.js index 759a65c..e4d5e6a 100644 --- a/src/index.js +++ b/src/index.js @@ -3,16 +3,23 @@ // standard Node module "util" has "format" function const { format } = require('util') +function formatTitle(pattern, ...values) { + // count how many format placeholders are in the pattern + // by counting the "%" characters + const count = pattern.match(/%/g).length + return format.apply(null, [pattern].concat(values.slice(0, count))) +} + if (!it.each) { it.each = (values) => { return function (titlePattern, testCallback) { values.forEach((value) => { // define a test for each value if (Array.isArray(value)) { - const title = format(titlePattern, ...value) + const title = formatTitle(titlePattern, ...value) it(title, testCallback.bind(null, ...value)) } else { - const title = format(titlePattern, value) + const title = formatTitle(titlePattern, value) it(title, testCallback.bind(null, value)) } }) @@ -26,10 +33,10 @@ if (!describe.each) { // define a test for each value values.forEach((value) => { if (Array.isArray(value)) { - const title = format(titlePattern, ...value) + const title = formatTitle(titlePattern, ...value) describe(title, testCallback.bind(null, ...value)) } else { - const title = format(titlePattern, value) + const title = formatTitle(titlePattern, value) describe(title, testCallback.bind(null, value)) } })