03|匹配器

2020-05-21  本文已影响0人  雪燃归来

一、持续监听

package.json

scripts:{
  "test":"jest --watchAll"
}

二、匹配器

1、 toBe 匹配器 matchers
test('测试10与10相匹配',() => {
     // expect(10).toBe(10)
    // const a = { one: 1}
    // expect(a).toBe({ one: 1})
})

test('测试对象内容相等',() => {
    const a = { one: 1}
    expect(a).toEqual({one: 1})
})

test('是否为null',() => {
    const a = null;
    expect(a).toBeNull()
})

// 真假相关
test('是否为null',() => {
    const a = null;
    expect(a).toBeNull()
})

test('toBeUndefined',() =>{
    const a = undefined
    expect(a).toBeUndefined();
})

test('toBeDefined匹配器',() => {
    const a = null;
    expect(a).toBeDefined()
})

test('toBeTruthy 匹配器', () => {
    const a = 1
    expect(a).toBeTruthy()
})

test('toBeFalsy 匹配器',() => {
    const a = null
    expect(a).toBeFalsy()
})

test('not 匹配器',() => {
    const a = 1;
    expect(a).not.toBeFalsy()
})

// 数字相关的
test('toBeGreaterThan',() => {
    const count = 10;
    expect(count).toBeGreaterThan(9)
})

test('toBeLessThan', () => {
    const count = 10;
    expect(count).toBeLessThan(11)
})
test('toBeGreaterThanOrEqual', () => {
    const count = 10;
    expect(count).toBeGreaterThanOrEqual(10)
})

test('toBeCloseTo', () => {
    const a = 0.1;
    const b = 0.2;
    expect(a+b).toBeCloseTo(0.3)
})

// 跟字符串相关的匹配器
test('toMatch', () => {
    const str = 'http://www.dell-lee.com'
    expect(str).toMatch(/dell/)
})

// 跟数组相关的匹配器
// array set
test('toContain', () => {
    const arr = ['dell', 'lee', 'imooc']
    const data = new Set(arr);
    // expect(data).toContain('dell')
    expect(arr).toContain('dell')
})


//异常
const throwNewErrorFunc = () => {
    throw new Error('this is a new error');
}
test('toThrow', () => {
    expect(throwNewErrorFunc).toThrow(/this is a new error/)
})
上一篇 下一篇

猜你喜欢

热点阅读