Skip to content

Commit

Permalink
Report exceptions thrown by a describe before any it calls
Browse files Browse the repository at this point in the history
Previously, these were masked by the "describe with no children" error.
Now they're reported as suite level errors on an empty suite.
  • Loading branch information
sgravrock committed Sep 17, 2022
1 parent 44f331f commit d402599
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
13 changes: 8 additions & 5 deletions lib/jasmine-core/jasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -9872,11 +9872,6 @@ getJasmineRequireObj().SuiteBuilder = function(j$) {
suite.exclude();
}
this.addSpecsToSuite_(suite, definitionFn);
if (suite.parentSuite && !suite.children.length) {
throw new Error(
`describe with no children (describe() or it()): ${suite.getFullName()}`
);
}
return suite;
}

Expand Down Expand Up @@ -10023,11 +10018,19 @@ getJasmineRequireObj().SuiteBuilder = function(j$) {
const parentSuite = this.currentDeclarationSuite_;
parentSuite.addChild(suite);
this.currentDeclarationSuite_ = suite;
let threw = false;

try {
definitionFn();
} catch (e) {
suite.handleException(e);
threw = true;
}

if (suite.parentSuite && !suite.children.length && !threw) {
throw new Error(
`describe with no children (describe() or it()): ${suite.getFullName()}`
);
}

this.currentDeclarationSuite_ = parentSuite;
Expand Down
38 changes: 38 additions & 0 deletions spec/core/integration/EnvSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3966,6 +3966,44 @@ describe('Env integration', function() {
});
});

it('reports a suite level error when a describe fn throws', async function() {
const reporter = jasmine.createSpyObj('reporter', ['suiteDone']);
env.addReporter(reporter);

env.describe('throws before defining specs', function() {
throw new Error('nope');
});

env.describe('throws after defining specs', function() {
env.it('is a spec');
throw new Error('nope');
});

await env.execute();

expect(reporter.suiteDone).toHaveBeenCalledWith(
jasmine.objectContaining({
fullName: 'throws after defining specs',
failedExpectations: [
jasmine.objectContaining({
message: jasmine.stringContaining('Error: nope')
})
]
})
);

expect(reporter.suiteDone).toHaveBeenCalledWith(
jasmine.objectContaining({
fullName: 'throws after defining specs',
failedExpectations: [
jasmine.objectContaining({
message: jasmine.stringContaining('Error: nope')
})
]
})
);
});

function browserEventMethods() {
return {
addEventListener() {},
Expand Down
13 changes: 8 additions & 5 deletions src/core/SuiteBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ getJasmineRequireObj().SuiteBuilder = function(j$) {
suite.exclude();
}
this.addSpecsToSuite_(suite, definitionFn);
if (suite.parentSuite && !suite.children.length) {
throw new Error(
`describe with no children (describe() or it()): ${suite.getFullName()}`
);
}
return suite;
}

Expand Down Expand Up @@ -183,11 +178,19 @@ getJasmineRequireObj().SuiteBuilder = function(j$) {
const parentSuite = this.currentDeclarationSuite_;
parentSuite.addChild(suite);
this.currentDeclarationSuite_ = suite;
let threw = false;

try {
definitionFn();
} catch (e) {
suite.handleException(e);
threw = true;
}

if (suite.parentSuite && !suite.children.length && !threw) {
throw new Error(
`describe with no children (describe() or it()): ${suite.getFullName()}`
);
}

this.currentDeclarationSuite_ = parentSuite;
Expand Down

0 comments on commit d402599

Please sign in to comment.