Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
afeiship committed Dec 12, 2018
1 parent 5ddbefd commit baee772
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
21 changes: 0 additions & 21 deletions docs/2018-12/2018-12-10/009-async.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,3 @@ test('the fetch fails with an error', () => {
});
```



## .resolves / .rejects

您还可以使用 .resolves 匹配器在您期望的声明,Jest 会等待这一 Promise 来解决。如果 Promise 被拒绝,则测试将自动失败。
```js
test('the data is peanut butter', () => {
expect.assertions(1);
return expect(fetchData()).resolves.toBe('peanut butter');
});
```
一定要返回承诺 - 如果你省略 return 语句,您的测试将在 fetchData 完成之前完成。

如果你想要 Promise 被拒绝,使用 .catch 方法。 它参照工程 .resolves 匹配器。 如果 Promise 被拒绝,则测试将自动失败。
```js
test('the fetch fails with an error', () => {
expect.assertions(1);
return expect(fetchData()).rejects.toMatch('error');
});
```

21 changes: 21 additions & 0 deletions docs/2018-12/2018-12-11/001-resolves-rejects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# resolves/rejects:

## .resolves / .rejects

您还可以使用 .resolves 匹配器在您期望的声明,Jest 会等待这一 Promise 来解决。如果 Promise 被拒绝,则测试将自动失败。
```js
test('the data is peanut butter', () => {
expect.assertions(1);
return expect(fetchData()).resolves.toBe('peanut butter');
});
```
一定要返回承诺 - 如果你省略 return 语句,您的测试将在 fetchData 完成之前完成。

如果你想要 Promise 被拒绝,使用 .catch 方法。 它参照工程 .resolves 匹配器。 如果 Promise 被拒绝,则测试将自动失败。
```js
test('the fetch fails with an error', () => {
expect.assertions(1);
return expect(fetchData()).rejects.toMatch('error');
});
```

20 changes: 17 additions & 3 deletions src/2018-12/2018-12/async.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
function fetchData(inCallback){
setTimeout(()=>{
function fetchData(inCallback) {
setTimeout(() => {
inCallback('peanut butter')
},2000)
}, 2000)
}

function fetchWithPromise() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('peanut butter')
}, 1000)
})
}

// fetch async:
Expand All @@ -11,4 +19,10 @@ test('the data is peanut butter', done => {
done();
}
fetchData(callback);
});

test('testa data is peanut butter with promise',()=>{
fetchWithPromise().then(response=>{
expect(response).toBe('peanut butter');
});
});

0 comments on commit baee772

Please sign in to comment.