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.
This commit is contained in:
Richard Hansen 2020-10-13 20:44:07 -04:00 committed by John McLear
parent a3f062af96
commit 50e402193b

View file

@ -216,49 +216,30 @@ describe("the test helper", function(){
}); });
describe('the waitForPromise method', 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() { it('takes a timeout and waits long enough', async function() {
this.timeout(2000); this.timeout(2000);
var startTime = Date.now(); const startTime = Date.now();
await helper.waitForPromise(function() { let rejected;
return false; await helper.waitForPromise(() => false, 1500)
}, 1500).catch(function() { .catch(() => { rejected = true; });
var duration = Date.now() - startTime; expect(rejected).to.be(true);
expect(duration).to.be.greaterThan(1490); const duration = Date.now() - startTime;
}); expect(duration).to.be.greaterThan(1490);
}); });
it('takes an interval and checks on every interval', async function() { it('takes an interval and checks on every interval', async function() {
this.timeout(4000); this.timeout(4000);
var checks = 0; let checks = 0;
await helper.waitForPromise(function() { let rejected;
checks++; await helper.waitForPromise(() => { checks++; return false; }, 2000, 100)
return false; .catch(() => { rejected = true; });
}, 2000, 100).catch(function() { expect(rejected).to.be(true);
expect(checks).to.be.greaterThan(15); expect(checks).to.be.greaterThan(15);
expect(checks).to.be.lessThan(21); 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);
});
}); });
}); });