auto complete
npm i -D @types/jest
{
"typeAcquisition": {
"include": ["jest"]
}
}
coverage
npm run test -- --coverage
unit test
describe(('테스트') => {
let update;
beforeEach(() => {
update = jest.fn();
})
it(('테스트') => {
expect().toBe()
expect().toEqual()
expect().toHaveBeenCalledTimes()
expect().toHaveBeenCalledWith(arg1, arg2)
expect(() => callback()).toThrow()
})
})
component test
import React from "react";
import { render, screen } from "@testing-library/react";
import HabitAddForm from "../habitAddForm";
import userEvent from "@testing-library/user-event";
describe("HabitAddForm", () => {
let onAdd;
let input;
let button;
beforeEach(() => {
onAdd = jest.fn();
input = screen.getByPlaceholderText("Habit");
button = screen.getByText("Add");
render(<HabitAddForm onAdd={onAdd} />);
});
it("calls onAdd when button is clicked and valid habit is entered", () => {
fireEvent.click(screen.getByTitle('add'))
fireEvent.click(screen.getAllByTitle("reset")[0]);
fireEvent.change(input, { target: { value: "hi" } });
expect(onAdd).toHaveBeenCalledWith("New Habit");
expect(onAdd).toHaveBeenCalledTimes(1);
const addedCount = screen.getAllByTestId("habit-count")[3];
expect(addedCount.innerHTML).toBe("0");
expect().toBeInTheDocument();
});
});
snapshot test
import renderer from "react-test-renderer";
test("renders", () => {
const component = renderer.create(<HabitAddForm onAdd={jest.fn()} />);
expect(component.toJSON()).toMatchSnapshot();
});