Creating a Test Automation Harness for Jest with Typescript
Scenario
We have a simple function contains
that tests whether a string
contains another:
/**
* 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
We will place our test data in a harness like this:
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
See the Firefly Semantics ValidatorTS
Tests: