Added a basic set of async matchers

- Fixes #1447
- Fixes #1547
This commit is contained in:
Steve Gravrock
2018-06-04 21:01:22 -07:00
parent 8f7327cb4d
commit 9472df0db4
16 changed files with 876 additions and 34 deletions

View File

@@ -0,0 +1,288 @@
describe('AsyncExpectation', function() {
describe('Factory', function() {
it('throws an Error if promises are not available', function() {
var thenable = {then: function() {}},
options = {global: {}, actual: thenable}
function f() { jasmineUnderTest.AsyncExpectation.factory(options); }
expect(f).toThrowError('expectAsync is unavailable because the environment does not support promises.');
});
it('throws an Error if the argument is not a promise', function() {
jasmine.getEnv().requirePromises();
function f() {
jasmineUnderTest.AsyncExpectation.factory({actual: 'not a promise'});
}
expect(f).toThrowError('Expected expectAsync to be called with a promise.');
});
});
describe('#toBeResolved', function() {
it('passes if the actual is resolved', function() {
jasmine.getEnv().requirePromises();
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
actual = Promise.resolve(),
expectation = new jasmineUnderTest.AsyncExpectation({
util: jasmineUnderTest.matchersUtil,
actual: actual,
addExpectationResult: addExpectationResult
});
return expectation.toBeResolved().then(function() {
expect(addExpectationResult).toHaveBeenCalledWith(true, {
matcherName: 'toBeResolved',
passed: true,
message: '',
error: undefined,
errorForStack: jasmine.any(Error),
actual: actual
});
});
});
it('fails if the actual is rejected', function() {
jasmine.getEnv().requirePromises();
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
actual = Promise.reject('AsyncExpectationSpec rejection'),
expectation = new jasmineUnderTest.AsyncExpectation({
util: jasmineUnderTest.matchersUtil,
actual: actual,
addExpectationResult: addExpectationResult
});
return expectation.toBeResolved().then(function() {
expect(addExpectationResult).toHaveBeenCalledWith(false, {
matcherName: 'toBeResolved',
passed: false,
message: 'Expected a promise to be resolved.',
error: undefined,
errorForStack: jasmine.any(Error),
actual: actual
});
});
});
});
describe('#toBeRejected', function() {
it('passes if the actual is rejected', function() {
jasmine.getEnv().requirePromises();
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
actual = Promise.reject('AsyncExpectationSpec rejection'),
expectation = new jasmineUnderTest.AsyncExpectation({
util: jasmineUnderTest.matchersUtil,
actual: actual,
addExpectationResult: addExpectationResult
});
return expectation.toBeRejected().then(function() {
expect(addExpectationResult).toHaveBeenCalledWith(true, {
matcherName: 'toBeRejected',
passed: true,
message: '',
error: undefined,
errorForStack: jasmine.any(Error),
actual: actual
});
});
});
it('fails if the actual is resolved', function() {
jasmine.getEnv().requirePromises();
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
actual = Promise.resolve(),
expectation = new jasmineUnderTest.AsyncExpectation({
util: jasmineUnderTest.matchersUtil,
actual: actual,
addExpectationResult: addExpectationResult
});
return expectation.toBeRejected().then(function() {
expect(addExpectationResult).toHaveBeenCalledWith(false, {
matcherName: 'toBeRejected',
passed: false,
message: 'Expected a promise to be rejected.',
error: undefined,
errorForStack: jasmine.any(Error),
actual: actual
});
});
});
});
describe('#toBeResolvedTo', function() {
it('passes if the promise is resolved to the expected value', function() {
jasmine.getEnv().requirePromises();
var actual = Promise.resolve({foo: 42});
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
expectation = new jasmineUnderTest.AsyncExpectation({
util: jasmineUnderTest.matchersUtil,
actual: actual,
addExpectationResult: addExpectationResult
});
return expectation.toBeResolvedTo({foo: 42}).then(function() {
expect(addExpectationResult).toHaveBeenCalledWith(true, {
matcherName: 'toBeResolvedTo',
passed: true,
message: '',
error: undefined,
errorForStack: jasmine.any(Error),
actual: actual
});
});
});
it('fails if the promise is rejected', function() {
jasmine.getEnv().requirePromises();
var actual = Promise.reject('AsyncExpectationSpec error');
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
expectation = new jasmineUnderTest.AsyncExpectation({
util: jasmineUnderTest.matchersUtil,
actual: actual,
addExpectationResult: addExpectationResult
});
return expectation.toBeResolvedTo('').then(function() {
expect(addExpectationResult).toHaveBeenCalledWith(false, {
matcherName: 'toBeResolvedTo',
passed: false,
message: "Expected a promise to be resolved to '' but it was rejected.",
error: undefined,
errorForStack: jasmine.any(Error),
actual: actual
});
});
});
it('fails if the promise is resolved to a different value', function() {
jasmine.getEnv().requirePromises();
var actual = Promise.resolve({foo: 17});
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
expectation = new jasmineUnderTest.AsyncExpectation({
util: jasmineUnderTest.matchersUtil,
actual: actual,
addExpectationResult: addExpectationResult
});
return expectation.toBeResolvedTo({foo: 42}).then(function() {
expect(addExpectationResult).toHaveBeenCalledWith(false, {
matcherName: 'toBeResolvedTo',
passed: false,
message: 'Expected a promise to be resolved to Object({ foo: 42 }) but it was resolved to Object({ foo: 17 }).',
error: undefined,
errorForStack: jasmine.any(Error),
actual: actual
});
});
});
it('builds its message correctly when negated', function() {
jasmine.getEnv().requirePromises();
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
expectation = jasmineUnderTest.AsyncExpectation.factory({
util: jasmineUnderTest.matchersUtil,
actual: Promise.resolve(true),
addExpectationResult: addExpectationResult
});
return expectation.not.toBeResolvedTo(true).then(function() {
expect(addExpectationResult).toHaveBeenCalledWith(false,
jasmine.objectContaining({
passed: false,
message: 'Expected a promise not to be resolved to true.'
})
);
});
});
it('supports custom equality testers', function() {
jasmine.getEnv().requirePromises();
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
expectation = new jasmineUnderTest.AsyncExpectation({
util: jasmineUnderTest.matchersUtil,
customEqualityTesters: [function() { return true; }],
actual: Promise.resolve('actual'),
addExpectationResult: addExpectationResult
});
return expectation.toBeResolvedTo('expected').then(function() {
expect(addExpectationResult).toHaveBeenCalledWith(true,
jasmine.objectContaining({passed: true}));
});
});
});
describe('#not', function() {
it('converts a pass to a fail', function() {
jasmine.getEnv().requirePromises();
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
actual = Promise.resolve(),
expectation = jasmineUnderTest.AsyncExpectation.factory({
util: jasmineUnderTest.matchersUtil,
actual: actual,
addExpectationResult: addExpectationResult
});
return expectation.not.toBeResolved().then(function() {
expect(addExpectationResult).toHaveBeenCalledWith(false,
jasmine.objectContaining({
passed: false,
message: 'Expected a promise not to be resolved.'
})
);
});
});
it('converts a fail to a pass', function() {
jasmine.getEnv().requirePromises();
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
actual = Promise.reject(),
expectation = jasmineUnderTest.AsyncExpectation.factory({
util: jasmineUnderTest.matchersUtil,
actual: actual,
addExpectationResult: addExpectationResult
});
return expectation.not.toBeResolved().then(function() {
expect(addExpectationResult).toHaveBeenCalledWith(true,
jasmine.objectContaining({
passed: true,
message: ''
})
);
});
});
});
it('propagates rejections from the comparison function', function() {
jasmine.getEnv().requirePromises();
var error = new Error('AsyncExpectationSpec failure');
spyOn(jasmineUnderTest.AsyncExpectation.prototype, 'toBeResolved')
.and.returnValue(Promise.reject(error));
var addExpectationResult = jasmine.createSpy('addExpectationResult'),
actual = dummyPromise(),
expectation = new jasmineUnderTest.AsyncExpectation({
actual: actual,
addExpectationResult: addExpectationResult
});
return expectation.toBeResolved()
.then(
function() { fail('Expected a rejection'); },
function(e) { expect(e).toBe(error); }
);
});
function dummyPromise() {
return new Promise(function(resolve, reject) {
});
}
});

