tests: `waitForPromise()` test improvements
* Avoid a false positive if a Promise that is expected to reject doesn't reject. * Use modern JavaScript language features: arrow functions, `const`/`let` instead of `var`. * Remove the tests that test Promise behavior. * Add new test that checks that it returns a Promise.pull/4415/head
parent
a3f062af96
commit
50e402193b
|
@ -216,49 +216,30 @@ describe("the test helper", function(){
|
|||
});
|
||||
|
||||
describe('the waitForPromise method', function() {
|
||||
it('returns a Promise', async function() {
|
||||
expect(helper.waitForPromise(() => true)).to.be.a(Promise);
|
||||
});
|
||||
|
||||
it('takes a timeout and waits long enough', async function() {
|
||||
this.timeout(2000);
|
||||
var startTime = Date.now();
|
||||
await helper.waitForPromise(function() {
|
||||
return false;
|
||||
}, 1500).catch(function() {
|
||||
var duration = Date.now() - startTime;
|
||||
expect(duration).to.be.greaterThan(1490);
|
||||
});
|
||||
const startTime = Date.now();
|
||||
let rejected;
|
||||
await helper.waitForPromise(() => false, 1500)
|
||||
.catch(() => { rejected = true; });
|
||||
expect(rejected).to.be(true);
|
||||
const duration = Date.now() - startTime;
|
||||
expect(duration).to.be.greaterThan(1490);
|
||||
});
|
||||
|
||||
it('takes an interval and checks on every interval', async function() {
|
||||
this.timeout(4000);
|
||||
var checks = 0;
|
||||
await helper.waitForPromise(function() {
|
||||
checks++;
|
||||
return false;
|
||||
}, 2000, 100).catch(function() {
|
||||
expect(checks).to.be.greaterThan(15);
|
||||
expect(checks).to.be.lessThan(21);
|
||||
});
|
||||
});
|
||||
|
||||
describe('returns a Promise', function() {
|
||||
it('calls then after success', async function() {
|
||||
let called = false;
|
||||
await helper.waitForPromise(function() {
|
||||
return true;
|
||||
}).then(function() {
|
||||
called = true;
|
||||
});
|
||||
expect(called).to.be(true);
|
||||
});
|
||||
|
||||
it('calls catch on failure', async function() {
|
||||
let called = false;
|
||||
await helper.waitForPromise(function() {
|
||||
return false;
|
||||
},0).catch(function() {
|
||||
called = true;
|
||||
});
|
||||
expect(called).to.be(true);
|
||||
});
|
||||
let checks = 0;
|
||||
let rejected;
|
||||
await helper.waitForPromise(() => { checks++; return false; }, 2000, 100)
|
||||
.catch(() => { rejected = true; });
|
||||
expect(rejected).to.be(true);
|
||||
expect(checks).to.be.greaterThan(15);
|
||||
expect(checks).to.be.lessThan(21);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue