this.timeout fails inside describe block
jamesopti opened this issue · comments
Opening new issue as requested:
-
**I'm submitting a ... **
- bug report
-
What is the current behavior?
Context not enforced insidedescribe
blocks
this
is incorrect -
What is the expected behavior?
-
What is the motivation / use case for changing the behavior?
this.timeout(<ms>)
does not work -
Please tell us about your environment:
- version: 2.0.0-beta.X
- Browser: [Chrome 57]
- Language: [all]
We commonly call this.timeout
inside the beginning of the root describe
in a test file:
describe('some_module/some_component', function() {
this.timeout(10000);
// Setup and run some tests
...
});
Here is an example output:
➜ myapp git:(karma_parallel) grunt test:myapp:unit
Running "test:myapp:unit" (test) task
Running "karma:myapp:unit" (karma) task
START:
Hash: e0bdc8cc97632b01d813
Version: webpack 2.4.1
Time: 44ms
webpack: Compiled successfully.
webpack: Compiling...
Hash: e0fab60a728ee52c6d84
Version: webpack 2.4.1
Time: 21798ms
Asset Size Chunks Chunk Names
0.bundle.js 273 kB 0 [emitted] [big] extras_bundle
1.bundle.js 110 kB 1 [emitted] stub_data_bundle
firstparty.js 6.97 MB 2 [emitted] [big] firstparty
src/js/tests/myapp/main.js 1.79 MB 3 [emitted] [big] src/js/tests/myapp/main.js
vendor.js 8.22 MB 4 [emitted] [big] vendor
webpack: Compiled with warnings.
Chrome 63.0.3239 (Mac OS X 10.12.6) ERROR
Uncaught TypeError: this.timeout is not a function
at src/js/tests/myapp/main.js:287
Chrome 63.0.3239 (Mac OS X 10.12.6) ERROR
Uncaught TypeError: this.timeout is not a function
at src/js/tests/myapp/main.js:356
Finished in 2.413 secs / 0 secs
SUMMARY:
✔ 0 tests completed
Warning: Task "karma:myapp:unit" failed. Use --force to continue.
Aborted due to warnings.
And the relevant portion of the karma config:
frameworks: ['parallel', 'mocha-debug', 'mocha'],
parallelOptions: {
executors: 2, // Defaults to cpu-count - 1
shardStrategy: 'round-robin',
},
Thank you for this bug report. This is occurring because this framework must call all the describe blocks in every instance, even those that contain it()s that will only be run in another instance. It is difficult and error-prone to fake the entire executing API of this
for each describe.
As opposed to mocking the API, it might be easiest to put the this.timeout() in a before()
or beforeAll()
. Those will only be called in the instances that will actually run each test spec.
Example:
Instead of
describe('some_module/some_component', function() {
this.timeout(10000);
// Setup and run some tests
...
});
Try this
describe('some_module/some_component', function() {
before(function() { // Runs once
this.timeout(10000);
});
// Setup and run some tests
...
});
This approach works! I think if this is the only non-standard thing we have to do that I can get onboard with this approach.
I'm hesitant to deviate too much from following the mocha docs, but this exception alone would be worth it.
Coming across another issue that I'll open a separate ticket for.
For me, this
is accessible and set timeout works in the before
statement but it's reset in the it
statement. As a workaround,
describe("component", () => {
it("description", function(done) {
this.timeout(10000); // place the timeout here make it works as expected.
...
});
});
FYI, the code was as follow
describe("component", () => {
it("description", (done) => {
...
}).timeout(10000); // was set timeout here.
});