Norman's Note 諾曼筆記

Norman's Note 諾曼筆記

Jest testing 測試

https://jestjs.io/docs/zh-Hans/api

  • Mock
    jest.doMock()
    jest.fn()

測試流程:
Local: localhost
DEV: 開發者用的機器
SIT: 測試資料、只有內網可以連 (or VPN)
UAT: production 資料、外網要先打帳號密碼才能連

常用 Methods

describe.each(table)(name, fn, timeout)

Use describe.each if you keep duplicating the same test suites with different data. describe.each allows you to write the test suite once and pass data in.

table: Array of Arrays with the arguments that are passed into the fn for each row.
name: String the title of the test suite.

fn: Function the suite of tests to be ran, this is the function that will receive the parameters in each row as function arguments.

describe.each([
  [1, 1, 2],
  [1, 2, 3],
  [2, 1, 3],
])('.add(%i, %i)', (a, b, expected) => {
  test(`returns ${expected}`, () => {
    expect(a + b).toBe(expected);
  });

  test(`returned value not be greater than ${expected}`, () => {
    expect(a + b).not.toBeGreaterThan(expected);
  });

  test(`returned value not be less than ${expected}`, () => {
    expect(a + b).not.toBeLessThan(expected);
  });
});