0.2.0-alpha • Published 5 years ago

lazy-jest v0.2.0-alpha

Weekly downloads
937
License
MIT
Repository
github
Last release
5 years ago

lazy-jest

npm version Build Status codecov

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


configArgs(initialConfig) ⇒ module:caseGenerator/configArgs.Args

Kind: Exported function

ParamType
initialConfigArray.<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)
ParamType
initialConfigArray.<ArgConfig>

args.arg(name, validCases, opts) ⇒ this

Add an argument

Kind: instance method of Args

ParamTypeDescription
namestring
validCasesArray.<*>valid test cases for this argument
optsObject.<string, *>other options

Properties

NameTypeDescription
opts.optionalbooleanflag to indicate whether this argument is optional
opts.invalidCasesArray.<*>invalid test cases for thsi argument

caseGenerator/configObjectArg


configObjectArg(propsConfig) ⇒ ArgConfig

A helper to generate object test cases by provided configurations

Kind: Exported function

ParamType
propsConfigArgs | 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

ParamTypeDescription
argsConfigArray.<ArgConfig>
invalidArgConfigIndexnumberIndex 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


enumerateCases(appendMethod, argsConfig, invalidArgConfigIndex) ⇒ Array.<(Array.<*>|Object)>

Generate cases by given configuration

Kind: Exported function

ParamTypeDescription
appendMethodappendMethodThis method will receive 2 input: existing case list, case value to extend. And should return extended case list.
argsConfigArray.<ArgConfig>
invalidArgConfigIndexnumberIndex 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

ParamTypeDescription
casesArray.<(Array.<*>|Object)>generated cases
nextArgCase*

testFunction


testFunction~testFunction(func, argsCases, testCaption)

Kind: inner method of testFunction

ParamType
funcfunction
argsCasesArray.<Array.<*>>
testCaptionstring

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

ParamTypeDescription
funcfunctionTarget function
argsConfigArgs | Array.<ArgConfig>
testCaptionstringTest 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

NameType
namestring
validCasesArray.<*>
invalidCasesArray.<*>
optionalboolean

0.2.0-alpha

5 years ago

0.1.0-beta.6

5 years ago

0.1.0-beta.5

5 years ago

0.1.0-beta.4

6 years ago

0.1.0-beta.3

6 years ago

0.1.0-beta.2

6 years ago

0.1.0-beta.1

6 years ago

0.1.0-beta

6 years ago