Skip to content

Latest commit

 

History

History
362 lines (244 loc) · 6.77 KB

File metadata and controls

362 lines (244 loc) · 6.77 KB
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()

BDD সিনট্যাক্স

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

অপশনাল ফ্ল্যাগ

--coverage = See a summary of test coverage

--detectOpenHandles = See a summary of ports that didn't close

--runInBand = Run all tests one after the other

এক্সপেক্ট

ব্যাসিক এক্সপেকটেশন

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

রেফারেন্স