Files
jasmine/spec/core/matchers/async/toBeRejectedWithSpec.js
Steve Gravrock 434575f49d Use one declaration per statement
The old style of merging all of a function's variable declarations into
a single statement made some sense back in the days of var, but there's
no reason to keep doing it now that we use const and let.
2026-03-11 06:30:46 -07:00

102 lines
3.2 KiB
JavaScript

describe('#toBeRejectedWith', function() {
it('should return true if the promise is rejected with the expected value', function() {
const matchersUtil = new privateUnderTest.MatchersUtil();
const matcher = privateUnderTest.asyncMatchers.toBeRejectedWith(
matchersUtil
);
const actual = Promise.reject({ error: 'PEBCAK' });
return matcher.compare(actual, { error: 'PEBCAK' }).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
});
});
it('should fail if the promise resolves', function() {
const matchersUtil = new privateUnderTest.MatchersUtil();
const matcher = privateUnderTest.asyncMatchers.toBeRejectedWith(
matchersUtil
);
const actual = Promise.resolve();
return matcher.compare(actual, '').then(function(result) {
expect(result).toEqual(jasmine.objectContaining({ pass: false }));
});
});
it('should fail if the promise is rejected with a different value', function() {
const matchersUtil = new privateUnderTest.MatchersUtil({
pp: privateUnderTest.makePrettyPrinter()
});
const matcher = privateUnderTest.asyncMatchers.toBeRejectedWith(
matchersUtil
);
const actual = Promise.reject('A Bad Apple');
return matcher.compare(actual, 'Some Cool Thing').then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: false,
message:
"Expected a promise to be rejected with 'Some Cool Thing' but it was rejected with 'A Bad Apple'."
})
);
});
});
it('should build its error correctly when negated', function() {
const matchersUtil = new privateUnderTest.MatchersUtil({
pp: privateUnderTest.makePrettyPrinter()
});
const matcher = privateUnderTest.asyncMatchers.toBeRejectedWith(
matchersUtil
);
const actual = Promise.reject(true);
return matcher.compare(actual, true).then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: true,
message: 'Expected a promise not to be rejected with true.'
})
);
});
});
it('should support custom equality testers', function() {
const customEqualityTesters = [
function() {
return true;
}
];
const matchersUtil = new privateUnderTest.MatchersUtil({
customTesters: customEqualityTesters
});
const matcher = privateUnderTest.asyncMatchers.toBeRejectedWith(
matchersUtil
);
const actual = Promise.reject('actual');
return matcher.compare(actual, 'expected').then(function(result) {
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
});
});
it('fails if actual is not a promise', function() {
const matchersUtil = new privateUnderTest.MatchersUtil({
pp: privateUnderTest.makePrettyPrinter()
});
const matcher = privateUnderTest.asyncMatchers.toBeRejectedWith(
matchersUtil
);
const actual = 'not a promise';
function f() {
return matcher.compare(actual);
}
expect(f).toThrowError(
'Expected toBeRejectedWith to be called on a promise but was on a string.'
);
});
});