Skip to content

Commit 4b87107

Browse files
committed
Properly bind "this" for beforeEach/it/afterEach in mocha integration.
Fixes issue 7105.
1 parent e804d9b commit 4b87107

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

javascript/node/selenium-webdriver/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v2.41.0-dev
2+
3+
* FIXED: 7105: beforeEach/it/afterEach properly bind `this` for Mocha tests.
4+
15
## v2.40.0
26

37
* API documentation is now included in the docs directory.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2013 Selenium committers
2+
// Copyright 2013 Software Freedom Conservancy
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
'use strict';
17+
18+
var assert = require('assert');
19+
20+
var test = require('../../testing');
21+
22+
describe('Mocha Integration', function() {
23+
24+
describe('beforeEach properly binds "this"', function() {
25+
beforeEach(function() { this.x = 1; });
26+
test.beforeEach(function() { this.x = 2; });
27+
it('', function() { assert.equal(this.x, 2); });
28+
});
29+
30+
describe('afterEach properly binds "this"', function() {
31+
it('', function() { this.x = 1; });
32+
test.afterEach(function() { this.x = 2; });
33+
afterEach(function() { assert.equal(this.x, 2); });
34+
});
35+
36+
describe('it properly binds "this"', function() {
37+
beforeEach(function() { this.x = 1; });
38+
test.it('', function() { this.x = 2; });
39+
afterEach(function() { assert.equal(this.x, 2); });
40+
});
41+
});

javascript/node/selenium-webdriver/testing/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ function wrapped(globalFn) {
120120
function asyncTestFn(fn) {
121121
return function(done) {
122122
this.timeout(0);
123-
flow.execute(fn).then(seal(done), done);
123+
var timeout = this.timeout;
124+
this.timeout = undefined; // Do not let tests change the timeout.
125+
try {
126+
flow.execute(fn.bind(this)).then(seal(done), done);
127+
} finally {
128+
this.timeout = timeout;
129+
}
124130
};
125131
}
126132
}

0 commit comments

Comments
 (0)