-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add make test title function support (#9)
* add test title function spec * feat: implement test title function * add readme section
- Loading branch information
Showing
5 changed files
with
126 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// @ts-check | ||
/// <reference types="cypress" /> | ||
|
||
import '../../src' | ||
|
||
/** | ||
* Forms the test title from the item and index | ||
* @param {string} s Individual string | ||
* @param {number} k Item index (0-based) | ||
* @param {string[]} strings All strings | ||
* @returns | ||
*/ | ||
function makeTestTitle(s, k, strings) { | ||
expect(s, 'first argument is the item').to.equal('foo') | ||
expect(k, 'second argument is the index').to.equal(0) | ||
expect(strings, 'all items').to.deep.equal(['foo']) | ||
|
||
return `test ${k + 1} for "${s}"` | ||
} | ||
|
||
describe('Form test title using a function', () => { | ||
it.each(['foo'])(makeTestTitle, function (s) { | ||
expect(s, 'item value').to.equal('foo') | ||
// @ts-ignore | ||
expect(this.test.title, 'computed test title').to.equal('test 1 for "foo"') | ||
}) | ||
|
||
it.each([ | ||
{ name: 'Joe', age: 30 }, | ||
{ name: 'Mary', age: 20 }, | ||
])( | ||
(person) => `tests person ${person.name}`, | ||
function (user) { | ||
expect(user).to.have.keys('name', 'age') | ||
// @ts-ignore | ||
expect(this.test.title, 'computed test title').to.be.oneOf([ | ||
'tests person Joe', | ||
'tests person Mary', | ||
]) | ||
}, | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
// types for it.each and describe.each | ||
// any help improving them is welcome | ||
// https://github.com/bahmutov/cypress-each | ||
|
||
type TestTitleFn<T> = (item: T, index: number, items: T[]) => string | ||
|
||
declare namespace Mocha { | ||
type TestCallback<T> = (this: Context, arg0: T, arg1: any, arg2: any) => void | ||
|
||
interface TestFunction { | ||
// definition for it.each | ||
each<T = unknown>( | ||
values: T[], | ||
): (titlePattern: string, fn: TestCallback<T>) => void | ||
): (titlePattern: string | TestTitleFn<T>, fn: TestCallback<T>) => void | ||
} | ||
|
||
interface SuiteFunction { | ||
// definition for describe.each | ||
each<T = unknown>( | ||
values: T[], | ||
): (titlePattern: string, fn: TestCallback<T>) => void | ||
): (titlePattern: string | TestTitleFn<T>, fn: TestCallback<T>) => void | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters