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 c62b259
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
"commonjs": true,
"es6": true,
"node": true,
"jasmine": true
"jasmine": true,
"mocha": true
},
"extends": [
"eslint:recommended",
"plugin:security/recommended"
],
"globals": {
"FakeNews": true
"FakeNews": true,
"GlobalConditions": 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 c62b259

Please sign in to comment.