describe('테스트 스위트(Suite)', () => {
test('테스트 케이스(Case)', () => {
// 테스트 내용
})
})
// SUITE
describe('진실은 진실하고, 거짓은 거짓이다.', () => {
// CASE 1
test('진실은 진실하다.', () => {
expect(true).toBe(true);
})
// CASE 2
test('거짓은 거짓이다.', () => {
expect(false).toBe(false);
})
})
import shoppingList from './shoppingList'
test('쇼핑리스트 기본 품목 중에는 `맥주`가 포함되어 있다.', () => {
expect(shoppingList()).toContain('맥주')
})
npm test
[FAIL] shoppingList.test.js
✕ 쇼핑리스트 기본 품목 중에는 `맥주`가 포함되어 있다. (2 ms)
● 쇼핑리스트 기본 품목 중에는 `맥주`가 포함되어 있다.
expect(received).toContain(expected) // indexOf
Expected value: "맥주"
Received array: []
const element = getByTestId('text-content')
expect(element).toHaveTextContent('Content')
expect(element).toHaveTextContent(/^Text Content$/) // 전체 내용 매칭
expect(element).toHaveTextContent(/content$/i) // 대/소문자 구분 없이 매칭
expect(element).not.toHaveTextContent('content')
<button aria-label="Close" aria-describedby="description-close">X</button>
<!-- 설명 -->
<div id="description-close">Closing will discard any changes</div>
<button>Delete</button>
const closeButton = getByRole('button', {name: 'Close'})
expect(closeButton).toHaveDescription('Closing will discard any changes')
expect(closeButton).toHaveDescription(/will discard/) // 일부 매칭
expect(closeButton).toHaveDescription(expect.stringContaining('will discard')) // 일부 매칭
expect(closeButton).toHaveDescription(/^closing/i) // 대/소문자 구분 없이 매칭
expect(closeButton).not.toHaveDescription('Other description')
const deleteButton = getByRole('button', {name: 'Delete'})
expect(deleteButton).not.toHaveDescription()
expect(deleteButton).toHaveDescription('') // 설명이 없거나, 비어 있으면 항상 빈 문자열