Karma Disconnected, because no message in 10000 ms
Asked Answered
H

7

19

The Karma test suite fails with the message:

Disconnected, because no message in 10000 ms.

No tests are executed at all.

"@angular/core": "7.1.3",
"jasmine-core": "3.3.0",
"karma-jasmine": "1.1.2",

There is no apparent reason for the failure, it just started after a new test was introduced.

Helm answered 18/2, 2019 at 9:58 Comment(0)
H
6

When the --module compiler option for TypeScript in tsconfig.spec.json is set to commonjs Karma fails internally before any tests are executed and shows the timeout error above.

The import ordering can let Karma fail:

import CustomerTypeEnum = CustomerDto.CustomerTypeEnum;
import {CustomerDto} from '../api/CustomerDto';

While this order works as expected:

import {CustomerDto} from '../api/CustomerDto';
import CustomerTypeEnum = CustomerDto.CustomerTypeEnum;

The problem can also be fixed by changing the module compiler option to e.g. es2015.

Helm answered 18/2, 2019 at 9:58 Comment(5)
No. It isn't a bug at all. import CustomerTypeEnum = CustomerDto.CustomerTypeEnum; is not an import at all. It is a syntax for aliasing a member of a TypeScript namespace. Such namespaces can exist in both the value space and the types space. Since CustomerTypeEnum is a value as well as a type, a variable assignment is emitted by the compiler with an initializer referencing a property of an object it isn't defined yet. This depends to some extent on your output module format. Look at the actual compiled code and you'll see exactly what is happening and why.Sekofski
The TypeScript compiler should probably flag this as an error, but it will only fail in certain module formats.Sekofski
Thanks for this input, I adjusted my answer based on your input. I agree that the TypeScript compiler should flag this as an error or at least give a warning.Helm
It's actually not related to --moduleResolution or --target but to --module. But the possible difference based on that flag is really a side issue. Ultimately, what you're doing is using the value before it is available, and the compiler should warn you, but the order that works is also the logical order - imports should be lexically before their usage if for no other reason than clarity.Sekofski
Changed the answer again: Actually changing --module from commonjs to es2015 worked in my codebase and is definately the nicer solution.Helm
R
27

I had the same problem and tried everything - nothing works except adding this option to my karma.conf.js:

browserNoActivityTimeout: 400000
Recondite answered 13/5, 2019 at 7:58 Comment(2)
Hopefully not a silly question but my recently-created AngularCLI app lacks karma.conf.js entirely, but I'm having a similar problem. Did something change about the boilerplate setup?Angeli
@Angeli I think that means you are just using the karma defaults. You can create such a file if you need to override that value. BTW that change did it for me. Actually I went whole hog and used the multiple setting values provided in Rafał Polak's answer below.Vala
H
6

When the --module compiler option for TypeScript in tsconfig.spec.json is set to commonjs Karma fails internally before any tests are executed and shows the timeout error above.

The import ordering can let Karma fail:

import CustomerTypeEnum = CustomerDto.CustomerTypeEnum;
import {CustomerDto} from '../api/CustomerDto';

While this order works as expected:

import {CustomerDto} from '../api/CustomerDto';
import CustomerTypeEnum = CustomerDto.CustomerTypeEnum;

The problem can also be fixed by changing the module compiler option to e.g. es2015.

Helm answered 18/2, 2019 at 9:58 Comment(5)
No. It isn't a bug at all. import CustomerTypeEnum = CustomerDto.CustomerTypeEnum; is not an import at all. It is a syntax for aliasing a member of a TypeScript namespace. Such namespaces can exist in both the value space and the types space. Since CustomerTypeEnum is a value as well as a type, a variable assignment is emitted by the compiler with an initializer referencing a property of an object it isn't defined yet. This depends to some extent on your output module format. Look at the actual compiled code and you'll see exactly what is happening and why.Sekofski
The TypeScript compiler should probably flag this as an error, but it will only fail in certain module formats.Sekofski
Thanks for this input, I adjusted my answer based on your input. I agree that the TypeScript compiler should flag this as an error or at least give a warning.Helm
It's actually not related to --moduleResolution or --target but to --module. But the possible difference based on that flag is really a side issue. Ultimately, what you're doing is using the value before it is available, and the compiler should warn you, but the order that works is also the logical order - imports should be lexically before their usage if for no other reason than clarity.Sekofski
Changed the answer again: Actually changing --module from commonjs to es2015 worked in my codebase and is definately the nicer solution.Helm
D
6

I had a similar problem on Chrome 85.0.4183. I don't know why Karma lose connection with browser and I get "Disconnected, because of no message in 30000 ms."

I've added this to Karma.conf:

captureTimeout: 210000,
browserDisconnectTolerance: 3, 
browserDisconnectTimeout : 210000,
browserNoActivityTimeout : 210000

now it works, hope this will help you

Distrust answered 10/9, 2020 at 10:1 Comment(0)
U
2

It failed for me because I was setting window.location.href in my component, but the test run just hung at random times rather than failing in the test for my component.

Unapproachable answered 28/9, 2020 at 1:39 Comment(0)
G
1

Check the log of karma, when there is a compilation errors in test files, the karma server return as a timeout error and not the real error

Gustaf answered 22/2, 2021 at 16:12 Comment(0)
K
0

In my case after updating basic angular libraries chrome driver started to work properly.

"devDependencies": {
        "@angular-devkit/build-angular": "15.1.1", #was
        "@angular-devkit/build-angular": "^15.2.10", #became
        "@angular/cli": "15.1.1",   #was
        "@angular/cli": "^15.2.10", #became

Kevon answered 3/11, 2023 at 19:44 Comment(0)
S
-4

You can add this where you have need greater time than jasmine default time.

  beforeEach(async(() => {
    jasmine.DEFAULT_TIMEOUT_INTERVAL = <whatever time your test need to complete>;
  }));

and also you can check this answer if this not working. https://mcmap.net/q/673100/-no-message-in-10000-ms-jasmine-karma-requirejss

Sow answered 18/2, 2019 at 11:22 Comment(3)
Tried that already, didn't work in my case. It failed during karma initialization (see my answer below).Helm
Yes I have, it's definately not a timing issue. It took me a whole day to figure it out because Karma did not report the compiler issue. Changing the commonjs module compilerOption to es2015 resolves the problem.Helm
I don't think so its DEFAULT_TIMEOUT_INTERVAL issue. Because sometimes I am seeing even though my test cases have not even started running and after just karma launcher I am seeing Disconnected issue.Juvenescent

© 2022 - 2024 — McMap. All rights reserved.