Skip to content

Commit

Permalink
feat: Propagate errors thrown in afterAll blocks
Browse files Browse the repository at this point in the history
If an error is thrown in an afterAll block, it needs to be handled
specially since the error isn't attached to a specific spec.  This
change won't fail any specific test, but will cause a general failure
which will still cause overall run failure.

Closes karma-runner#161
  • Loading branch information
TheModMaker committed Mar 29, 2017
1 parent fe23ac7 commit f3fa264
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/adapter.js
Expand Up @@ -170,6 +170,18 @@ function KarmaReporter (tc, jasmineEnv) {
return suite.description === 'Jasmine_TopLevel_Suite'
}

function handleGlobalErrors (result) {
if (result.failedExpectations && result.failedExpectations.length) {
var message = 'An error was thrown in afterAll'
var steps = result.failedExpectations

This comment has been minimized.

Copy link
@Xesenix

Xesenix Jul 13, 2018

this one can obscure some error informations for example i got this in app with over 900+ test and couldn't find source of issue till i logged the whole result object:

{
    "message": "An error was thrown in afterAll\n[object ErrorEvent] thrown",
    "str": "An error was thrown in afterAll\n[object ErrorEvent] thrown"
  }

each step stack was undefined but in result.fullName i found some info that actualy helped me to localize issue
here is example console.log of result that was casing above obfuscated error

Object{id: 'suite2', description: '.logout()', fullName: 'AccountMenuComponent .logout()', failedExpectations: [Object{matcherName: ..., message: ..., stack: ..., passed: ..., expected: ..., actual: ...}], deprecationWarnings: [], status: 'failed'}
for (var i = 0, l = steps.length; i < l; i++) {
message += '\n' + formatFailedStep(steps[i])
}

tc.error(message)
}
}

/**
* Jasmine 2.0 dispatches the following events:
*
Expand All @@ -191,6 +203,10 @@ function KarmaReporter (tc, jasmineEnv) {

this.jasmineDone = function (result) {
result = result || {}

// Any errors in top-level afterAll blocks are given here.
handleGlobalErrors(result)

tc.complete({
order: result.order,
coverage: window.__coverage__
Expand All @@ -210,6 +226,10 @@ function KarmaReporter (tc, jasmineEnv) {
return
}

// Any errors in afterAll blocks are given here, except for top-level
// afterAll blocks.
handleGlobalErrors(result)

currentSuite = currentSuite.parent
}

Expand Down
17 changes: 17 additions & 0 deletions test/adapter.spec.js
Expand Up @@ -145,6 +145,23 @@ describe('jasmine adapter', function () {
expect(karma.result).toHaveBeenCalled()
})

it('should report errors in afterAll blocks', function () {
spyOn(karma, 'complete')
spyOn(karma, 'error')

var result = {
failedExpectations: []
}

reporter.jasmineDone(result)
expect(karma.error).not.toHaveBeenCalled()

result.failedExpectations.push({})

reporter.jasmineDone(result)
expect(karma.error).toHaveBeenCalled()
})

it('should report executedExpectCount as sum of passed and failed expectations', function () {
karma.result.and.callFake(function (result) {
expect(result.executedExpectationsCount).toBe(2)
Expand Down

0 comments on commit f3fa264

Please sign in to comment.