mocha-param icon indicating copy to clipboard operation
mocha-param copied to clipboard

Proposal: descriptive (non-value) but distinct names for tests

Open HoldYourWaffle opened this issue 5 years ago • 2 comments

It's great that we can use ${value} inside test names, but sometimes value isn't a very descriptive name for a test. To get around this, I often write tests like this:

itParam('${value[0]}', [
    [ 'descriptive name for a', a ],
    [ 'descriptive name for b', b ]
], function([, value ]) {
    // ...
})

This works absolutely fine, but isn't very pleasing to the eyes. Therefore, I propose to add the following "shorthand" syntax:

itParam([
    [ 'descriptive name for a', a ],
    [ 'descriptive name for b', b ]
], function(value) { // notice the lack of an ugly destructing operator
    // ...
})

The lack of a name parameter would signal itParam to use the first element of each "data element" as testname, and only pass the second to the callback.

HoldYourWaffle avatar Apr 02 '20 15:04 HoldYourWaffle

Just like #15 this might introduce parsing difficulties, although I think it's easier to differentiate between a string or array as the first parameter.

To avoid this complication the syntax could be changed to something like this:

itParam.named([
    [ 'descriptive name for a', a ],
    [ 'descriptive name for b', b ]
], function(value) {
    // ...
})

HoldYourWaffle avatar Apr 02 '20 15:04 HoldYourWaffle

We could even eliminate the inner-arrays like so:

itParam.named([
    'descriptive name for a', a,
    'descriptive name for b', b
], function(value) {
    // ...
})

And "split" the arguments into groups of two. However, I think this can't be done type-safely (in the context of TypeScript), so this wouldn't be my preferred option.

HoldYourWaffle avatar Apr 02 '20 19:04 HoldYourWaffle