View File

@@ -44,6 +44,21 @@ describe("buildExpectationResult", function() {
expect(result.stack).toEqual('foo');
});
it("delegates stack formatting to the provided formatter if there was a provided errorForStack", function() {
var fakeError = {stack: 'foo'},
stackFormatter = jasmine.createSpy("stack formatter").and.returnValue(fakeError.stack);
var result = jasmineUnderTest.buildExpectationResult(
{
passed: false,
errorForStack: fakeError,
stackFormatter: stackFormatter
});
expect(stackFormatter).toHaveBeenCalledWith(fakeError);
expect(result.stack).toEqual('foo');
});
it("matcherName returns passed matcherName", function() {
var result = jasmineUnderTest.buildExpectationResult({matcherName: 'some-value'});
expect(result.matcherName).toBe('some-value');

View File

@@ -2415,4 +2415,106 @@ describe("Env integration", function() {
env.execute();
});
it('supports async matchers', function(done) {
jasmine.getEnv().requirePromises();
var env = new jasmineUnderTest.Env(),
specDone = jasmine.createSpy('specDone'),
suiteDone = jasmine.createSpy('suiteDone');
env.addReporter({
specDone: specDone,
suiteDone: suiteDone,
jasmineDone: function(result) {
expect(result.failedExpectations).toEqual([jasmine.objectContaining({
message: 'Expected a promise to be rejected.'
})]);
expect(specDone).toHaveBeenCalledWith(jasmine.objectContaining({
description: 'has an async failure',
failedExpectations: [jasmine.objectContaining({
message: 'Expected a promise to be rejected.'
})]
}));
expect(suiteDone).toHaveBeenCalledWith(jasmine.objectContaining({
description: 'a suite',
failedExpectations: [jasmine.objectContaining({
message: 'Expected a promise to be rejected.'
})]
}));
done();
}
});
function fail(innerDone) {
var resolve;
var p = new Promise(function(res, rej) { resolve = res });
env.expectAsync(p).toBeRejected().then(innerDone);
resolve();
}
env.afterAll(fail);
env.describe('a suite', function() {
env.afterAll(fail);
env.it('has an async failure', fail);
});
env.execute();
});
it('provides custom equality testers to async matchers', function(done) {
jasmine.getEnv().requirePromises();
var env = new jasmineUnderTest.Env(),
specDone = jasmine.createSpy('specDone');
env.addReporter({
specDone: specDone,
jasmineDone: function() {
expect(specDone).toHaveBeenCalledWith(jasmine.objectContaining({
description: 'has an async failure',
failedExpectations: []
}));
done();
}
});
env.it('has an async failure', function() {
env.addCustomEqualityTester(function() { return true; });
var p = Promise.resolve('something');
return env.expectAsync(p).toBeResolvedTo('something else');
});
env.execute();
});
it('includes useful stack frames in async matcher failures', function(done) {
jasmine.getEnv().requirePromises();
var env = new jasmineUnderTest.Env(),
specDone = jasmine.createSpy('specDone');
env.addReporter({
specDone: specDone,
jasmineDone: function() {
expect(specDone).toHaveBeenCalledWith(jasmine.objectContaining({
failedExpectations: [jasmine.objectContaining({
stack: jasmine.stringMatching('EnvSpec.js')
})]
}));
done();
}
});
env.it('has an async failure', function() {
env.addCustomEqualityTester(function() { return true; });
var p = Promise.resolve();
return env.expectAsync(p).toBeRejected();
});
env.execute();
});
});

8
spec/helpers/promises.js Normal file
View File

@@ -0,0 +1,8 @@
(function(env) {
env.requirePromises = function() {
if (typeof Promise !== 'function') {
env.pending("Environment does not support promises");
}
};
})(jasmine.getEnv());

View File

@@ -11,6 +11,7 @@
"helpers/checkForSymbol.js",
"helpers/checkForTypedArrays.js",
"helpers/integrationMatchers.js",
"helpers/promises.js",
"helpers/nodeDefineJasmineUnderTest.js"
],
"random": true

View File

@@ -24,6 +24,7 @@ helpers:
- 'helpers/checkForSymbol.js'
- 'helpers/checkForTypedArrays.js'
- 'helpers/integrationMatchers.js'
- 'helpers/promises.js'
- 'helpers/defineJasmineUnderTest.js'
spec_files:
- '**/*[Ss]pec.js'