forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
promiseIt.test.js
102 lines (84 loc) 路 2.36 KB
/
promiseIt.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
describe('promise it', () => {
beforeEach(() => {
this.someContextValue = 'value';
});
// passing tests
it('passes a sync test', () => {
expect(1).toBe(1);
});
it('waits for promise to be resolved', () => Promise.resolve());
it('works with done', done => {
done();
});
it('works with async done', done => {
setTimeout(done, 1);
});
it('is bound to context object', () =>
new Promise(resolve => {
if (this.someContextValue !== 'value') {
throw new Error(
'expected this.someContextValue to be set: ' + this.someContextValue,
);
}
resolve();
}));
// failing tests
it('fails if promise is rejected', () =>
Promise.reject(new Error('rejected promise returned')));
it('works with done.fail', done => {
done.fail(new Error('done.fail was called'));
});
it('works with done(error)', done => {
done(new Error('done was called with error'));
});
it('fails if failed expectation with done', done => {
expect(true).toEqual(false);
done();
});
it('fails if failed expectation with done - async', done => {
setTimeout(() => {
expect(true).toEqual(false);
done();
}, 1);
});
it('fails with thrown error with done - sync', done => {
throw new Error('sync fail');
// eslint-disable-next-line no-unreachable
done();
});
it('fails with thrown error with done - async', done => {
setTimeout(() => {
throw new Error('async fail');
// eslint-disable-next-line no-unreachable
done();
}, 1);
});
// I wish it was possible to catch this but I do not see a way.
// Currently both jest and mocha will pass this test.
it.skip('fails with thrown error - async', () => {
setTimeout(() => {
throw new Error('async fail - no done');
}, 1);
});
it('fails a sync test', () => {
expect('sync').toBe('failed');
});
it(
'succeeds if the test finishes in time',
() => new Promise(resolve => setTimeout(resolve, 10)),
250,
);
// failing tests
it(
'fails if a custom timeout is exceeded',
() => new Promise(resolve => setTimeout(resolve, 100)),
10,
);
});