Compare commits

..

3 Commits

Author SHA1 Message Date
Steve Gravrock 6e52b971cd Merge branch 'fix-issue-1781' of github.com:Tyoneb/jasmine
* Adds toBeRejectedWithMatching async matcher
* Merges #2097 from @Tyoneb
* Fixes #1781
2026-02-28 10:33:33 -08:00
Benoit CHOMEL 0a5acd99cb feat: Add new toBeRejectedWithMatching async matcher
Addresses #1781
2026-02-26 00:01:45 +01:00
Steve Gravrock f7132d98cd Copy 7.0.0-pre.0 release notes from branch 2026-02-22 14:24:50 -08:00
5 changed files with 269 additions and 2 deletions
+67 -1
View File
@@ -5122,6 +5122,71 @@ getJasmineRequireObj().toBeRejectedWithError = function(j$) {
}
};
getJasmineRequireObj().toBeRejectedWithMatching = function(j$) {
'use strict';
const usageError = j$.private.formatErrorMsg(
'<toBeRejectedWithMatching>',
'expect(function() {<expectation>}).toBeRejectedWithMatching(<Predicate>)'
);
/**
* Expect a promise to be rejected with a value matching a predicate.
* @function
* @async
* @name async-matchers#toBeRejectedWithMatching
* @since 6.2.0
* @param {Function} predicate - A function that takes the rejected promise value as its parameter and returns true if it matches.
* @example
* await expectAsync(aPromise).toBeRejectedWithMatching((error) => error.message === 'Some error');
* @example
* return expectAsync(aPromise).toBeRejectedWithMatching((error) => error.message === 'Some error');
*/
return function toBeRejectedWithMatching() {
return {
compare: function(actualPromise, predicate) {
if (!j$.private.isPromiseLike(actualPromise)) {
throw new Error(
`Expected toBeRejectedWithMatching to be called on a promise but was on a ${typeof actualPromise}.`
);
}
if (typeof predicate !== 'function') {
throw new Error(usageError('Predicate is not a Function'));
}
function prefix(passed) {
return (
'Expected a promise ' +
(passed ? 'not ' : '') +
'to be rejected matching a predicate'
);
}
return actualPromise.then(
function() {
return {
pass: false,
message: prefix(false) + ', but it was resolved.'
};
},
function(actualValue) {
const result = predicate(actualValue);
return {
pass: Boolean(result),
message:
prefix(result) +
', but the predicate returned "' +
result +
'".'
};
}
);
}
};
};
};
getJasmineRequireObj().toBeResolved = function(j$) {
'use strict';
@@ -6189,7 +6254,8 @@ getJasmineRequireObj().requireAsyncMatchers = function(jRequire, j$) {
'toBeRejected',
'toBeResolvedTo',
'toBeRejectedWith',
'toBeRejectedWithError'
'toBeRejectedWithError',
'toBeRejectedWithMatching'
],
matchers = {};
+58
View File
@@ -0,0 +1,58 @@
# Jasmine Core 7.0.0-pre.0 Release Notes
This is a pre-release, intended to offer a preview of breaking changes and to
solicit feedback.
Before installing this release, update to 6.0.1 or later and fix all
deprecation warnings.
## Breaking changes
* `HtmlReporter`, `HtmlSpecFilter`, and `jsApiReporter`, which were deprecated
in 6.0, have been removed.
* Most private APIs are no longer exposed.
* Monkey patching is blocked. This does not affect globals (`describe`,
`expect`, etc.) or properties that are explicitly documented as writeable.
Those can still be overwritten.
* Redesigned boot process:
* `jasmineRequire`, `boot`, and `noGlobals` have been removed.
* The core instance is automatically created.
* In browsers, globals are automatically created.
* In Node, globals are not automatically created. Call the exported
`installGlobals` function to create them. If you're using the `jasmine`
package, this is done for you by default.
* In Node, the same instance is returned every time jasmine-core is imported.
If you need to do multiple consecutive independent test runs, call the
exported `reset` function to reset all state and configuration between runs.
If you maintain your own browser boot files, you may be able to update by just
removing `boot1.js`. See the `boot.js` file in this package for a working
example. If you boot jasmine-core in Node, remove the call to `boot` or
`noGlobals` and add a call to `installGlobals` if desired:
```javascript
const jasmineCore = require('jasmine-core');
jasmineCore.installGlobals();
```
## Supported environments
This version has been tested in the following environments.
| Environment | Supported versions |
|-------------------|--------------------------------|
| Node | 20, 22, 24 |
| Safari | 26.3** |
| Chrome | 144* |
| Firefox | 102**, 115**, 128**, 140, 147* |
| Edge | 144* |
\* Evergreen browser. Each version of Jasmine is tested against the latest
version available at release time.<br>
\** Supported on a best-effort basis. Support for these versions may be dropped
if it becomes impractical, and bugs affecting only these versions may not be
treated as release blockers.
------
_Release Notes generated with _[Anchorman](http://github.com/infews/anchorman)_
@@ -0,0 +1,78 @@
describe('#toBeRejectedWithMatching', function() {
it('passes if the promise is rejected with something matching the predicate', function() {
const matcher = privateUnderTest.asyncMatchers.toBeRejectedWithMatching(),
actual = Promise.reject(new Error('nope')),
predicate = value => value.message === 'nope';
return matcher.compare(actual, predicate).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
});
});
it('fails if the promise resolves', function() {
const matcher = privateUnderTest.asyncMatchers.toBeRejectedWithMatching(),
actual = Promise.resolve(),
predicate = () => true;
return matcher.compare(actual, predicate).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({ pass: false }));
});
});
it('fails if the promise is rejected with something not matching the predicate', function() {
const matcher = privateUnderTest.asyncMatchers.toBeRejectedWithMatching(),
actual = Promise.reject('A Bad Apple'),
predicate = value => value === 'A Good Orange';
return matcher.compare(actual, predicate).then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: false,
message:
'Expected a promise to be rejected matching a predicate, but the predicate returned "false".'
})
);
});
});
it('should build its error correctly when negated', function() {
const matcher = privateUnderTest.asyncMatchers.toBeRejectedWithMatching(),
actual = Promise.reject(true),
predicate = () => true;
return matcher.compare(actual, predicate).then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: true,
message:
'Expected a promise not to be rejected matching a predicate, but the predicate returned "true".'
})
);
});
});
it('fails if actual is not a promise', function() {
const matcher = privateUnderTest.asyncMatchers.toBeRejectedWithMatching(),
actual = 'not a promise';
function f() {
return matcher.compare(actual);
}
expect(f).toThrowError(
'Expected toBeRejectedWithMatching to be called on a promise but was on a string.'
);
});
it('fails if predicate is not a function', function() {
const matcher = privateUnderTest.asyncMatchers.toBeRejectedWithMatching(),
actual = Promise.resolve(),
predicate = 'not a function';
function f() {
return matcher.compare(actual, predicate);
}
expect(f).toThrowError(/Predicate is not a Function/);
});
});
@@ -0,0 +1,64 @@
getJasmineRequireObj().toBeRejectedWithMatching = function(j$) {
'use strict';
const usageError = j$.private.formatErrorMsg(
'<toBeRejectedWithMatching>',
'expect(function() {<expectation>}).toBeRejectedWithMatching(<Predicate>)'
);
/**
* Expect a promise to be rejected with a value matching a predicate.
* @function
* @async
* @name async-matchers#toBeRejectedWithMatching
* @since 6.2.0
* @param {Function} predicate - A function that takes the rejected promise value as its parameter and returns true if it matches.
* @example
* await expectAsync(aPromise).toBeRejectedWithMatching((error) => error.message === 'Some error');
* @example
* return expectAsync(aPromise).toBeRejectedWithMatching((error) => error.message === 'Some error');
*/
return function toBeRejectedWithMatching() {
return {
compare: function(actualPromise, predicate) {
if (!j$.private.isPromiseLike(actualPromise)) {
throw new Error(
`Expected toBeRejectedWithMatching to be called on a promise but was on a ${typeof actualPromise}.`
);
}
if (typeof predicate !== 'function') {
throw new Error(usageError('Predicate is not a Function'));
}
function prefix(passed) {
return (
'Expected a promise ' +
(passed ? 'not ' : '') +
'to be rejected matching a predicate'
);
}
return actualPromise.then(
function() {
return {
pass: false,
message: prefix(false) + ', but it was resolved.'
};
},
function(actualValue) {
const result = predicate(actualValue);
return {
pass: Boolean(result),
message:
prefix(result) +
', but the predicate returned "' +
result +
'".'
};
}
);
}
};
};
};
+2 -1
View File
@@ -7,7 +7,8 @@ getJasmineRequireObj().requireAsyncMatchers = function(jRequire, j$) {
'toBeRejected',
'toBeResolvedTo',
'toBeRejectedWith',
'toBeRejectedWithError'
'toBeRejectedWithError',
'toBeRejectedWithMatching'
],
matchers = {};