Add test utilities (skip, only, retries) to itParam
Title says it all, fixes #6.
This branch is based on #13, meaning it shouldn't be merged until that one's merged. I did this to avoid regressing the this-context shenanigans.
Defining these utility functions can be done manually (3f9f25c) or automatically (6521e2e), since Object.keys(it) (currently) returns just these three utility functions. This has the benefit of not having to update when mocha adds another utility function, but could potentially lead to undefined behavior if mocha adds something that is not a utility function.
The branch is currently contains the 'automatic' version, if you prefer the manual one I can revert it.
I opened this PR as a draft because I'm still working on the type definitions, unfortunately they're not as straightforward anymore.
I finally managed to get the type definitions right, again with a manual and automatic variant.
I just noticed that everything (defintions & code) concerning retries is probably wrong. I have never actually used that functionality before, and it looks like there's a mistake in the typescript definitions (which I based my code around). I'll investigate further...
After some investigation I can only conclude that a couple of things just don't seem to add up.
-
it.retries(x)(and thusitParam.retries) is valid, but errors at runtime- mocha allows
it.retries, thus my automatic version allowsitParam.retries. I'd see this as expected behavior (even though it leads to weird results here): everything you can call onityou can call onitParam. - I've created an issue for this over at the mocha typings to see what's going on here.
- mocha allows
-
Using
this.retries(x)either "next to"itParamor inside the callback works as I'd expect: all tests (each value) is repeatedxtimes (except when used inside an 'if' inside the callback). -
The return values of
itare not accessible to the user because we're running a loop. This means that the user can't use the properties/functions defined here and here (includingretries). I think it'd make sense to return an array of the return types of theitcalls (by changingforEachtomap). We could even add some additional functions to this array to allow for things likeitParam(...).retries(x)executingretrieson every element of the array.
In case you're unfamiliar with TypeScript, here's a quick (but pretty complete) overview of the language. A summary for the code I linked:
- An
interfacecan be seen as a 'description' of the types of properties in an object. - Properties marked with '
?' are optional. -
foo(bar: Baz): Quxdescribes a functionfoowith a parameterbarof typeBazand a return type ofQux. -
(parameters): Fooin an interface without property name means that the object itself is callable (a function). - The type
'a' | 'b'means that the property can only be"a"or"b".
Unfortunately it looks like this means that the automatic system won't work, because (as far as I know) there's no way in JS to determine if a function should be wrapped (only and skip) or simply passed through (retries).
What are your thoughts on this matter?