From c24b2f5a731f986c87a1f5efc6ed087ca9e9ab83 Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Sat, 14 May 2022 12:05:53 -0700 Subject: [PATCH] Converted some integration specs to async/await --- .../AsymmetricEqualityTestersSpec.js | 70 ++-- spec/core/integration/CustomMatchersSpec.js | 136 +++---- .../integration/CustomObjectFormatterSpec.js | 40 +- spec/core/integration/DeprecationSpec.js | 375 +++++++++--------- spec/core/integration/MatchersSpec.js | 293 +++++++------- 5 files changed, 450 insertions(+), 464 deletions(-) diff --git a/spec/core/integration/AsymmetricEqualityTestersSpec.js b/spec/core/integration/AsymmetricEqualityTestersSpec.js index 07c8fc58..9fb4d6ca 100644 --- a/spec/core/integration/AsymmetricEqualityTestersSpec.js +++ b/spec/core/integration/AsymmetricEqualityTestersSpec.js @@ -1,55 +1,57 @@ describe('Asymmetric equality testers (Integration)', function() { function verifyPasses(expectations) { - it('passes', function(done) { + it('passes', async function() { const env = new jasmineUnderTest.Env(); env.it('a spec', function() { expectations(env); }); - const specExpectations = function(result) { - 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(); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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) { - it('fails', function(done) { + it('fails', async function() { const env = new jasmineUnderTest.Env(); env.it('a spec', function() { expectations(env); }); - const specExpectations = function(result) { - 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(''); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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(''); }); } diff --git a/spec/core/integration/CustomMatchersSpec.js b/spec/core/integration/CustomMatchersSpec.js index eba7bd16..e56f8a04 100644 --- a/spec/core/integration/CustomMatchersSpec.js +++ b/spec/core/integration/CustomMatchersSpec.js @@ -10,7 +10,7 @@ describe('Custom Matchers (Integration)', function() { 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.addMatchers({ matcherForSpec: function() { @@ -37,20 +37,18 @@ describe('Custom Matchers (Integration)', function() { }); 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.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.addMatchers({ toBeReal: function() { @@ -65,15 +63,16 @@ describe('Custom Matchers (Integration)', function() { env.expect(true).toBeReal(); }); - const specExpectations = function(result) { - expect(result.status).toEqual('passed'); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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() { const customEqualityFn = function(a, b) { // All "foo*" strings match each other. @@ -99,15 +98,16 @@ describe('Custom Matchers (Integration)', function() { .toEqual(jasmineUnderTest.arrayWithExactContents(['fooBar'])); }); - const specExpectations = function(result) { - expect(result.status).toEqual('passed'); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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() { const customEqualityFn = function(a, b) { // "foo" is not equal to anything @@ -120,18 +120,19 @@ describe('Custom Matchers (Integration)', function() { env.expect({ foo: 'foo' }).toEqual({ foo: 'foo' }); }); - const specExpectations = function(result) { - expect(result.status).toEqual('failed'); - expect(result.failedExpectations[0].message).toEqual( - "Expected $.foo = 'foo' to equal 'foo'." - ); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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.addMatchers({ toBeReal: function() { @@ -149,15 +150,16 @@ describe('Custom Matchers (Integration)', function() { env.expect(true).not.toBeReal(); }); - const specExpectations = function(result) { - expect(result.status).toEqual('passed'); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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.addMatchers({ toBeReal: function() { @@ -172,17 +174,18 @@ describe('Custom Matchers (Integration)', function() { env.expect('a').toBeReal(); }); - const specExpectations = function(result) { - expect(result.failedExpectations[0].message).toEqual( - "Expected 'a' to be real." - ); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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 .createSpy('argument spy') .and.returnValue({ pass: true }); @@ -199,17 +202,13 @@ describe('Custom Matchers (Integration)', function() { env.expect(true).toBeReal('arg1', 'arg2'); }); - const specExpectations = function() { - expect(argumentSpy).toHaveBeenCalledWith(true); - expect(argumentSpy).toHaveBeenCalledWith(true, 'arg'); - expect(argumentSpy).toHaveBeenCalledWith(true, 'arg1', 'arg2'); - }; - - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + await env.execute(); + expect(argumentSpy).toHaveBeenCalledWith(true); + expect(argumentSpy).toHaveBeenCalledWith(true, 'arg'); + expect(argumentSpy).toHaveBeenCalledWith(true, 'arg1', 'arg2'); }); - 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() { return { compare: function() { @@ -229,17 +228,13 @@ describe('Custom Matchers (Integration)', function() { env.expect(true).toBeReal(); }); - const specExpectations = function() { - expect(matcherFactorySpy).toHaveBeenCalledWith( - jasmine.any(jasmineUnderTest.MatchersUtil) - ); - }; - - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + await env.execute(); + expect(matcherFactorySpy).toHaveBeenCalledWith( + jasmine.any(jasmineUnderTest.MatchersUtil) + ); }); - 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) { return { compare: function(actual, expected) { @@ -262,12 +257,13 @@ describe('Custom Matchers (Integration)', function() { env.expect([1, 2]).toBeArrayWithFirstElement('1'); }); - const specExpectations = function(result) { - expect(customEqualityFn).toHaveBeenCalledWith(1, '1'); - expect(result.failedExpectations).toEqual([]); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + const result = reporter.specDone.calls.argsFor(0)[0]; + expect(customEqualityFn).toHaveBeenCalledWith(1, '1'); + expect(result.failedExpectations).toEqual([]); }); }); diff --git a/spec/core/integration/CustomObjectFormatterSpec.js b/spec/core/integration/CustomObjectFormatterSpec.js index 5a696b82..284ad87c 100644 --- a/spec/core/integration/CustomObjectFormatterSpec.js +++ b/spec/core/integration/CustomObjectFormatterSpec.js @@ -6,7 +6,7 @@ describe('Custom object formatters', function() { 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.addCustomObjectFormatter(function(obj) { return 'custom(' + obj + ')'; @@ -22,21 +22,19 @@ describe('Custom object formatters', function() { const specDone = function(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.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.expect(42).toBeUndefined(); }); @@ -57,18 +55,16 @@ describe('Custom object formatters', function() { const specDone = function(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.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() { diff --git a/spec/core/integration/DeprecationSpec.js b/spec/core/integration/DeprecationSpec.js index 29748b84..cbb95b2f 100644 --- a/spec/core/integration/DeprecationSpec.js +++ b/spec/core/integration/DeprecationSpec.js @@ -10,7 +10,7 @@ describe('Deprecation (integration)', function() { 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']); env.addReporter(reporter); spyOn(console, 'error'); @@ -20,24 +20,23 @@ describe('Deprecation (integration)', function() { }); env.it('a spec', function() {}); - env.execute(null, function() { - expect(reporter.jasmineDone).toHaveBeenCalledWith( - jasmine.objectContaining({ - deprecationWarnings: [ - jasmine.objectContaining({ - message: jasmine.stringMatching(/^the message/) - }) - ] - }) - ); - expect(console.error).toHaveBeenCalledWith( - jasmine.stringMatching(/^DEPRECATION: the message/) - ); - done(); - }); + await env.execute(); + + expect(reporter.jasmineDone).toHaveBeenCalledWith( + jasmine.objectContaining({ + deprecationWarnings: [ + jasmine.objectContaining({ + message: jasmine.stringMatching(/^the message/) + }) + ] + }) + ); + expect(console.error).toHaveBeenCalledWith( + jasmine.stringMatching(/^DEPRECATION: the message/) + ); }); - 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']); env.addReporter(reporter); spyOn(console, 'error'); @@ -49,26 +48,23 @@ describe('Deprecation (integration)', function() { env.it('a spec', function() {}); }); - env.execute(null, function() { - expect(reporter.suiteDone).toHaveBeenCalledWith( - jasmine.objectContaining({ - deprecationWarnings: [ - jasmine.objectContaining({ - message: jasmine.stringMatching(/^the message/) - }) - ] - }) - ); - expect(console.error).toHaveBeenCalledWith( - jasmine.stringMatching( - /^DEPRECATION: the message \(in suite: a suite\)/ - ) - ); - done(); - }); + await env.execute(); + + expect(reporter.suiteDone).toHaveBeenCalledWith( + jasmine.objectContaining({ + deprecationWarnings: [ + jasmine.objectContaining({ + message: jasmine.stringMatching(/^the message/) + }) + ] + }) + ); + expect(console.error).toHaveBeenCalledWith( + jasmine.stringMatching(/^DEPRECATION: the message \(in suite: a suite\)/) + ); }); - it('reports a deprecation on a spec', function(done) { + it('reports a deprecation on a spec', async function() { const reporter = jasmine.createSpyObj('reporter', ['specDone']); env.addReporter(reporter); spyOn(console, 'error'); @@ -79,26 +75,25 @@ describe('Deprecation (integration)', function() { }); }); - env.execute(null, function() { - expect(reporter.specDone).toHaveBeenCalledWith( - jasmine.objectContaining({ - deprecationWarnings: [ - jasmine.objectContaining({ - message: jasmine.stringMatching(/^the message/) - }) - ] - }) - ); - expect(console.error).toHaveBeenCalledWith( - jasmine.stringMatching( - /^DEPRECATION: the message \(in spec: a suite a spec\)/ - ) - ); - done(); - }); + await env.execute(); + + expect(reporter.specDone).toHaveBeenCalledWith( + jasmine.objectContaining({ + deprecationWarnings: [ + jasmine.objectContaining({ + message: jasmine.stringMatching(/^the message/) + }) + ] + }) + ); + expect(console.error).toHaveBeenCalledWith( + jasmine.stringMatching( + /^DEPRECATION: the message \(in spec: a suite a spec\)/ + ) + ); }); - 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']); env.addReporter(reporter); spyOn(console, 'error'); @@ -107,27 +102,26 @@ describe('Deprecation (integration)', function() { env.deprecated('the message', { ignoreRunnable: true }); }); - env.execute(null, function() { - expect(reporter.jasmineDone).toHaveBeenCalledWith( - jasmine.objectContaining({ - deprecationWarnings: [ - jasmine.objectContaining({ - message: jasmine.stringMatching(/^the message/) - }) - ] - }) - ); - expect(console.error).toHaveBeenCalledWith( - jasmine.stringMatching(/the message/) - ); - expect(console.error).not.toHaveBeenCalledWith( - jasmine.stringMatching(/a spec/) - ); - done(); - }); + await env.execute(); + + expect(reporter.jasmineDone).toHaveBeenCalledWith( + jasmine.objectContaining({ + deprecationWarnings: [ + jasmine.objectContaining({ + message: jasmine.stringMatching(/^the message/) + }) + ] + }) + ); + expect(console.error).toHaveBeenCalledWith( + jasmine.stringMatching(/the message/) + ); + expect(console.error).not.toHaveBeenCalledWith( + jasmine.stringMatching(/a spec/) + ); }); - it('includes the stack trace', function(done) { + it('includes the stack trace', async function() { const reporter = jasmine.createSpyObj('reporter', ['specDone']); env.addReporter(reporter); spyOn(console, 'error'); @@ -138,25 +132,24 @@ describe('Deprecation (integration)', function() { }); }); - env.execute(null, function() { - expect(reporter.specDone).toHaveBeenCalledWith( - jasmine.objectContaining({ - deprecationWarnings: [ - jasmine.objectContaining({ - stack: jasmine.stringMatching(/DeprecationSpec.js/) - }) - ] - }) - ); - expect(console.error).toHaveBeenCalled(); - 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(); - }); + await env.execute(); + + expect(reporter.specDone).toHaveBeenCalledWith( + jasmine.objectContaining({ + deprecationWarnings: [ + jasmine.objectContaining({ + stack: jasmine.stringMatching(/DeprecationSpec.js/) + }) + ] + }) + ); + expect(console.error).toHaveBeenCalled(); + expect(console.error.calls.argsFor(0)[0].replace(/\n/g, 'NL')).toMatch( + /^DEPRECATION: the message \(in spec: a suite a spec\)NL.*DeprecationSpec.js/ + ); }); - 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']); env.addReporter(reporter); spyOn(console, 'error'); @@ -167,25 +160,24 @@ describe('Deprecation (integration)', function() { }); }); - env.execute(null, function() { - expect(reporter.specDone).toHaveBeenCalledWith( - jasmine.objectContaining({ - deprecationWarnings: [ - jasmine.objectContaining({ - stack: jasmine.falsy() - }) - ] - }) - ); - expect(console.error).toHaveBeenCalled(); - expect(console.error).not.toHaveBeenCalledWith( - jasmine.stringMatching(/DeprecationSpec.js/) - ); - done(); - }); + await env.execute(); + + expect(reporter.specDone).toHaveBeenCalledWith( + jasmine.objectContaining({ + deprecationWarnings: [ + jasmine.objectContaining({ + stack: jasmine.falsy() + }) + ] + }) + ); + expect(console.error).toHaveBeenCalled(); + expect(console.error).not.toHaveBeenCalledWith( + jasmine.stringMatching(/DeprecationSpec.js/) + ); }); - it('emits a given deprecation only once', function(done) { + it('emits a given deprecation only once', async function() { const reporter = jasmine.createSpyObj('reporter', [ 'specDone', 'suiteDone' @@ -205,43 +197,40 @@ describe('Deprecation (integration)', function() { }); }); - env.execute(null, function() { - expect(reporter.suiteDone).toHaveBeenCalledWith( - jasmine.objectContaining({ - deprecationWarnings: [ - // only one - jasmine.objectContaining({ - message: jasmine.stringMatching(/^the message/) - }) - ] - }) - ); - expect(reporter.specDone).toHaveBeenCalledWith( - jasmine.objectContaining({ - deprecationWarnings: [ - // only the other one - jasmine.objectContaining({ - message: jasmine.stringMatching(/^a different message/) - }) - ] - }) - ); - expect(console.error).toHaveBeenCalledTimes(2); - expect(console.error).toHaveBeenCalledWith( - jasmine.stringMatching( - /^DEPRECATION: the message \(in suite: a suite\)/ - ) - ); - expect(console.error).toHaveBeenCalledWith( - jasmine.stringMatching( - /^DEPRECATION: a different message \(in spec: a suite a spec\)/ - ) - ); - done(); - }); + await env.execute(); + + expect(reporter.suiteDone).toHaveBeenCalledWith( + jasmine.objectContaining({ + deprecationWarnings: [ + // only one + jasmine.objectContaining({ + message: jasmine.stringMatching(/^the message/) + }) + ] + }) + ); + expect(reporter.specDone).toHaveBeenCalledWith( + jasmine.objectContaining({ + deprecationWarnings: [ + // only the other one + jasmine.objectContaining({ + message: jasmine.stringMatching(/^a different message/) + }) + ] + }) + ); + expect(console.error).toHaveBeenCalledTimes(2); + expect(console.error).toHaveBeenCalledWith( + jasmine.stringMatching(/^DEPRECATION: the message \(in suite: a suite\)/) + ); + expect(console.error).toHaveBeenCalledWith( + jasmine.stringMatching( + /^DEPRECATION: a different message \(in spec: a suite a spec\)/ + ) + ); }); - 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', [ 'specDone', 'suiteDone' @@ -262,46 +251,45 @@ describe('Deprecation (integration)', function() { }); }); - env.execute(null, function() { - expect(reporter.suiteDone).toHaveBeenCalledWith( - jasmine.objectContaining({ - deprecationWarnings: [ - jasmine.objectContaining({ - message: jasmine.stringMatching(/^the message/) - }), - jasmine.objectContaining({ - message: jasmine.stringMatching(/^the message/) - }) - ] - }) - ); - expect(reporter.specDone).toHaveBeenCalledWith( - jasmine.objectContaining({ - deprecationWarnings: [ - jasmine.objectContaining({ - message: jasmine.stringMatching(/^the message/) - }) - ] - }) - ); - expect(console.error).toHaveBeenCalledTimes(3); - 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(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(); - }); + await env.execute(); + + expect(reporter.suiteDone).toHaveBeenCalledWith( + jasmine.objectContaining({ + deprecationWarnings: [ + jasmine.objectContaining({ + message: jasmine.stringMatching(/^the message/) + }), + jasmine.objectContaining({ + message: jasmine.stringMatching(/^the message/) + }) + ] + }) + ); + expect(reporter.specDone).toHaveBeenCalledWith( + jasmine.objectContaining({ + deprecationWarnings: [ + jasmine.objectContaining({ + message: jasmine.stringMatching(/^the message/) + }) + ] + }) + ); + expect(console.error).toHaveBeenCalledTimes(3); + 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(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\)/ + ); }); - 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']); env.addReporter(reporter); spyOn(console, 'error'); @@ -309,20 +297,19 @@ describe('Deprecation (integration)', function() { env.deprecated('the message'); env.it('a spec', function() {}); - env.execute(null, function() { - expect(reporter.jasmineDone).toHaveBeenCalledWith( - jasmine.objectContaining({ - deprecationWarnings: [ - jasmine.objectContaining({ - message: jasmine.stringMatching(/^the message/) - }) - ] - }) - ); - expect(console.error).toHaveBeenCalledWith( - jasmine.stringMatching(/^DEPRECATION: the message/) - ); - done(); - }); + await env.execute(); + + expect(reporter.jasmineDone).toHaveBeenCalledWith( + jasmine.objectContaining({ + deprecationWarnings: [ + jasmine.objectContaining({ + message: jasmine.stringMatching(/^the message/) + }) + ] + }) + ); + expect(console.error).toHaveBeenCalledWith( + jasmine.stringMatching(/^DEPRECATION: the message/) + ); }); }); diff --git a/spec/core/integration/MatchersSpec.js b/spec/core/integration/MatchersSpec.js index 0ab3379d..1ce62c9f 100755 --- a/spec/core/integration/MatchersSpec.js +++ b/spec/core/integration/MatchersSpec.js @@ -10,156 +10,158 @@ describe('Matchers (Integration)', function() { }); function verifyPasses(expectations) { - it('passes', function(done) { + it('passes', async function() { env.it('a spec', function() { expectations(env); }); - const specExpectations = function(result) { - 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(); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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) { - it('fails', function(done) { + it('fails', async function() { env.it('a spec', function() { expectations(env); }); - const specExpectations = function(result) { - 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(''); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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) { - it('uses custom object formatters', function(done) { + it('uses custom object formatters', async function() { env.it('a spec', function() { env.addCustomObjectFormatter(config.formatter); config.expectations(env); }); - const specExpectations = function(result) { - expect(result.status).toEqual('failed'); - expect(result.failedExpectations.length) - .withContext('Number of failed expectations') - .toEqual(1); - expect(result.failedExpectations[0].message).toEqual( - config.expectedMessage - ); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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) { - it('passes', function(done) { + it('passes', async function() { env.it('a spec', function() { return expectations(env); }); - const specExpectations = function(result) { - 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(); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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) { - it('fails', function(done) { + it('fails', async function() { env.it('a spec', function() { return expectations(env); }); - const specExpectations = function(result) { - 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(''); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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) { - it('uses custom object formatters', function(done) { + it('uses custom object formatters', async function() { const env = new jasmineUnderTest.Env(); env.it('a spec', function() { env.addCustomObjectFormatter(config.formatter); return config.expectations(env); }); - const specExpectations = function(result) { - expect(result.status).toEqual('failed'); - expect(result.failedExpectations.length) - .withContext('Number of failed expectations') - .toEqual(1); - expect(result.failedExpectations[0].message).toEqual( - config.expectedMessage - ); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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() { - 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() { return env.expectAsync(Promise.resolve()).already.toBeRejected(); }); - const specExpectations = function(result) { - 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(''); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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() { return env .expectAsync(Promise.reject(new Error('nope'))) .already.toBeResolved(); }); - const specExpectations = function(result) { - 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(''); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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() {}); env.it('a spec', function() { return env.expectAsync(promise).already.toBeResolved(); }); - const specExpectations = function(result) { - 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(''); - }; + const reporter = jasmine.createSpyObj('reporter', ['specDone']); + env.addReporter(reporter); + await env.execute(); - env.addReporter({ specDone: specExpectations }); - env.execute(null, done); + expect(reporter.specDone).toHaveBeenCalledTimes(1); + 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(''); }); }); });