/
🧪

Jest

javascriptreacttesting
On this page
  • Basic test skeleton

Basic test skeleton

js
describe('Given a xxx', () => {
describe('when it is rendered', () => {
test('it should ', () => {
// Arrange
// Act
// Assert
expect(true).toBe(true)
})
})
})

Use Jest globals

js
import { expect, test } from '@jest/globals'
test('two plus two is four', () => {
expect(2 + 2).toBe(4)
})

Throw

js
import { formatDate } from './formatDate'
test('it should throw', () => {
expect(() => formatDate('197901180')).toThrow(
'Date string wrong format/length',
)
expect(() => formatDate('abcdefgh')).toThrow(
'Date string must be only digits',
)
})
Edit this page
Want to make your own site like this?
Try gatsby-theme-code-notes by Zander Martineau.
Notes on code. My second brain, by Zander Martineau.