Creating a Test Automation Harness for Jest with Typescript

Scenario

/**
* Test whether the target string contains the contained string.
*
* @param target The string that should contains the element
* @param contained The possibly contained string
* @return true if the `target` string contains the `contained` string, false otherwise
* @example
* ```
* expect(contains('foobar', 'foo')).toBeTruthy()
* ```
*/
export function contains(target: string, contained: string):boolean {
return target.indexOf(contained) >= 0;
}

And we want to test it by passing by passing in valid and invalid data to a test function like this:

test('contains', ()=>{
runtest(testdata, contains)
})

Approach

Harness

const testdata: TestData[] = [{it: 'should test contains',args: ['foo'],valid: ['foo', 'foobar', 'bazfoo'],invalid: ['bar', 'fobar'],}]

Note that the args contains the second argument we are passing to contains .

If multiple arguments are being tested we can pass them in via each . For example:

each: ['baz', 'bar', 'beech']

The TestData interface

export interface TestData {it: stringargs?: anyeach?: any[]valid: any[],invalid: any[]}

Test Utility Function

More Examples

--

--

Founder of Firefly Semantics Corporation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store