lazy-jest
v0.2.0-alpha
Published
Jest util for lazy person like me to generate mass test snapshots with few configurations.
Downloads
21
Maintainers
Readme
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
export function testFunction(a, b) {
return a + b;
}
index.test.js
import { testFunction, enumerateArgsTestFunction, configArgs } from 'lazy-jest';
import { targetFunction } from '.';
// custom combinations
testFunction(targetFunction,
[
[1, 2],
[0, -1],
[0],
[]
],
'custom combination test of targetFunction()'
);
// enumerate test
const conf =
configArgs()
.arg('a', [1, 0, -1])
.arg('b', [-1, 0.9], { invalidCases: [ null ] });
enumerateArgsTestFunction(targetFunction, conf, 'enumerate test for targetFunction()');
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
Typedefs
caseGenerator/configArgs
- caseGenerator/configArgs
- configArgs(initialConfig) ⇒ module:caseGenerator/configArgs.Args ⏏
configArgs(initialConfig) ⇒ module:caseGenerator/configArgs.Args ⏏
Kind: Exported function
| Param | Type | | --- | --- | | initialConfig | Array.<ArgConfig> |
Example
// example ArgConfig for `func(a, b) {}`
configArgs()
.arg('a', [0, 1, 2])
.arg('b', [-1, -2], { optional: true, invalidCases: [0] });
configArgs~Args
Class for argument configurations
Kind: inner class of configArgs
new Args(initialConfig)
| Param | Type | | --- | --- | | initialConfig | Array.<ArgConfig> |
args.arg(name, validCases, [opts]) ⇒ this
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
configObjectArg(propsConfig) ⇒ ArgConfig ⏏
A helper to generate object test cases by provided configurations
Kind: Exported function
| Param | Type | | --- | --- | | propsConfig | Args | Array.<ArgConfig> |
Example
configObjectArg(
configArgs()
.arg('a', [1, 2], { invalidCases: [NaN, 0] })
.arg('b', ['a'], { invalidCases: ['', 1], optional: true })
);
// Result:
// {
// validCases: [
// { a: 1 },
// { a: 2 },
// { a: 1, b: a }
// ],
// invalidCases: [
// { a: NaN },
// { a: 0 },
// { a: 1, b: '' },
// { a: 1, b: 1 }
// ]
// }
caseGenerator/enumerateArrayCases
enumerateArrayCases ⇒ Array.<(Array.<*>|Object)> ⏏
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)>
- enumerateCases(appendMethod, argsConfig, invalidArgConfigIndex) ⇒ Array.<(Array.<*>|Object)> ⏏
enumerateCases(appendMethod, argsConfig, invalidArgConfigIndex) ⇒ Array.<(Array.<*>|Object)> ⏏
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
enumerateCases(extendArrayCase, [
{ name: 'a', validCases: [true], invalidCases: [false]},
{ name: 'b', validCases: [1], invalidCases: [2]}
]);
// Result: [[true, 1]]
enumerateCases(extendArrayCase, [
{ name: 'a', validCases: [true], invalidCases: [false]},
{ name: 'b', validCases: [1], invalidCases: [2], optional: true }
]);
// Result: [[true], [true, 1]]
enumerateCases(extendArrayCase, [
{ name: 'a', validCases: [true], invalidCases: [false, 0]},
{ name: 'b', validCases: [1, 2], invalidCases: [3] }
], 0);
// Result: [[false, 1], [0, 1]]
enumerateCases(extendArrayCase, [
{ name: 'a', validCases: [true], invalidCases: [false, 0]},
{ name: 'b', validCases: [1, 2], invalidCases: [3], optional: true }
], 0);
// Result: [[false], [0], [false, 1], [0, 1]]
enumerateCases~appendMethod ⇒ Array.<(Array.<*>|Object)>
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(targetFunction, [
[1, 2],
[0, -1],
[0],
[]
], 'custom combination test of targetFunction()');
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 = () => {};
enumerateArgsTestFunction(emptyFunc);
const simpleFunc = (param) => param;
enumerateArgsTestFunction(
simpleFunc,
configArgs().arg('param', [1], { invalidCases: [0] })
]);
const funcHasOptional = (param, opts) => opts || param;
enumerateArgsTestFunction(
funcHasOpts,
configArgs()
.arg('param', [1], { invalidCases: [0] })
.arg('opts', [2], { invalidCases: [0], optional: true })
);
const complexFunc = ({ param, opts }) => opts || param;
enumerateArgsTestFunction(
funcHasOpts,
configArgs().objectArg(
'param',
configArgs()
.arg('param', [1], { invalidCases: [0] })
.arg('opts', [2], { invalidCases: [0], optional: true })
)
);
ArgConfig : Object
Kind: global typedef
Properties
| Name | Type | | --- | --- | | name | string | | validCases | Array.<*> | | invalidCases | Array.<*> | | optional | boolean |