lazy-jest
Jest util for lazy person like me to generate mass test snapshots with few configurations. It only supports function test now
NOTE that please use it wisely, otherwise your snapshot will explode.
Quick start
index.js
{ return a + b;}
index.test.js
;; // custom combinations; // enumerate testconst conf = ;;
How does enumerate test work?
First of all, testing is to verify input samples will always match corresponding output. To know what is the correct output, you need either calculate by yourself, or capture the snapshot of correct output. Which is an owesome feature introduced by jest.
For functions, input is simply a set of arguments. So why not I just provide a list of valid or invalid cases, and let JS to generate all the combinations as test cases and capture snapshot?
To be strict like lab experiments, I choose to test a function in following way (which I called enumerate test):
- If no compulsory args, test function with empty arguments:
func()
- If there are compulsory args, test together with invalid case for one of the args, and valid cases for the rest. Like this:
func(invalid, valid)
func(valid, invalid)
- If there are optional args, test in above way by add in optional args one by one. Like this:
func(compulsory_invalid, compulsory_valid, optional_valid)
func(compulsory_valid, compulsory_invalid, optional_valid)
func(compulsory_valid, compulsory_valid, optional_invalid)
func(compulsory_invalid, compulsory_valid, optional_valid, optional_valid)
func(compulsory_valid, compulsory_invalid, optional_valid, optional_valid)
func(compulsory_valid, compulsory_valid, optional_invalid, optional_valid)
func(compulsory_valid, compulsory_valid, optional_valid, optional_invalid)
- And then test all the valid combinations
API
Modules
- caseGenerator/configArgs
- caseGenerator/configObjectArg
- caseGenerator/enumerateArrayCases
- caseGenerator/enumerateCases
- testFunction
Typedefs
- ArgConfig :
Object
caseGenerator/configArgs
- caseGenerator/configArgs
- configArgs(initialConfig) ⇒
module:caseGenerator/configArgs.Args
⏏
- configArgs(initialConfig) ⇒
module:caseGenerator/configArgs.Args
⏏
configArgs(initialConfig) ⇒ Kind: Exported function
Param | Type |
---|---|
initialConfig | Array.<ArgConfig> |
Example
// example ArgConfig for `func(a, b) {}`;
configArgs~Args
Class for argument configurations
Kind: inner class of configArgs
new Args(initialConfig)
Param | Type |
---|---|
initialConfig | Array.<ArgConfig> |
this
args.arg(name, validCases, [opts]) ⇒ Add an argument
Kind: instance method of Args
Param | Type | Description |
---|---|---|
name | string |
|
validCases | Array.<*> |
valid test cases for this argument |
[opts] | Object.<string, *> |
other options |
Properties
Name | Type | Description |
---|---|---|
[opts.optional] | boolean |
flag to indicate whether this argument is optional |
[opts.invalidCases] | Array.<*> |
invalid test cases for thsi argument |
caseGenerator/configObjectArg
ArgConfig
⏏
configObjectArg(propsConfig) ⇒ A helper to generate object test cases by provided configurations
Kind: Exported function
Param | Type |
---|---|
propsConfig | Args | Array.<ArgConfig> |
Example
;// Result:// {// validCases: [// { a: 1 },// { a: 2 },// { a: 1, b: a }// ],// invalidCases: [// { a: NaN },// { a: 0 },// { a: 1, b: '' },// { a: 1, b: 1 }// ]// }
caseGenerator/enumerateArrayCases
Array.<(Array.<*>|Object)>
⏏
enumerateArrayCases ⇒ Kind: Exported constant
See: enumerateCases
Param | Type | Description |
---|---|---|
argsConfig | Array.<ArgConfig> |
|
invalidArgConfigIndex | number |
Index of arg in the config list to have invalid case. If this is not set, it will generate cases that all arguments are valid. |
caseGenerator/enumerateCases
- caseGenerator/enumerateCases
- enumerateCases(appendMethod, argsConfig, invalidArgConfigIndex) ⇒
Array.<(Array.<*>|Object)>
⏏- ~appendMethod ⇒
Array.<(Array.<*>|Object)>
- ~appendMethod ⇒
- enumerateCases(appendMethod, argsConfig, invalidArgConfigIndex) ⇒
Array.<(Array.<*>|Object)>
⏏
enumerateCases(appendMethod, argsConfig, invalidArgConfigIndex) ⇒ Generate cases by given configuration
Kind: Exported function
Param | Type | Description |
---|---|---|
appendMethod | appendMethod |
This method will receive 2 input: existing case list, case value to extend. And should return extended case list. |
argsConfig | Array.<ArgConfig> |
|
invalidArgConfigIndex | number |
Index of arg in the config list to have invalid case. If this is not set, it will generate cases that all arguments are valid. |
Example
;// Result: [[true, 1]] ;// Result: [[true], [true, 1]] ;// Result: [[false, 1], [0, 1]] ;// Result: [[false], [0], [false, 1], [0, 1]]
Array.<(Array.<*>|Object)>
enumerateCases~appendMethod ⇒ Kind: inner typedef of enumerateCases
Param | Type | Description |
---|---|---|
cases | Array.<(Array.<*>|Object)> |
generated cases |
nextArgCase | * |
testFunction
testFunction~testFunction(func, [argsCases], [testCaption])
Kind: inner method of testFunction
Param | Type |
---|---|
func | function |
[argsCases] | Array.<Array.<*>> |
[testCaption] | string |
Example
;
testFunction~enumerateArgsTestFunction(func, argsConfig, [testCaption])
Catch snapshot of a function. It will do following tests:
- If no compulsory arguments defined, it will test empty argument case
- Test compulsory arguments first, and add in optional argument test cases one by one
- Test invalid cases by using arguments with one invalid argument, and first valid case for others
- Test all valid combinations of arguments
Kind: inner method of testFunction
Param | Type | Description |
---|---|---|
func | function |
Target function |
argsConfig | Args | Array.<ArgConfig> |
|
[testCaption] | string |
Test description |
Example
const emptyFunc = {};; const simpleFunc = param;; const funcHasOptional = opts || param;; const complexFunc = opts || param;;
Object
ArgConfig : Kind: global typedef
Properties
Name | Type |
---|---|
name | string |
validCases | Array.<*> |
invalidCases | Array.<*> |
optional | boolean |