GeezerVison is showing github.com — Pass an undefined title argument to macro-title functions · avajs/ava@aa35f15
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/recipes/typescript.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -66,7 +66,7 @@ import test, {Macro} from 'ava'; | 66 | 66 | const macro: Macro = (t, input: string, expected: number) => { | 67 | 67 | t.is(eval(input), expected); | 68 | 68 | }; | 69 | | -macro.title = (providedTitle: string, input: string, expected: number) => `${providedTitle} ${input} = ${expected}`.trim(); | | 69 | +macro.title = (providedTitle = '', input: string, expected: number) => `${providedTitle} ${input} = ${expected}`.trim(); | 70 | 70 |
| 71 | 71 | test(macro, '2 + 2', 4); | 72 | 72 | test(macro, '2 * 3', 6); | |
Collapse file Copy file name to clipboardExpand all lines: index.d.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -390,13 +390,13 @@ export interface Macro<Context = {}> { | 390 | 390 | * Implement this function to generate a test (or hook) title whenever this macro is used. `providedTitle` contains | 391 | 391 | * the title provided when the test or hook was declared. Also receives the remaining test arguments. | 392 | 392 | */ | 393 | | - title?: (providedTitle: string, ...args: Array<any>) => string; | | 393 | + title?: (providedTitle: string | undefined, ...args: Array<any>) => string; | 394 | 394 | } | 395 | 395 |
| 396 | 396 | /** A reusable test or hook implementation, for tests & hooks declared with the `.cb` modifier. */ | 397 | 397 | export interface CbMacro<Context = {}> { | 398 | 398 | (t: CbExecutionContext<Context>, ...args: Array<any>): ImplementationResult; | 399 | | - title?: (providedTitle: string, ...args: Array<any>) => string; | | 399 | + title?: (providedTitle: string | undefined, ...args: Array<any>) => string; | 400 | 400 | } | 401 | 401 |
| 402 | 402 | export interface TestInterface<Context = {}> { | |
Collapse file Copy file name to clipboardExpand all lines: index.js.flow +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -403,13 +403,13 @@ export interface Macro<Context = {}> { | 403 | 403 | * Implement this function to generate a test (or hook) title whenever this macro is used. `providedTitle` contains | 404 | 404 | * the title provided when the test or hook was declared. Also receives the remaining test arguments. | 405 | 405 | */ | 406 | | - title?: (providedTitle: string, ...args: Array<any>) => string; | | 406 | + title?: (providedTitle: string | void, ...args: Array<any>) => string; | 407 | 407 | } | 408 | 408 |
| 409 | 409 | /** A reusable test or hook implementation, for tests & hooks declared with the `.cb` modifier. */ | 410 | 410 | export interface CbMacro<Context = {}> { | 411 | 411 | (t: CbExecutionContext<Context>, ...args: Array<any>): ImplementationResult; | 412 | | - title?: (providedTitle: string, ...args: Array<any>) => string; | | 412 | + title?: (providedTitle: string | void, ...args: Array<any>) => string; | 413 | 413 | } | 414 | 414 |
| 415 | 415 | export interface TestInterface<Context = {}> { | |
Collapse file Copy file name to clipboardExpand all lines: lib/runner.js +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,7 +55,7 @@ class Runner extends Emittery { | 55 | 55 |
| 56 | 56 | const specifiedTitle = typeof args[0] === 'string' ? | 57 | 57 | args.shift() : | 58 | | - ''; | | 58 | + undefined; | 59 | 59 | const implementations = Array.isArray(args[0]) ? | 60 | 60 | args.shift() : | 61 | 61 | args.splice(0, 1); | @@ -65,7 +65,7 @@ class Runner extends Emittery { | 65 | 65 | throw new TypeError('`todo` tests are not allowed to have an implementation. Use `test.skip()` for tests with an implementation.'); | 66 | 66 | } | 67 | 67 |
| 68 | | - if (specifiedTitle === '') { | | 68 | + if (specifiedTitle === undefined || specifiedTitle === '') { | 69 | 69 | throw new TypeError('`todo` tests require a title'); | 70 | 70 | } | 71 | 71 |
| @@ -97,14 +97,14 @@ class Runner extends Emittery { | 97 | 97 |
| 98 | 98 | for (const implementation of implementations) { | 99 | 99 | let title = implementation.title ? | 100 | | - implementation.title.apply(implementation, [specifiedTitle].concat(args)) : | | 100 | + implementation.title(specifiedTitle, ...args) : | 101 | 101 | specifiedTitle; | 102 | 102 |
| 103 | | - if (typeof title !== 'string') { | | 103 | + if (title !== undefined && typeof title !== 'string') { | 104 | 104 | throw new TypeError('Test & hook titles must be strings'); | 105 | 105 | } | 106 | 106 |
| 107 | | - if (title === '') { | | 107 | + if (title === undefined || title === '') { | 108 | 108 | if (metadata.type === 'test') { | 109 | 109 | throw new TypeError('Tests must have a title'); | 110 | 110 | } else if (metadata.always) { | |
Collapse file Copy file name to clipboardExpand all lines: readme.md +2-2Lines changed: 2 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -691,14 +691,14 @@ function macro(t, input, expected) { | 691 | 691 | t.is(eval(input), expected); | 692 | 692 | } | 693 | 693 |
| 694 | | -macro.title = (providedTitle, input, expected) => `${providedTitle} ${input} = ${expected}`.trim(); | | 694 | +macro.title = (providedTitle = '', input, expected) => `${providedTitle} ${input} = ${expected}`.trim(); | 695 | 695 |
| 696 | 696 | test(macro, '2 + 2', 4); | 697 | 697 | test(macro, '2 * 3', 6); | 698 | 698 | test('providedTitle', macro, '3 * 3', 9); | 699 | 699 | ``` | 700 | 700 |
| 701 | | -The `providedTitle` argument defaults to an empty string if the user does not supply a string title. This allows for easy concatenation without having to worry about `null` / `undefined`. It is worth remembering that the empty string is considered a falsy value, so you can still use `if(providedTitle) {...}`. | | 701 | +The `providedTitle` argument defaults to `undefined` if the user does not supply a string title. This means you can use a parameter assignment to set the default value. The example above uses the empty string as the default. | 702 | 702 |
| 703 | 703 | You can also pass arrays of macro functions: | 704 | 704 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/runner.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -611,7 +611,7 @@ test('macros: Customize test names attaching a `title` function', t => { | 611 | 611 | avaT.pass(); | 612 | 612 | } | 613 | 613 |
| 614 | | - macroFn.title = (title, firstArg) => (title || 'default') + firstArg; | | 614 | + macroFn.title = (title = 'default', firstArg) => title + firstArg; | 615 | 615 |
| 616 | 616 | return promiseEnd(new Runner(), runner => { | 617 | 617 | runner.on('stateChange', evt => { | |
|