1-6 Verify Custom JavaScript Tests with Jest

Verify Custom JavaScript Tests with Jest

Up to this point we’ve created all our own utilities. As it turns out, the utilities we’ve created mirror the utilities provided by Jest perfectly! Let’s install Jest and use it to run our test!

提取码:mdij

观看视频

Code

Transcript

The testing framework we've written actually looks remarkably a lot like Jest. Rather than running node --require, and then instead of globals and so on and so forth, we could actually just use Jest directly.

If I run npx jest,

Terminal

npx jest

jest will automatically pick up our jest.test.js file based off of that convention.

jest.test.js

const {sumAsync, subtractAsync} = require('../math')

test('sumAsync adds numbers asynchronously', async () => {
  const result = await sumAsync(3, 7)
  const expected = 10
  expect(result).toBe(expected)
})

test('subtractAsync subtracts numbers asynchronously', async () => {
  const result = await subtractAsync(7, 3)
  const expected = 4
  expect(result).toBe(expected)
})

It will show us really helpful error messages, and even a code frame to show us exactly where in our code that error was thrown.

image

These are some of the things that make jest such an awesome testing framework because the error messages are so clear.

star该项目

你可能感兴趣的:(1-6 Verify Custom JavaScript Tests with Jest)