@plugjs/expect5
Version:
Unit Testing for the PlugJS Build System ========================================
74 lines (73 loc) • 2.75 kB
JavaScript
// expectation/async.ts
import { Expectations } from "./expectations.mjs";
import { isMatcher } from "./types.mjs";
var AsyncExpectations = class extends Expectations {
/**
* Create an {@link AsyncExpectations} instance associated with the specified
* value and error remarks.
*/
constructor(value, remarks) {
super(value, remarks);
}
toBeRejected(assertionOrMatcher) {
return Promise.resolve().then(() => {
this.toHaveProperty("then", (assert) => assert.toBeA("function"));
return Promise.allSettled([Promise.resolve(this.value)]);
}).then(([settlement]) => {
if (settlement.status === "rejected") {
if (isMatcher(assertionOrMatcher)) {
assertionOrMatcher.expect(settlement.reason);
} else if (assertionOrMatcher) {
assertionOrMatcher(new Expectations(settlement.reason, this.remarks));
}
return this;
}
this._fail("to be rejected");
});
}
/* ------------------------------------------------------------------------ */
/**
* Expects the value to be a {@link PromiseLike} _rejected_ with an
* {@link Error} {@link Expectations.toStrictlyEqual _strictly equal_}
* to the one specified.
*
* Negation: {@link Expectations.toBeResolved `toBeResolved(...)`}
*/
toBeRejectedWith(expected) {
return this.toBeRejected((assert) => assert.toStrictlyEqual(expected));
}
toBeRejectedWithError(constructorOrMessage, maybeMessage) {
const [constructor, message] = typeof constructorOrMessage === "function" ? [constructorOrMessage, maybeMessage] : [Error, constructorOrMessage];
return this.toBeRejected((assert) => assert.toBeError(constructor, message));
}
toBeResolved(assertion) {
return Promise.resolve().then(() => {
this.toHaveProperty("then", (assert) => assert.toBeA("function"));
return Promise.allSettled([Promise.resolve(this.value)]);
}).then(([settlement]) => {
if (settlement.status === "fulfilled") {
if (isMatcher(assertion)) {
assertion.expect(settlement.value);
} else if (assertion) {
assertion(new Expectations(settlement.value, this.remarks));
}
return this;
}
this._fail("to be resolved");
});
}
/* ------------------------------------------------------------------------ */
/**
* Expects the value to be a {@link PromiseLike} _resolved_ with a value
* {@link Expectations.toEqual _deeply equal_} to the one specified.
*
* Negation: {@link Expectations.toBeRejected `toBeRejected(...)`}
*/
toBeResolvedWith(expected) {
return this.toBeResolved((assert) => assert.toEqual(expected));
}
};
export {
AsyncExpectations
};
//# sourceMappingURL=async.mjs.map