Skip to content

Commit

Permalink
Call through for global before-* and after-* blocks (fixes joeljeske#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
ercultimate committed Apr 28, 2023
1 parent 4234197 commit d95ef74
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"plugin:security/recommended"
],
"globals": {
"FakeNews": true
"FakeNews": true,
"GlobalConditions": true,
"before": true,
"after": true
},
"plugins": [
"security"
Expand Down
3 changes: 3 additions & 0 deletions lib/karma-parallelizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ function createFakeTestContext(ctx, shouldUseDescription) {
// If its a fake description, then we need to call through to eval inner its() looking for focuses
// TODO, do we ever need parameters?
def();
} else if(depth === 0 && !isFocus && !isDescription && !isSpec) {
// If it is a global before-* or after-*, then we want to call through and return the result
return fn.apply(this, arguments);
}
};
}
Expand Down
12 changes: 12 additions & 0 deletions test/global-conditions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
window.GlobalConditions = {
beforeAllCount: 0,
beforeEachCount: 0,
afterEachCount: 0,
afterAllCount: 0,
testRunCount: 0,
beforeAll: function beforeAll() { this.beforeAllCount++; },
beforeEach: function beforeEach() { this.beforeEachCount++; },
afterEach: function afterEach() { this.afterEachCount++; },
afterAll: function afterAll() { this.afterAllCount++; },
testRun: function testRun() { this.testRunCount++; },
};
44 changes: 44 additions & 0 deletions test/set-global-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(before || beforeAll)(() => {
GlobalConditions.beforeAll();
});

beforeEach(() => {
GlobalConditions.beforeEach();
});

afterEach(() => {
GlobalConditions.afterEach();
});

(after || afterAll)(() => {
GlobalConditions.afterAll();
});



describe('set global conditions', function() {
describe('first subject', function() {
it('has a test', function() {
expect(GlobalConditions.testRun());
});
it('has another test', function() {
expect(GlobalConditions.testRun());
});
});
describe('second subject', function() {
it('has a test', function() {
expect(GlobalConditions.testRun());
});
it('has another test', function() {
expect(GlobalConditions.testRun());
});
});
describe('third subject', function() {
it('has a test', function() {
expect(GlobalConditions.testRun());
});
it('has another test', function() {
expect(GlobalConditions.testRun());
});
});
});

0 comments on commit d95ef74

Please sign in to comment.