|
| 1 | +--- |
| 2 | +id: jest |
| 3 | +title: জেস্ট |
| 4 | +slug: jest |
| 5 | +description: জেস্ট একটি অত্যন্ত সহজ এবং অসাধারণ জাভাস্ক্রিপ্ট টেস্ট ফ্রেমওয়ার্ক |
| 6 | +colorPref: #333 |
| 7 | +--- |
| 8 | + |
| 9 | +## টেস্টিং |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +### শুরু করার জন্য |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +```bash |
| 18 | +yarn add --save-dev jest babel-jest |
| 19 | +``` |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +```js |
| 24 | +/* Add to package.json */ |
| 25 | +"scripts": { |
| 26 | + "test": "jest" |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +```bash |
| 31 | +# Run your tests |
| 32 | +yarn test -- --watch |
| 33 | +``` |
| 34 | + |
| 35 | +দেখুনঃ [Getting started](http://facebook.github.io/jest/docs/en/getting-started.html) |
| 36 | + |
| 37 | +### টেস্ট লেখার জন্য |
| 38 | + |
| 39 | +```js |
| 40 | +describe("My work", () => { |
| 41 | + test("works", () => { |
| 42 | + expect(2).toEqual(2); |
| 43 | + }); |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
| 47 | +দেখুনঃ [describe()](http://facebook.github.io/jest/docs/en/api.html#describename-fn), [test()](http://facebook.github.io/jest/docs/en/api.html#testname-fn), [expect()](http://facebook.github.io/jest/docs/en/expect.html#content) |
| 48 | + |
| 49 | +### BDD সিনট্যাক্স |
| 50 | + |
| 51 | +```js |
| 52 | +describe('My work', () => { |
| 53 | + it('works', () => { |
| 54 | + ··· |
| 55 | + }) |
| 56 | +}) |
| 57 | +``` |
| 58 | + |
| 59 | +`it` is an alias for `test`. |
| 60 | +দেখুনঃ [test()](http://facebook.github.io/jest/docs/en/api.html#testname-fn) |
| 61 | + |
| 62 | +### সেটাপ |
| 63 | + |
| 64 | +```js |
| 65 | +beforeEach(() => { ... }) |
| 66 | +afterEach(() => { ... }) |
| 67 | +``` |
| 68 | + |
| 69 | +```js |
| 70 | +beforeAll(() => { ... }) |
| 71 | +afterAll(() => { ... }) |
| 72 | +``` |
| 73 | + |
| 74 | +দেখুনঃ [afterAll() and more](http://facebook.github.io/jest/docs/en/api.html#afterallfn) |
| 75 | + |
| 76 | +### টেস্ট ফোকাসিং |
| 77 | + |
| 78 | +```js |
| 79 | +describe.only(···) |
| 80 | +it.only(···) // alias: fit() |
| 81 | +``` |
| 82 | + |
| 83 | +দেখুনঃ [test.only](http://facebook.github.io/jest/docs/en/api.html#testonlyname-fn) |
| 84 | + |
| 85 | +### স্কিপিং টেস্ট |
| 86 | + |
| 87 | +```js |
| 88 | +describe.skip(···) |
| 89 | +it.skip(···) // alias: xit() |
| 90 | +``` |
| 91 | + |
| 92 | +দেখুনঃ [test.skip](http://facebook.github.io/jest/docs/en/api.html#testskipname-fn) |
| 93 | + |
| 94 | +### অপশনাল ফ্ল্যাগ |
| 95 | + |
| 96 | +#### `--coverage` = See a summary of test coverage |
| 97 | +#### `--detectOpenHandles` = See a summary of ports that didn't close |
| 98 | +#### `--runInBand` = Run all tests one after the other |
| 99 | + |
| 100 | + |
| 101 | +## এক্সপেক্ট |
| 102 | + |
| 103 | + |
| 104 | +### ব্যাসিক এক্সপেকটেশন |
| 105 | + |
| 106 | +```js |
| 107 | +expect(value).not.toBe(value).toEqual(value).toBeTruthy(); |
| 108 | +``` |
| 109 | + |
| 110 | +Note that `toEqual` is a deep equality check. |
| 111 | +দেখুনঃ [expect()](http://facebook.github.io/jest/docs/en/expect.html#expectvalue) |
| 112 | + |
| 113 | +### স্ন্যাপশটস |
| 114 | + |
| 115 | +```js |
| 116 | +expect(value).toMatchSnapshot().toMatchInlineSnapshot(); |
| 117 | +``` |
| 118 | + |
| 119 | +Note that `toMatchInlineSnapshot()` requires Prettier to be set up for the project. |
| 120 | +দেখুনঃ [Inline snapshots](https://jestjs.io/docs/en/snapshot-testing#inline-snapshots) |
| 121 | + |
| 122 | +### ইরোর |
| 123 | + |
| 124 | +```js |
| 125 | +expect(value).toThrow(error).toThrowErrorMatchingSnapshot(); |
| 126 | +``` |
| 127 | + |
| 128 | +### বুলিয়ান |
| 129 | + |
| 130 | +```js |
| 131 | +expect(value).toBeFalsy().toBeNull().toBeTruthy().toBeUndefined().toBeDefined(); |
| 132 | +``` |
| 133 | + |
| 134 | +### নাম্বার |
| 135 | + |
| 136 | +```js |
| 137 | +expect(value) |
| 138 | + .toBeCloseTo(number, numDigits) |
| 139 | + .toBeGreaterThan(number) |
| 140 | + .toBeGreaterThanOrEqual(number) |
| 141 | + .toBeLessThan(number) |
| 142 | + .toBeLessThanOrEqual(number); |
| 143 | +``` |
| 144 | + |
| 145 | +### অবজেক্ট |
| 146 | + |
| 147 | +```js |
| 148 | +expect(value) |
| 149 | + .toBeInstanceOf(Class) |
| 150 | + .toMatchObject(object) |
| 151 | + .toHaveProperty(keyPath, value); |
| 152 | +``` |
| 153 | + |
| 154 | +### অব্জেক্টস |
| 155 | + |
| 156 | +```js |
| 157 | +expect(value).toContain(item).toContainEqual(item).toHaveLength(number); |
| 158 | +``` |
| 159 | + |
| 160 | +### স্ট্রিং |
| 161 | + |
| 162 | +```js |
| 163 | +expect(value).toMatch(regexpOrString); |
| 164 | +``` |
| 165 | + |
| 166 | +### অন্যান্য |
| 167 | + |
| 168 | +```js |
| 169 | +expect.extend(matchers); |
| 170 | +expect.any(constructor); |
| 171 | +expect.addSnapshotSerializer(serializer); |
| 172 | + |
| 173 | +expect.assertions(1); |
| 174 | +``` |
| 175 | + |
| 176 | +## আরো ফিচার |
| 177 | + |
| 178 | +### অ্যাসিক্রোনাস টেস্ট |
| 179 | + |
| 180 | +```js |
| 181 | +test('works with promises', () => { |
| 182 | + return new Promise((resolve, reject) => { |
| 183 | + ··· |
| 184 | + }) |
| 185 | +}) |
| 186 | +``` |
| 187 | + |
| 188 | + |
| 189 | + |
| 190 | +```js |
| 191 | +test('works with async/await', async () => { |
| 192 | + const hello = await foo() |
| 193 | + ··· |
| 194 | +}) |
| 195 | +``` |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | +Return promises, or use async/await. |
| 200 | +দেখুনঃ [Async tutorial](http://facebook.github.io/jest/docs/en/tutorial-async.html) |
| 201 | + |
| 202 | +### স্ন্যাপশট |
| 203 | + |
| 204 | +```jsx |
| 205 | +it("works", () => { |
| 206 | + const output = something(); |
| 207 | + expect(output).toMatchSnapshot(); |
| 208 | +}); |
| 209 | +``` |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +First run creates a snapshot. Subsequent runs match the saved snapshot. |
| 214 | +দেখুনঃ [Snapshot testing](http://facebook.github.io/jest/docs/en/snapshot-testing.html) |
| 215 | + |
| 216 | +### রিয়াক্ট টেস্ট রেন্ডারার |
| 217 | + |
| 218 | +```jsx |
| 219 | +import renderer from "react-test-renderer"; |
| 220 | +``` |
| 221 | + |
| 222 | + |
| 223 | + |
| 224 | +```jsx |
| 225 | +it("works", () => { |
| 226 | + const tree = renderer |
| 227 | + .create(<Link page="http://www.facebook.com">Facebook</Link>) |
| 228 | + .toJSON(); |
| 229 | + |
| 230 | + expect(tree).toMatchSnapshot(); |
| 231 | +}); |
| 232 | +``` |
| 233 | + |
| 234 | + |
| 235 | + |
| 236 | +React's test renderer can be used for Jest snapshots. |
| 237 | +দেখুনঃ [Snapshot test](http://facebook.github.io/jest/docs/en/tutorial-react-native.html#snapshot-test) |
| 238 | + |
| 239 | +### টাইমার |
| 240 | + |
| 241 | +```js |
| 242 | +jest.useFakeTimers(); |
| 243 | +``` |
| 244 | + |
| 245 | +```js |
| 246 | +it("works", () => { |
| 247 | + jest.runOnlyPendingTimers(); |
| 248 | + jest.runTimersToTime(1000); |
| 249 | + jest.runAllTimers(); |
| 250 | +}); |
| 251 | +``` |
| 252 | + |
| 253 | +দেখুনঃ [Timer Mocks](http://facebook.github.io/jest/docs/en/timer-mocks.html) |
| 254 | + |
| 255 | +## মক ফাংশন |
| 256 | + |
| 257 | +### মক ফাংশন |
| 258 | + |
| 259 | +```js |
| 260 | +const fn = jest.fn(); |
| 261 | +``` |
| 262 | + |
| 263 | +```js |
| 264 | +const fn = jest.fn((n) => n * n); |
| 265 | +``` |
| 266 | + |
| 267 | +দেখুনঃ [Mock functions](http://facebook.github.io/jest/docs/en/mock-functions.html#using-a-mock-function) |
| 268 | + |
| 269 | +### অ্যাসারশন |
| 270 | + |
| 271 | +```js |
| 272 | +expect(fn) |
| 273 | + .toHaveBeenCalled() |
| 274 | + .toHaveBeenCalledTimes(number) |
| 275 | + .toHaveBeenCalledWith(arg1, arg2, ...) |
| 276 | + .toHaveBeenLastCalledWith(arg1, arg2, ...) |
| 277 | +``` |
| 278 | + |
| 279 | +```js |
| 280 | +expect(fn) |
| 281 | + .toHaveBeenCalledWith(expect.anything()) |
| 282 | + .toHaveBeenCalledWith(expect.any(constructor)) |
| 283 | + .toHaveBeenCalledWith(expect.arrayContaining([values])) |
| 284 | + .toHaveBeenCalledWith(expect.objectContaining({ props })) |
| 285 | + .toHaveBeenCalledWith(expect.stringContaining(string)) |
| 286 | + .toHaveBeenCalledWith(expect.stringMatching(regexp)); |
| 287 | +``` |
| 288 | + |
| 289 | +### ইন্সটেন্স |
| 290 | + |
| 291 | +```js |
| 292 | +const Fn = jest.fn(); |
| 293 | + |
| 294 | +a = new Fn(); |
| 295 | +b = new Fn(); |
| 296 | +``` |
| 297 | + |
| 298 | +```js |
| 299 | +Fn.mock.instances; |
| 300 | +// → [a, b] |
| 301 | +``` |
| 302 | + |
| 303 | + |
| 304 | + |
| 305 | +দেখুনঃ [.mock property](http://facebook.github.io/jest/docs/en/mock-functions.html#mock-property) |
| 306 | + |
| 307 | +### কলস |
| 308 | + |
| 309 | +```js |
| 310 | +const fn = jest.fn(); |
| 311 | +fn(123); |
| 312 | +fn(456); |
| 313 | +``` |
| 314 | + |
| 315 | +```js |
| 316 | +fn.mock.calls.length; // → 2 |
| 317 | +fn.mock.calls[0][0]; // → 123 |
| 318 | +fn.mock.calls[1][0]; // → 456 |
| 319 | +``` |
| 320 | + |
| 321 | + |
| 322 | + |
| 323 | +দেখুনঃ [.mock property](http://facebook.github.io/jest/docs/en/mock-functions.html#mock-property) |
| 324 | + |
| 325 | +### রিটার্ন ভ্যালুস |
| 326 | + |
| 327 | +```js |
| 328 | +const fn = jest.fn(() => "hello"); |
| 329 | +``` |
| 330 | + |
| 331 | +#### অথবা: |
| 332 | + |
| 333 | +```js |
| 334 | +jest.fn().mockReturnValue("hello"); |
| 335 | +jest.fn().mockReturnValueOnce("hello"); |
| 336 | +``` |
| 337 | + |
| 338 | +### মক ইমপ্লিমেনশন |
| 339 | + |
| 340 | +```js |
| 341 | +const fn = jest |
| 342 | + .fn() |
| 343 | + .mockImplementationOnce(() => 1) |
| 344 | + .mockImplementationOnce(() => 2); |
| 345 | +``` |
| 346 | + |
| 347 | + |
| 348 | + |
| 349 | +```js |
| 350 | +fn(); // → 1 |
| 351 | +fn(); // → 2 |
| 352 | +``` |
| 353 | + |
| 354 | +## রেফারেন্স |
| 355 | + |
| 356 | + |
| 357 | + |
| 358 | +- <https://devhints.io/jest> |
| 359 | + |
| 360 | + |
| 361 | +- <http://facebook.github.io/jest/> |
| 362 | + |
0 commit comments