| id | jest |
|---|---|
| title | জেস্ট |
| slug | jest |
| description | জেস্ট একটি অত্যন্ত সহজ এবং অসাধারণ জাভাস্ক্রিপ্ট টেস্ট ফ্রেমওয়ার্ক |
| colorPref |
yarn add --save-dev jest babel-jest/* Add to package.json */
"scripts": {
"test": "jest"
}# Run your tests
yarn test -- --watchদেখুনঃ Getting started
describe("My work", () => {
test("works", () => {
expect(2).toEqual(2);
});
});দেখুনঃ describe(), test(), expect()
describe('My work', () => {
it('works', () => {
···
})
})it is an alias for test.
দেখুনঃ test()
beforeEach(() => { ... })
afterEach(() => { ... })beforeAll(() => { ... })
afterAll(() => { ... })দেখুনঃ afterAll() and more
describe.only(···)
it.only(···) // alias: fit()দেখুনঃ test.only
describe.skip(···)
it.skip(···) // alias: xit()দেখুনঃ test.skip
expect(value).not.toBe(value).toEqual(value).toBeTruthy();Note that toEqual is a deep equality check.
দেখুনঃ expect()
expect(value).toMatchSnapshot().toMatchInlineSnapshot();Note that toMatchInlineSnapshot() requires Prettier to be set up for the project.
দেখুনঃ Inline snapshots
expect(value).toThrow(error).toThrowErrorMatchingSnapshot();expect(value).toBeFalsy().toBeNull().toBeTruthy().toBeUndefined().toBeDefined();expect(value)
.toBeCloseTo(number, numDigits)
.toBeGreaterThan(number)
.toBeGreaterThanOrEqual(number)
.toBeLessThan(number)
.toBeLessThanOrEqual(number);expect(value)
.toBeInstanceOf(Class)
.toMatchObject(object)
.toHaveProperty(keyPath, value);expect(value).toContain(item).toContainEqual(item).toHaveLength(number);expect(value).toMatch(regexpOrString);expect.extend(matchers);
expect.any(constructor);
expect.addSnapshotSerializer(serializer);
expect.assertions(1);test('works with promises', () => {
return new Promise((resolve, reject) => {
···
})
})test('works with async/await', async () => {
const hello = await foo()
···
})Return promises, or use async/await. দেখুনঃ Async tutorial
it("works", () => {
const output = something();
expect(output).toMatchSnapshot();
});First run creates a snapshot. Subsequent runs match the saved snapshot. দেখুনঃ Snapshot testing
import renderer from "react-test-renderer";it("works", () => {
const tree = renderer
.create(<Link page="http://www.facebook.com">Facebook</Link>)
.toJSON();
expect(tree).toMatchSnapshot();
});React's test renderer can be used for Jest snapshots. দেখুনঃ Snapshot test
jest.useFakeTimers();it("works", () => {
jest.runOnlyPendingTimers();
jest.runTimersToTime(1000);
jest.runAllTimers();
});দেখুনঃ Timer Mocks
const fn = jest.fn();const fn = jest.fn((n) => n * n);দেখুনঃ Mock functions
expect(fn)
.toHaveBeenCalled()
.toHaveBeenCalledTimes(number)
.toHaveBeenCalledWith(arg1, arg2, ...)
.toHaveBeenLastCalledWith(arg1, arg2, ...)expect(fn)
.toHaveBeenCalledWith(expect.anything())
.toHaveBeenCalledWith(expect.any(constructor))
.toHaveBeenCalledWith(expect.arrayContaining([values]))
.toHaveBeenCalledWith(expect.objectContaining({ props }))
.toHaveBeenCalledWith(expect.stringContaining(string))
.toHaveBeenCalledWith(expect.stringMatching(regexp));const Fn = jest.fn();
a = new Fn();
b = new Fn();Fn.mock.instances;
// → [a, b]দেখুনঃ .mock property
const fn = jest.fn();
fn(123);
fn(456);fn.mock.calls.length; // → 2
fn.mock.calls[0][0]; // → 123
fn.mock.calls[1][0]; // → 456দেখুনঃ .mock property
const fn = jest.fn(() => "hello");jest.fn().mockReturnValue("hello");
jest.fn().mockReturnValueOnce("hello");const fn = jest
.fn()
.mockImplementationOnce(() => 1)
.mockImplementationOnce(() => 2);fn(); // → 1
fn(); // → 2