Unit Testing
Basic Unit Test
This is the basic unit test example, we have seen before but we can break this down a bit further
// __tests__/firstTest.test.ts
import { runTestGroup } from '@testickle/client'
import { expect } from 'chai'
runTestGroup(
'Text Tests',
{
beforeAll: () => {
const text = new Text({ text: 'Hello world', style: { fontSize: 96 } })
return { text }
},
'Create Text': ({ text }) => {
expect(text.text).to.equal('Hello world')
expect(text.style.fontSize).to.equal(96)
},
'Add static media to store': ({ text }) => {
mediaStore.addStaticMedia(text)
const mediaElement = text.mediaElement
expect(mediaElement, 'Media element is not undefined').to.be.not
.undefined
}
},
true
)
runTestGroup
This is using our basic runTestGroup
function, which excepts the following
type RunTestGroupType = (
groupName: string, // This is just the name of the test
tests: TestGroup,
exitOnFailure?: boolean | undefined // If the test should exit if it fails
) => Promise<TestGroupReturn>
TestGroup
A testGroup might look something like this
const testGroup = {
beforeAll : () => {
// set something up
return {data}
},
"Check data is valid" : ({data}) => {
// assert data is valid
},
afterAll: () => {
// clean up
}
}
You are excepted to write your tests inside customFunctions where the key
is the name of the test.
There are four special functions inside a TestGroup
, everything else is just a regular function.
beforeAll
This will execute before any other function executes.
Whatever you return
here will be accessible in all other functions
beforeEach
This will execute before every other function (except the four special functions)
Whatever you return
here will be accessible in all other functions
afterEach
This will execute after every other function (except the four special functions)
afterAll
This will be the last function to execute before returning
verifyGroups
A helper function to check if any tests have failed at the end of all your tests
import { runTestGroup, verifyGroups } from '@testickle/client'
const group1 = runTestGroup("Group1", ...)
const group2 = runTestGroup("Group2", ...)
verifyGroups([group1, group2])
Type definition for verifyGroups
const verifyGroups: (results: TestGroupReturn[] | Promise<TestGroupReturn>[]) => Promise<void>
runTests
This is a nice wrapper around runTestGroup
and verifyGroups
where you can run multiple test groups
import { runTests } from '@testickle/client'
runTests("My Test", {
"Group1" : {
beforeAll: () => {
// setup
return {data}
},
"Verify data": ({data}) => {
// verify data
},
...
},
"Group2" : ...,
})
Type Definition
type runTests = ({name:string, groups: Record<string, TestGroup>}) => Promise<void>