jest基础—1

语法

jest('',()=>{
     expect().toBe();
})
第一个参数是你的描述相当于console.log,
第二个是箭头函数是你的测试
1.toBe匹配器
2.toEqual和toBe一样,但是toEqual可以测对象
3.测试匹配器的反面
	expect(1+1).not.toBe(0);//1+1不等于0
4.测试undefined,null,false
	toBeNull 只匹配 null
	toBeUndefined 只匹配 undefined
	toBeDefined 与...相反 toBeUndefined
	toBeTruthy匹配if声明视为真的任何内容
	toBeFalsy匹配if语句视为false的任何内容
5.numbers比较
test('two plus two', () => {
	const value = 2 + 2;
	 expect(value).toBeGreaterThan(3);//大于3
	 expect(value).toBeGreaterThanOrEqual(3.5);大于或等于3.5
	 expect(value).toBeLessThan(5);小于5
	 expect(value).toBeLessThanOrEqual(4.5);小于或等于4.5

	// toBe and toEqual are equivalent for numbers
	expect(value).toBe(4);
	 expect(value).toEqual(4);
6.对于浮点数用toBeCloseTo
	expect(0.1 + 0.2).toBeCloseTo(0.3);	
7.字符串,您可以检查对正则表达式的字符串toMatch:
test('there is no I in team', () => {
	expect('team').not.toMatch(/I/);//team中不含I
});
8.Arrays and iterables数组和迭代toContain
const shoppingList = [
			'diapers',
			'kleenex',
			'trash bags',
			'paper towels',
			'beer',
		 ];
	test('the shopping list has beer on it', () => {
		expect(shoppingList).toContain('beer');
		expect(new Set(shoppingList)).toContain('beer');
	});
9.异常 Exceptions 我们使用 toThrow
function compileAndroidCode() {
		throw new ConfigError('you are using the wrong JDK');
	}
	test('compiling android goes as expected', () => {
		expect(compileAndroidCode).toThrow();
		expect(compileAndroidCode).toThrow(ConfigError);
	
		// You can also use the exact error message or a regexp
		expect(compileAndroidCode).toThrow('you are using the wrong JDK');
		expect(compileAndroidCode).toThrow(/JDK/);
	});
10.对于js的异步测试
1.可以使用参数名done
		test('the data is peanut butter', done => {
			function callback(data) {
				expect(data).toBe('peanut butter');
				done();
			}
			
			fetchData(callback);
		});如果done()从未调用过,则测试将失败,这就是您想要发生的事情。
2.用promise
		test('the data is peanut butter', () => {
			return fetchData().then(data => {
			expect(data).toBe('peanut butter');
			});
		});
若是错误就使用.catch
		test('the fetch fails with an error', () => {
			expect.assertions(1);//验证在测试期间调用了一定数量的断言
			return fetchData().catch(e => expect(e).toMatch('error'));
		  });
3.您也可以使用resolve/reject
		test('the data is peanut butter', () => {
			return expect(fetchData()).resolves.toBe('peanut butter');
		});
		test('the fetch fails with an error', () => {
			return expect(fetchData()).rejects.toMatch('error');
		});
	4.当然您更可以用async和await
		test('the data is peanut butter', async () => {
			const data = await fetchData();
			expect(data).toBe('peanut butter');
		});
		
		test('the fetch fails with an error', async () => {
			expect.assertions(1);
			try {
			await fetchData();
			} catch (e) {
			expect(e).toMatch('error');
			}
		});
		简化版:你可以async和or 结合await使用。.resolves.rejects
			test('the data is peanut butter', async () => {
				await expect(fetchData()).resolves.toBe('peanut butter');
			});
		  
			test('the fetch fails with an error', async () => {
				await expect(fetchData()).rejects.toThrow('error');
			});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值