Skip to content

4、基础语法

SamYan edited this page May 11, 2018 · 1 revision

断言(expect)

// be and equal
expect(3 * 2).toBe(6)
expect({key: 'hello'}).toEqual({key: 'hello'}) // deep equal
expect(1).not.toBe(2)

// boolean
export(1 === 2).toBeFalsy()
expect(true).toBeTruthy()
expect(null).toBeNull()

// compare
expect(9).toBeGreaterThan(6)
expect(6).toBeGreaterThanOrEqual(6)
expect(3).toBeLessThan(6)
expect(3).toBeLessThanOrEqual(3)

// contain & property
expect(['jack', 'rose']).toContain('rose')
expect({animal: 'dog'}).toHaveProperty('animal', 'dog')

// also can extend own except
expect.extend({
toBeEven(received) {
  const even = (received % 2 === 0);
    if (event) {
      return {
        message: () => (`expected ${received} not to be even Number`),
        pass: true,
      };
    } else {
      return {
        message: () => (`expected ${received} to be even number`),
        pass: false,
      };
    }
  }
})
expect(10).toBeEven()

更多断言可查看官网:链接地址

内置方法

// 列举常用的方法
afterAll
afterEach
beforeAll
beforeEach
describe
it/test

更多内置方法可查看官网:链接地址

高级方法

// handle async methods expect
mockFn.mock.instances
mockFn.mock.calls
mockFn.mockReturnValueOnce
mockFn.mockResolvedValue
mockFn.mockRejectedValue

更多高级方法可查看官网:链接地址

Clone this wiki locally