Converted some integration specs to async/await

This commit is contained in:
Steve Gravrock
2022-05-14 12:05:53 -07:00
parent 774c83a36e
commit c24b2f5a73
5 changed files with 450 additions and 464 deletions

View File

@@ -1,55 +1,57 @@
describe('Asymmetric equality testers (Integration)', function() { describe('Asymmetric equality testers (Integration)', function() {
function verifyPasses(expectations) { function verifyPasses(expectations) {
it('passes', function(done) { it('passes', async function() {
const env = new jasmineUnderTest.Env(); const env = new jasmineUnderTest.Env();
env.it('a spec', function() { env.it('a spec', function() {
expectations(env); expectations(env);
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('passed'); env.addReporter(reporter);
expect(result.passedExpectations.length) await env.execute();
.withContext('Number of passed expectations')
.toEqual(1);
expect(result.failedExpectations.length)
.withContext('Number of failed expectations')
.toEqual(0);
expect(
result.failedExpectations[0] && result.failedExpectations[0].message
)
.withContext('Failure message')
.toBeUndefined();
};
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('passed');
expect(result.passedExpectations.length)
.withContext('Number of passed expectations')
.toEqual(1);
expect(result.failedExpectations.length)
.withContext('Number of failed expectations')
.toEqual(0);
expect(
result.failedExpectations[0] && result.failedExpectations[0].message
)
.withContext('Failure message')
.toBeUndefined();
}); });
} }
function verifyFails(expectations) { function verifyFails(expectations) {
it('fails', function(done) { it('fails', async function() {
const env = new jasmineUnderTest.Env(); const env = new jasmineUnderTest.Env();
env.it('a spec', function() { env.it('a spec', function() {
expectations(env); expectations(env);
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('failed'); env.addReporter(reporter);
expect(result.failedExpectations.length) await env.execute();
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message)
.withContext(
'Failed with a thrown error rather than a matcher failure'
)
.not.toMatch(/^Error: /);
expect(result.failedExpectations[0].matcherName)
.withContext('Matcher name')
.not.toEqual('');
};
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('failed');
expect(result.failedExpectations.length)
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message)
.withContext('Failed with a thrown error rather than a matcher failure')
.not.toMatch(/^Error: /);
expect(result.failedExpectations[0].matcherName)
.withContext('Matcher name')
.not.toEqual('');
}); });
} }

View File

@@ -10,7 +10,7 @@ describe('Custom Matchers (Integration)', function() {
env.cleanup_(); env.cleanup_();
}); });
it('allows adding more matchers local to a spec', function(done) { it('allows adding more matchers local to a spec', async function() {
env.it('spec defining a custom matcher', function() { env.it('spec defining a custom matcher', function() {
env.addMatchers({ env.addMatchers({
matcherForSpec: function() { matcherForSpec: function() {
@@ -37,20 +37,18 @@ describe('Custom Matchers (Integration)', function() {
}); });
const specDoneSpy = jasmine.createSpy('specDoneSpy'); const specDoneSpy = jasmine.createSpy('specDoneSpy');
const expectations = function() {
const firstSpecResult = specDoneSpy.calls.first().args[0];
expect(firstSpecResult.status).toEqual('failed');
expect(firstSpecResult.failedExpectations[0].message).toEqual(
'matcherForSpec: actual: zzz; expected: yyy'
);
done();
};
env.addReporter({ specDone: specDoneSpy }); env.addReporter({ specDone: specDoneSpy });
env.execute(null, expectations); await env.execute();
const firstSpecResult = specDoneSpy.calls.first().args[0];
expect(firstSpecResult.status).toEqual('failed');
expect(firstSpecResult.failedExpectations[0].message).toEqual(
'matcherForSpec: actual: zzz; expected: yyy'
);
}); });
it('passes the spec if the custom matcher passes', function(done) { it('passes the spec if the custom matcher passes', async function() {
env.it('spec using custom matcher', function() { env.it('spec using custom matcher', function() {
env.addMatchers({ env.addMatchers({
toBeReal: function() { toBeReal: function() {
@@ -65,15 +63,16 @@ describe('Custom Matchers (Integration)', function() {
env.expect(true).toBeReal(); env.expect(true).toBeReal();
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('passed'); env.addReporter(reporter);
}; await env.execute();
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('passed');
}); });
it('passes the spec if the custom equality matcher passes for types nested inside asymmetric equality testers', function(done) { it('passes the spec if the custom equality matcher passes for types nested inside asymmetric equality testers', async function() {
env.it('spec using custom equality matcher', function() { env.it('spec using custom equality matcher', function() {
const customEqualityFn = function(a, b) { const customEqualityFn = function(a, b) {
// All "foo*" strings match each other. // All "foo*" strings match each other.
@@ -99,15 +98,16 @@ describe('Custom Matchers (Integration)', function() {
.toEqual(jasmineUnderTest.arrayWithExactContents(['fooBar'])); .toEqual(jasmineUnderTest.arrayWithExactContents(['fooBar']));
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('passed'); env.addReporter(reporter);
}; await env.execute();
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('passed');
}); });
it('displays an appropriate failure message if a custom equality matcher fails', function(done) { it('displays an appropriate failure message if a custom equality matcher fails', async function() {
env.it('spec using custom equality matcher', function() { env.it('spec using custom equality matcher', function() {
const customEqualityFn = function(a, b) { const customEqualityFn = function(a, b) {
// "foo" is not equal to anything // "foo" is not equal to anything
@@ -120,18 +120,19 @@ describe('Custom Matchers (Integration)', function() {
env.expect({ foo: 'foo' }).toEqual({ foo: 'foo' }); env.expect({ foo: 'foo' }).toEqual({ foo: 'foo' });
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('failed'); env.addReporter(reporter);
expect(result.failedExpectations[0].message).toEqual( await env.execute();
"Expected $.foo = 'foo' to equal 'foo'."
);
};
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('failed');
expect(result.failedExpectations[0].message).toEqual(
"Expected $.foo = 'foo' to equal 'foo'."
);
}); });
it('uses the negative compare function for a negative comparison, if provided', function(done) { it('uses the negative compare function for a negative comparison, if provided', async function() {
env.it('spec with custom negative comparison matcher', function() { env.it('spec with custom negative comparison matcher', function() {
env.addMatchers({ env.addMatchers({
toBeReal: function() { toBeReal: function() {
@@ -149,15 +150,16 @@ describe('Custom Matchers (Integration)', function() {
env.expect(true).not.toBeReal(); env.expect(true).not.toBeReal();
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('passed'); env.addReporter(reporter);
}; await env.execute();
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('passed');
}); });
it('generates messages with the same rules as built in matchers absent a custom message', function(done) { it('generates messages with the same rules as built in matchers absent a custom message', async function() {
env.it('spec with an expectation', function() { env.it('spec with an expectation', function() {
env.addMatchers({ env.addMatchers({
toBeReal: function() { toBeReal: function() {
@@ -172,17 +174,18 @@ describe('Custom Matchers (Integration)', function() {
env.expect('a').toBeReal(); env.expect('a').toBeReal();
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.failedExpectations[0].message).toEqual( env.addReporter(reporter);
"Expected 'a' to be real." await env.execute();
);
};
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.failedExpectations[0].message).toEqual(
"Expected 'a' to be real."
);
}); });
it('passes the expected and actual arguments to the comparison function', function(done) { it('passes the expected and actual arguments to the comparison function', async function() {
const argumentSpy = jasmine const argumentSpy = jasmine
.createSpy('argument spy') .createSpy('argument spy')
.and.returnValue({ pass: true }); .and.returnValue({ pass: true });
@@ -199,17 +202,13 @@ describe('Custom Matchers (Integration)', function() {
env.expect(true).toBeReal('arg1', 'arg2'); env.expect(true).toBeReal('arg1', 'arg2');
}); });
const specExpectations = function() { await env.execute();
expect(argumentSpy).toHaveBeenCalledWith(true); expect(argumentSpy).toHaveBeenCalledWith(true);
expect(argumentSpy).toHaveBeenCalledWith(true, 'arg'); expect(argumentSpy).toHaveBeenCalledWith(true, 'arg');
expect(argumentSpy).toHaveBeenCalledWith(true, 'arg1', 'arg2'); expect(argumentSpy).toHaveBeenCalledWith(true, 'arg1', 'arg2');
};
env.addReporter({ specDone: specExpectations });
env.execute(null, done);
}); });
it('passes the jasmine utility to the matcher factory', function(done) { it('passes the jasmine utility to the matcher factory', async function() {
const matcherFactory = function() { const matcherFactory = function() {
return { return {
compare: function() { compare: function() {
@@ -229,17 +228,13 @@ describe('Custom Matchers (Integration)', function() {
env.expect(true).toBeReal(); env.expect(true).toBeReal();
}); });
const specExpectations = function() { await env.execute();
expect(matcherFactorySpy).toHaveBeenCalledWith( expect(matcherFactorySpy).toHaveBeenCalledWith(
jasmine.any(jasmineUnderTest.MatchersUtil) jasmine.any(jasmineUnderTest.MatchersUtil)
); );
};
env.addReporter({ specDone: specExpectations });
env.execute(null, done);
}); });
it('provides custom equality testers to the matcher factory via matchersUtil', function(done) { it('provides custom equality testers to the matcher factory via matchersUtil', async function() {
const matcherFactory = function(matchersUtil) { const matcherFactory = function(matchersUtil) {
return { return {
compare: function(actual, expected) { compare: function(actual, expected) {
@@ -262,12 +257,13 @@ describe('Custom Matchers (Integration)', function() {
env.expect([1, 2]).toBeArrayWithFirstElement('1'); env.expect([1, 2]).toBeArrayWithFirstElement('1');
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(customEqualityFn).toHaveBeenCalledWith(1, '1'); env.addReporter(reporter);
expect(result.failedExpectations).toEqual([]); await env.execute();
};
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(customEqualityFn).toHaveBeenCalledWith(1, '1');
expect(result.failedExpectations).toEqual([]);
}); });
}); });

View File

@@ -6,7 +6,7 @@ describe('Custom object formatters', function() {
env.configure({ random: false }); env.configure({ random: false });
}); });
it('scopes custom object formatters to a spec', function(done) { it('scopes custom object formatters to a spec', async function() {
env.it('a spec with custom pretty-printer', function() { env.it('a spec with custom pretty-printer', function() {
env.addCustomObjectFormatter(function(obj) { env.addCustomObjectFormatter(function(obj) {
return 'custom(' + obj + ')'; return 'custom(' + obj + ')';
@@ -22,21 +22,19 @@ describe('Custom object formatters', function() {
const specDone = function(result) { const specDone = function(result) {
specResults.push(result); specResults.push(result);
}; };
const expectations = function() {
expect(specResults[0].failedExpectations[0].message).toEqual(
'Expected custom(42) to be undefined.'
);
expect(specResults[1].failedExpectations[0].message).toEqual(
'Expected 42 to be undefined.'
);
done();
};
env.addReporter({ specDone: specDone }); env.addReporter({ specDone: specDone });
env.execute(null, expectations); await env.execute();
expect(specResults[0].failedExpectations[0].message).toEqual(
'Expected custom(42) to be undefined.'
);
expect(specResults[1].failedExpectations[0].message).toEqual(
'Expected 42 to be undefined.'
);
}); });
it('scopes custom object formatters to a suite', function(done) { it('scopes custom object formatters to a suite', async function() {
env.it('a spec without custom pretty-printer', function() { env.it('a spec without custom pretty-printer', function() {
env.expect(42).toBeUndefined(); env.expect(42).toBeUndefined();
}); });
@@ -57,18 +55,16 @@ describe('Custom object formatters', function() {
const specDone = function(result) { const specDone = function(result) {
specResults.push(result); specResults.push(result);
}; };
const expectations = function() {
expect(specResults[0].failedExpectations[0].message).toEqual(
'Expected 42 to be undefined.'
);
expect(specResults[1].failedExpectations[0].message).toEqual(
'Expected custom(42) to be undefined.'
);
done();
};
env.addReporter({ specDone: specDone }); env.addReporter({ specDone: specDone });
env.execute(null, expectations); await env.execute();
expect(specResults[0].failedExpectations[0].message).toEqual(
'Expected 42 to be undefined.'
);
expect(specResults[1].failedExpectations[0].message).toEqual(
'Expected custom(42) to be undefined.'
);
}); });
it('throws an exception if you try to add a custom object formatter outside a runable', function() { it('throws an exception if you try to add a custom object formatter outside a runable', function() {

View File

@@ -10,7 +10,7 @@ describe('Deprecation (integration)', function() {
env.cleanup_(); env.cleanup_();
}); });
it('reports a deprecation on the top suite', function(done) { it('reports a deprecation on the top suite', async function() {
const reporter = jasmine.createSpyObj('reporter', ['jasmineDone']); const reporter = jasmine.createSpyObj('reporter', ['jasmineDone']);
env.addReporter(reporter); env.addReporter(reporter);
spyOn(console, 'error'); spyOn(console, 'error');
@@ -20,24 +20,23 @@ describe('Deprecation (integration)', function() {
}); });
env.it('a spec', function() {}); env.it('a spec', function() {});
env.execute(null, function() { await env.execute();
expect(reporter.jasmineDone).toHaveBeenCalledWith(
jasmine.objectContaining({ expect(reporter.jasmineDone).toHaveBeenCalledWith(
deprecationWarnings: [ jasmine.objectContaining({
jasmine.objectContaining({ deprecationWarnings: [
message: jasmine.stringMatching(/^the message/) jasmine.objectContaining({
}) message: jasmine.stringMatching(/^the message/)
] })
}) ]
); })
expect(console.error).toHaveBeenCalledWith( );
jasmine.stringMatching(/^DEPRECATION: the message/) expect(console.error).toHaveBeenCalledWith(
); jasmine.stringMatching(/^DEPRECATION: the message/)
done(); );
});
}); });
it('reports a deprecation on a descendent suite', function(done) { it('reports a deprecation on a descendent suite', async function() {
const reporter = jasmine.createSpyObj('reporter', ['suiteDone']); const reporter = jasmine.createSpyObj('reporter', ['suiteDone']);
env.addReporter(reporter); env.addReporter(reporter);
spyOn(console, 'error'); spyOn(console, 'error');
@@ -49,26 +48,23 @@ describe('Deprecation (integration)', function() {
env.it('a spec', function() {}); env.it('a spec', function() {});
}); });
env.execute(null, function() { await env.execute();
expect(reporter.suiteDone).toHaveBeenCalledWith(
jasmine.objectContaining({ expect(reporter.suiteDone).toHaveBeenCalledWith(
deprecationWarnings: [ jasmine.objectContaining({
jasmine.objectContaining({ deprecationWarnings: [
message: jasmine.stringMatching(/^the message/) jasmine.objectContaining({
}) message: jasmine.stringMatching(/^the message/)
] })
}) ]
); })
expect(console.error).toHaveBeenCalledWith( );
jasmine.stringMatching( expect(console.error).toHaveBeenCalledWith(
/^DEPRECATION: the message \(in suite: a suite\)/ jasmine.stringMatching(/^DEPRECATION: the message \(in suite: a suite\)/)
) );
);
done();
});
}); });
it('reports a deprecation on a spec', function(done) { it('reports a deprecation on a spec', async function() {
const reporter = jasmine.createSpyObj('reporter', ['specDone']); const reporter = jasmine.createSpyObj('reporter', ['specDone']);
env.addReporter(reporter); env.addReporter(reporter);
spyOn(console, 'error'); spyOn(console, 'error');
@@ -79,26 +75,25 @@ describe('Deprecation (integration)', function() {
}); });
}); });
env.execute(null, function() { await env.execute();
expect(reporter.specDone).toHaveBeenCalledWith(
jasmine.objectContaining({ expect(reporter.specDone).toHaveBeenCalledWith(
deprecationWarnings: [ jasmine.objectContaining({
jasmine.objectContaining({ deprecationWarnings: [
message: jasmine.stringMatching(/^the message/) jasmine.objectContaining({
}) message: jasmine.stringMatching(/^the message/)
] })
}) ]
); })
expect(console.error).toHaveBeenCalledWith( );
jasmine.stringMatching( expect(console.error).toHaveBeenCalledWith(
/^DEPRECATION: the message \(in spec: a suite a spec\)/ jasmine.stringMatching(
) /^DEPRECATION: the message \(in spec: a suite a spec\)/
); )
done(); );
});
}); });
it('omits the suite or spec context when ignoreRunnable is true', function(done) { it('omits the suite or spec context when ignoreRunnable is true', async function() {
const reporter = jasmine.createSpyObj('reporter', ['jasmineDone']); const reporter = jasmine.createSpyObj('reporter', ['jasmineDone']);
env.addReporter(reporter); env.addReporter(reporter);
spyOn(console, 'error'); spyOn(console, 'error');
@@ -107,27 +102,26 @@ describe('Deprecation (integration)', function() {
env.deprecated('the message', { ignoreRunnable: true }); env.deprecated('the message', { ignoreRunnable: true });
}); });
env.execute(null, function() { await env.execute();
expect(reporter.jasmineDone).toHaveBeenCalledWith(
jasmine.objectContaining({ expect(reporter.jasmineDone).toHaveBeenCalledWith(
deprecationWarnings: [ jasmine.objectContaining({
jasmine.objectContaining({ deprecationWarnings: [
message: jasmine.stringMatching(/^the message/) jasmine.objectContaining({
}) message: jasmine.stringMatching(/^the message/)
] })
}) ]
); })
expect(console.error).toHaveBeenCalledWith( );
jasmine.stringMatching(/the message/) expect(console.error).toHaveBeenCalledWith(
); jasmine.stringMatching(/the message/)
expect(console.error).not.toHaveBeenCalledWith( );
jasmine.stringMatching(/a spec/) expect(console.error).not.toHaveBeenCalledWith(
); jasmine.stringMatching(/a spec/)
done(); );
});
}); });
it('includes the stack trace', function(done) { it('includes the stack trace', async function() {
const reporter = jasmine.createSpyObj('reporter', ['specDone']); const reporter = jasmine.createSpyObj('reporter', ['specDone']);
env.addReporter(reporter); env.addReporter(reporter);
spyOn(console, 'error'); spyOn(console, 'error');
@@ -138,25 +132,24 @@ describe('Deprecation (integration)', function() {
}); });
}); });
env.execute(null, function() { await env.execute();
expect(reporter.specDone).toHaveBeenCalledWith(
jasmine.objectContaining({ expect(reporter.specDone).toHaveBeenCalledWith(
deprecationWarnings: [ jasmine.objectContaining({
jasmine.objectContaining({ deprecationWarnings: [
stack: jasmine.stringMatching(/DeprecationSpec.js/) jasmine.objectContaining({
}) stack: jasmine.stringMatching(/DeprecationSpec.js/)
] })
}) ]
); })
expect(console.error).toHaveBeenCalled(); );
expect(console.error.calls.argsFor(0)[0].replace(/\n/g, 'NL')).toMatch( expect(console.error).toHaveBeenCalled();
/^DEPRECATION: the message \(in spec: a suite a spec\)NL.*DeprecationSpec.js/ expect(console.error.calls.argsFor(0)[0].replace(/\n/g, 'NL')).toMatch(
); /^DEPRECATION: the message \(in spec: a suite a spec\)NL.*DeprecationSpec.js/
done(); );
});
}); });
it('excludes the stack trace when omitStackTrace is true', function(done) { it('excludes the stack trace when omitStackTrace is true', async function() {
const reporter = jasmine.createSpyObj('reporter', ['specDone']); const reporter = jasmine.createSpyObj('reporter', ['specDone']);
env.addReporter(reporter); env.addReporter(reporter);
spyOn(console, 'error'); spyOn(console, 'error');
@@ -167,25 +160,24 @@ describe('Deprecation (integration)', function() {
}); });
}); });
env.execute(null, function() { await env.execute();
expect(reporter.specDone).toHaveBeenCalledWith(
jasmine.objectContaining({ expect(reporter.specDone).toHaveBeenCalledWith(
deprecationWarnings: [ jasmine.objectContaining({
jasmine.objectContaining({ deprecationWarnings: [
stack: jasmine.falsy() jasmine.objectContaining({
}) stack: jasmine.falsy()
] })
}) ]
); })
expect(console.error).toHaveBeenCalled(); );
expect(console.error).not.toHaveBeenCalledWith( expect(console.error).toHaveBeenCalled();
jasmine.stringMatching(/DeprecationSpec.js/) expect(console.error).not.toHaveBeenCalledWith(
); jasmine.stringMatching(/DeprecationSpec.js/)
done(); );
});
}); });
it('emits a given deprecation only once', function(done) { it('emits a given deprecation only once', async function() {
const reporter = jasmine.createSpyObj('reporter', [ const reporter = jasmine.createSpyObj('reporter', [
'specDone', 'specDone',
'suiteDone' 'suiteDone'
@@ -205,43 +197,40 @@ describe('Deprecation (integration)', function() {
}); });
}); });
env.execute(null, function() { await env.execute();
expect(reporter.suiteDone).toHaveBeenCalledWith(
jasmine.objectContaining({ expect(reporter.suiteDone).toHaveBeenCalledWith(
deprecationWarnings: [ jasmine.objectContaining({
// only one deprecationWarnings: [
jasmine.objectContaining({ // only one
message: jasmine.stringMatching(/^the message/) jasmine.objectContaining({
}) message: jasmine.stringMatching(/^the message/)
] })
}) ]
); })
expect(reporter.specDone).toHaveBeenCalledWith( );
jasmine.objectContaining({ expect(reporter.specDone).toHaveBeenCalledWith(
deprecationWarnings: [ jasmine.objectContaining({
// only the other one deprecationWarnings: [
jasmine.objectContaining({ // only the other one
message: jasmine.stringMatching(/^a different message/) jasmine.objectContaining({
}) message: jasmine.stringMatching(/^a different message/)
] })
}) ]
); })
expect(console.error).toHaveBeenCalledTimes(2); );
expect(console.error).toHaveBeenCalledWith( expect(console.error).toHaveBeenCalledTimes(2);
jasmine.stringMatching( expect(console.error).toHaveBeenCalledWith(
/^DEPRECATION: the message \(in suite: a suite\)/ jasmine.stringMatching(/^DEPRECATION: the message \(in suite: a suite\)/)
) );
); expect(console.error).toHaveBeenCalledWith(
expect(console.error).toHaveBeenCalledWith( jasmine.stringMatching(
jasmine.stringMatching( /^DEPRECATION: a different message \(in spec: a suite a spec\)/
/^DEPRECATION: a different message \(in spec: a suite a spec\)/ )
) );
);
done();
});
}); });
it('emits a given deprecation each time when config.verboseDeprecations is true', function(done) { it('emits a given deprecation each time when config.verboseDeprecations is true', async function() {
const reporter = jasmine.createSpyObj('reporter', [ const reporter = jasmine.createSpyObj('reporter', [
'specDone', 'specDone',
'suiteDone' 'suiteDone'
@@ -262,46 +251,45 @@ describe('Deprecation (integration)', function() {
}); });
}); });
env.execute(null, function() { await env.execute();
expect(reporter.suiteDone).toHaveBeenCalledWith(
jasmine.objectContaining({ expect(reporter.suiteDone).toHaveBeenCalledWith(
deprecationWarnings: [ jasmine.objectContaining({
jasmine.objectContaining({ deprecationWarnings: [
message: jasmine.stringMatching(/^the message/) jasmine.objectContaining({
}), message: jasmine.stringMatching(/^the message/)
jasmine.objectContaining({ }),
message: jasmine.stringMatching(/^the message/) jasmine.objectContaining({
}) message: jasmine.stringMatching(/^the message/)
] })
}) ]
); })
expect(reporter.specDone).toHaveBeenCalledWith( );
jasmine.objectContaining({ expect(reporter.specDone).toHaveBeenCalledWith(
deprecationWarnings: [ jasmine.objectContaining({
jasmine.objectContaining({ deprecationWarnings: [
message: jasmine.stringMatching(/^the message/) jasmine.objectContaining({
}) message: jasmine.stringMatching(/^the message/)
] })
}) ]
); })
expect(console.error).toHaveBeenCalledTimes(3); );
expect(console.error.calls.argsFor(0)[0]).toMatch( expect(console.error).toHaveBeenCalledTimes(3);
/^DEPRECATION: the message \(in suite: a suite\)/ expect(console.error.calls.argsFor(0)[0]).toMatch(
); /^DEPRECATION: the message \(in suite: a suite\)/
expect(console.error.calls.argsFor(1)[0]).toMatch( );
/^DEPRECATION: the message \(in suite: a suite\)/ expect(console.error.calls.argsFor(1)[0]).toMatch(
); /^DEPRECATION: the message \(in suite: a suite\)/
expect(console.error.calls.argsFor(2)[0]).toMatch( );
/^DEPRECATION: the message \(in spec: a suite a spec\)/ expect(console.error.calls.argsFor(2)[0]).toMatch(
); /^DEPRECATION: the message \(in spec: a suite a spec\)/
expect(console.error.calls.argsFor(2)[0]).toMatch( );
/^DEPRECATION: the message \(in spec: a suite a spec\)/ expect(console.error.calls.argsFor(2)[0]).toMatch(
); /^DEPRECATION: the message \(in spec: a suite a spec\)/
done(); );
});
}); });
it('handles deprecations that occur before execute() is called', function(done) { it('handles deprecations that occur before execute() is called', async function() {
const reporter = jasmine.createSpyObj('reporter', ['jasmineDone']); const reporter = jasmine.createSpyObj('reporter', ['jasmineDone']);
env.addReporter(reporter); env.addReporter(reporter);
spyOn(console, 'error'); spyOn(console, 'error');
@@ -309,20 +297,19 @@ describe('Deprecation (integration)', function() {
env.deprecated('the message'); env.deprecated('the message');
env.it('a spec', function() {}); env.it('a spec', function() {});
env.execute(null, function() { await env.execute();
expect(reporter.jasmineDone).toHaveBeenCalledWith(
jasmine.objectContaining({ expect(reporter.jasmineDone).toHaveBeenCalledWith(
deprecationWarnings: [ jasmine.objectContaining({
jasmine.objectContaining({ deprecationWarnings: [
message: jasmine.stringMatching(/^the message/) jasmine.objectContaining({
}) message: jasmine.stringMatching(/^the message/)
] })
}) ]
); })
expect(console.error).toHaveBeenCalledWith( );
jasmine.stringMatching(/^DEPRECATION: the message/) expect(console.error).toHaveBeenCalledWith(
); jasmine.stringMatching(/^DEPRECATION: the message/)
done(); );
});
}); });
}); });

View File

@@ -10,156 +10,158 @@ describe('Matchers (Integration)', function() {
}); });
function verifyPasses(expectations) { function verifyPasses(expectations) {
it('passes', function(done) { it('passes', async function() {
env.it('a spec', function() { env.it('a spec', function() {
expectations(env); expectations(env);
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('passed'); env.addReporter(reporter);
expect(result.passedExpectations.length) await env.execute();
.withContext('Number of passed expectations')
.toEqual(1);
expect(result.failedExpectations.length)
.withContext('Number of failed expectations')
.toEqual(0);
expect(
result.failedExpectations[0] && result.failedExpectations[0].message
)
.withContext('Failure message')
.toBeUndefined();
};
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('passed');
expect(result.passedExpectations.length)
.withContext('Number of passed expectations')
.toEqual(1);
expect(result.failedExpectations.length)
.withContext('Number of failed expectations')
.toEqual(0);
expect(
result.failedExpectations[0] && result.failedExpectations[0].message
)
.withContext('Failure message')
.toBeUndefined();
}); });
} }
function verifyFails(expectations) { function verifyFails(expectations) {
it('fails', function(done) { it('fails', async function() {
env.it('a spec', function() { env.it('a spec', function() {
expectations(env); expectations(env);
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('failed'); env.addReporter(reporter);
expect(result.failedExpectations.length) await env.execute();
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message)
.withContext(
'Failed with a thrown error rather than a matcher failure'
)
.not.toMatch(/^Error: /);
expect(result.failedExpectations[0].message)
.withContext(
'Failed with a thrown type error rather than a matcher failure'
)
.not.toMatch(/^TypeError: /);
expect(result.failedExpectations[0].matcherName)
.withContext('Matcher name')
.not.toEqual('');
};
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('failed');
expect(result.failedExpectations.length)
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message)
.withContext('Failed with a thrown error rather than a matcher failure')
.not.toMatch(/^Error: /);
expect(result.failedExpectations[0].message)
.withContext(
'Failed with a thrown type error rather than a matcher failure'
)
.not.toMatch(/^TypeError: /);
expect(result.failedExpectations[0].matcherName)
.withContext('Matcher name')
.not.toEqual('');
}); });
} }
function verifyFailsWithCustomObjectFormatters(config) { function verifyFailsWithCustomObjectFormatters(config) {
it('uses custom object formatters', function(done) { it('uses custom object formatters', async function() {
env.it('a spec', function() { env.it('a spec', function() {
env.addCustomObjectFormatter(config.formatter); env.addCustomObjectFormatter(config.formatter);
config.expectations(env); config.expectations(env);
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('failed'); env.addReporter(reporter);
expect(result.failedExpectations.length) await env.execute();
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message).toEqual(
config.expectedMessage
);
};
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('failed');
expect(result.failedExpectations.length)
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message).toEqual(
config.expectedMessage
);
}); });
} }
function verifyPassesAsync(expectations) { function verifyPassesAsync(expectations) {
it('passes', function(done) { it('passes', async function() {
env.it('a spec', function() { env.it('a spec', function() {
return expectations(env); return expectations(env);
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('passed'); env.addReporter(reporter);
expect(result.passedExpectations.length) await env.execute();
.withContext('Number of passed expectations')
.toEqual(1);
expect(result.failedExpectations.length)
.withContext('Number of failed expectations')
.toEqual(0);
expect(
result.failedExpectations[0] && result.failedExpectations[0].message
)
.withContext('Failure message')
.toBeUndefined();
};
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('passed');
expect(result.passedExpectations.length)
.withContext('Number of passed expectations')
.toEqual(1);
expect(result.failedExpectations.length)
.withContext('Number of failed expectations')
.toEqual(0);
expect(
result.failedExpectations[0] && result.failedExpectations[0].message
)
.withContext('Failure message')
.toBeUndefined();
}); });
} }
function verifyFailsAsync(expectations) { function verifyFailsAsync(expectations) {
it('fails', function(done) { it('fails', async function() {
env.it('a spec', function() { env.it('a spec', function() {
return expectations(env); return expectations(env);
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('failed'); env.addReporter(reporter);
expect(result.failedExpectations.length) await env.execute();
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message)
.withContext(
'Failed with a thrown error rather than a matcher failure'
)
.not.toMatch(/^Error: /);
expect(result.failedExpectations[0].matcherName)
.withContext('Matcher name')
.not.toEqual('');
};
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('failed');
expect(result.failedExpectations.length)
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message)
.withContext('Failed with a thrown error rather than a matcher failure')
.not.toMatch(/^Error: /);
expect(result.failedExpectations[0].matcherName)
.withContext('Matcher name')
.not.toEqual('');
}); });
} }
function verifyFailsWithCustomObjectFormattersAsync(config) { function verifyFailsWithCustomObjectFormattersAsync(config) {
it('uses custom object formatters', function(done) { it('uses custom object formatters', async function() {
const env = new jasmineUnderTest.Env(); const env = new jasmineUnderTest.Env();
env.it('a spec', function() { env.it('a spec', function() {
env.addCustomObjectFormatter(config.formatter); env.addCustomObjectFormatter(config.formatter);
return config.expectations(env); return config.expectations(env);
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('failed'); env.addReporter(reporter);
expect(result.failedExpectations.length) await env.execute();
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message).toEqual(
config.expectedMessage
);
};
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('failed');
expect(result.failedExpectations.length)
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message).toEqual(
config.expectedMessage
);
}); });
} }
@@ -753,76 +755,79 @@ describe('Matchers (Integration)', function() {
}); });
describe('When an async matcher is used with .already()', function() { describe('When an async matcher is used with .already()', function() {
it('propagates the matcher result when the promise is resolved', function(done) { it('propagates the matcher result when the promise is resolved', async function() {
env.it('a spec', function() { env.it('a spec', function() {
return env.expectAsync(Promise.resolve()).already.toBeRejected(); return env.expectAsync(Promise.resolve()).already.toBeRejected();
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('failed'); env.addReporter(reporter);
expect(result.failedExpectations.length) await env.execute();
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message).toEqual(
'Expected [object Promise] to be rejected.'
);
expect(result.failedExpectations[0].matcherName)
.withContext('Matcher name')
.not.toEqual('');
};
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('failed');
expect(result.failedExpectations.length)
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message).toEqual(
'Expected [object Promise] to be rejected.'
);
expect(result.failedExpectations[0].matcherName)
.withContext('Matcher name')
.not.toEqual('');
}); });
it('propagates the matcher result when the promise is rejected', function(done) { it('propagates the matcher result when the promise is rejected', async function() {
env.it('a spec', function() { env.it('a spec', function() {
return env return env
.expectAsync(Promise.reject(new Error('nope'))) .expectAsync(Promise.reject(new Error('nope')))
.already.toBeResolved(); .already.toBeResolved();
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('failed'); env.addReporter(reporter);
expect(result.failedExpectations.length) await env.execute();
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message).toEqual(
'Expected a promise to be resolved but it was ' +
'rejected with Error: nope.'
);
expect(result.failedExpectations[0].matcherName)
.withContext('Matcher name')
.not.toEqual('');
};
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('failed');
expect(result.failedExpectations.length)
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message).toEqual(
'Expected a promise to be resolved but it was ' +
'rejected with Error: nope.'
);
expect(result.failedExpectations[0].matcherName)
.withContext('Matcher name')
.not.toEqual('');
}); });
it('fails when the promise is pending', function(done) { it('fails when the promise is pending', async function() {
const promise = new Promise(function() {}); const promise = new Promise(function() {});
env.it('a spec', function() { env.it('a spec', function() {
return env.expectAsync(promise).already.toBeResolved(); return env.expectAsync(promise).already.toBeResolved();
}); });
const specExpectations = function(result) { const reporter = jasmine.createSpyObj('reporter', ['specDone']);
expect(result.status).toEqual('failed'); env.addReporter(reporter);
expect(result.failedExpectations.length) await env.execute();
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message).toEqual(
'Expected a promise to be settled ' +
'(via expectAsync(...).already) but it was pending.'
);
expect(result.failedExpectations[0].matcherName)
.withContext('Matcher name')
.not.toEqual('');
};
env.addReporter({ specDone: specExpectations }); expect(reporter.specDone).toHaveBeenCalledTimes(1);
env.execute(null, done); const result = reporter.specDone.calls.argsFor(0)[0];
expect(result.status).toEqual('failed');
expect(result.failedExpectations.length)
.withContext('Number of failed expectations')
.toEqual(1);
expect(result.failedExpectations[0].message).toEqual(
'Expected a promise to be settled ' +
'(via expectAsync(...).already) but it was pending.'
);
expect(result.failedExpectations[0].matcherName)
.withContext('Matcher name')
.not.toEqual('');
}); });
}); });
}); });