Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add predicate filter #21

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ it.each(items, 3, 1)(...)
it.each(items, 3, 2)(...)
```

## Custom filter predicate

You can filter the items by passing a predicate function

```js
it.each(items, (x, k) => ...)
// creates a test for every item the predicate returns a truthy value
```

## Exclusive tests

Normally you could run just a selected test using `it.only` or a suite of tests using `describe.only`. Similarly, you could skip a single test or a suite of tests using `it.skip` and `describe.skip` methods. These methods are NOT supported by `it.each` and `describe.each`. Thus if you want to only run the `it.each` tests, surround it with its own `describe` block.
Expand Down
19 changes: 19 additions & 0 deletions cypress/integration/filter-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// @ts-check
/// <reference types="cypress" />

import '../../src'

describe('filter function', () => {
const items = [1, 2, 3, 4]

it.each(items, (x, k) => x === 4)('only the last item matches %d', (x) => {
expect(x).to.equal(4)
})

it.each(items, (x, k) => k === 1)(
'only allows the 2nd item by index %d',
(x) => {
expect(x).to.equal(2)
},
)
})
5 changes: 3 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// https://github.com/bahmutov/cypress-each

type TestTitleFn<T> = (item: T, index: number, items: T[]) => string
type ItemPredicateFunction<T> = (item: T, index: number, items: T[]) => boolean

declare namespace Mocha {
type TestCallback<T> = (this: Context, arg0: T, arg1: any, arg2: any) => void
Expand All @@ -12,14 +13,14 @@ declare namespace Mocha {
* Iterates over each given item (optionally chunked), and creates
* a separate test for each one.
* @param values Input items to create the tests form OR number of times to repeat a test
* @param totalChunks (Optional) number of chunks to split the items into
* @param totalChunks (Optional) number of chunks to split the items into, or Nth filter, or a predicate function
* @param chunkIndex (Optional) index of the chunk to get items from
* @example it.each([1, 2, 3])('test %K', (x) => ...)
* @see https://github.com/bahmutov/cypress-each
*/
each<T = unknown>(
values: T[] | number,
totalChunks?: number,
totalChunks?: number | ItemPredicateFunction<T>,
chunkIndex?: number,
): (titlePattern: string | TestTitleFn<T>, fn: TestCallback<T>) => void
}
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ if (!it.each) {
) {
// take every Nth item
values = values.filter((_, k) => k % totalChunks === 0)
} else if (typeof totalChunks === 'function') {
// filter using the given predicate
values = values.filter(totalChunks)
}

values.forEach(function (value, k) {
Expand Down