Unit testing with Jest is used to test some setup and helper functionality
Integration testing is performed with Jest and React Testing Library to test page functionality such as events and links.
Extend expect is installed and made available in
test-utils
. This provides many additional matchers
yarn run test
in the root will run all jest tests. Alternatively if run from a package directory
To mock graphql-hooks (including ) firstly mock the query files with the filenames.
jest.mock('../queries', () => ({
fooQuery: 'fooQuery',
...
}))
Then mock the query results based on the filename and options
For example To mock useQuery:
jest.mock('components', () => ({
...jest.requireActual('components'),
useQuery: (queryName, options) => {
const selectOption = optionsString =>
({
'fooQuery-{"id":null}': 'getShallowAssessmentData-null',
...
}[optionsString])
const mockUrl = selectOption(`${queryName}-${JSON.stringify(options)}`)
return {
data: require(`./__mocks__/foo-${mockUrl}.mock`).default,
loading: false,
refetch: false,
}
},
}))
Testing material-ui elements can provide challenges:
- Check boxes - It is neccessary to test the getter on the DOM and not the checked attribute. expect(inputElement.checked).toBe(true) and not expect(inputElement).toHaveAttribute('checked', true)
- Modals and select lists must be mocked as they are rendered outside of the container.
End-to-end tests are provided in ./packages/e2e-tests and run using TestCafe. The package requires environment variables in its .env file, see the Quick Start Guide for details.
The following scripts are available from the project root directory:
-
yarn dev:e2e-test
- runs e2e tests on localhost:8000 using headless chrome, saving screenshots on any failures -
yarn hosted:e2e-test
- runs e2e tests as above, but on the remote URL and basic credentials in e2e-tests's .env -
yarn ci:e2e-test
- used in CircleCI. Starts Gatsby on localhost:8000 then runs e2e tests in headless chrome, without screenshots
Scripts for running user-observable tests in non-headless browsers are provided in the package's
own package.json. cd packages/e2e-tests
then:
yarn chrome-local
yarn chrome-hosted
yarn firefox-local
yarn firefox-hosted