Skip to content

Commit

Permalink
feat: only use the number of format placeholders in the title
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Oct 12, 2021
1 parent c45aefb commit 784ff10
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]> © 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 <[email protected]>

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.
4 changes: 4 additions & 0 deletions cypress/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
23 changes: 23 additions & 0 deletions cypress/integration/format-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference types="cypress" />

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)
})
})
15 changes: 11 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})
Expand All @@ -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))
}
})
Expand Down

0 comments on commit 784ff10

Please sign in to comment.