socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

ReferenceError: setImmediate is not defined while unittesting

Open MarcusElevait opened this issue 3 years ago • 4 comments

Describe the bug We have an nx project where we have a library that holds all of our angular services. One of these services is called SocketService and is managing socket-io socket instantiation. When we run our unittests for this library and the tests are running one after another, we get the following error: ReferenceError: setImmediate is not defined at XMLHttpRequest.dispatchEvent (/home/marcus/projects/aicosy/node_modules/xmlhttprequest-ssl/lib/XMLHttpRequest.js:628:9) at setState (/home/marcus/projects/aicosy/node_modules/xmlhttprequest-ssl/lib/XMLHttpRequest.js:654:12) at XMLHttpRequest.handleError (/home/marcus/projects/aicosy/node_modules/xmlhttprequest-ssl/lib/XMLHttpRequest.js:572:5) at ClientRequest.errorHandler (/home/marcus/projects/aicosy/node_modules/xmlhttprequest-ssl/lib/XMLHttpRequest.js:490:14) at ClientRequest.emit (node:events:520:28) at Socket.socketErrorListener (node:_http_client:442:9) at Socket.emit (node:events:520:28) at emitErrorNT (node:internal/streams/destroy:157:8) at emitErrorCloseNT (node:internal/streams/destroy:122:3) at processTicksAndRejections (node:internal/process/task_queues:83:21) This error occurs on the unittest of the service, that is directly running after the SocketService tests. (No matter which test this is) When I'm skipping the SocketService test, the unittests are succeeding.

To Reproduce Here is the code for our SocketService:

@Injectable({
    providedIn: 'root',
})
export class SocketService {
    socket = io(this.backendService.getActiveHost(), {
        path: this.backendService.getWsUrlSubpath() + '/socket.io',
        auth: {
            token: this.authService.getAccessToken() as string,
        },
        closeOnBeforeunload: false,
    });

    constructor(private backendService: BackendService, private authService: AuthService) {
    }
}

and here is the test:

describe('SocketService', () => {
    let service: SocketService;
    let authService: AuthService;

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                {
                    provide: BackendService,
                    useClass: BackendServiceStub,
                },
                {
                    provide: AuthService,
                    useClass: AuthServiceStub,
                },
            ],
        });
        service = TestBed.inject(SocketService);
        authService = TestBed.inject(AuthService);
    });

    it('should be created', () => {
        expect(service).toBeTruthy();
    });
});

Socket.IO client version: 4.5.1

Expected behavior I expect the error not to be thrown.

Platform: Running with :

  • angular: 13.3.2
  • jest: 27.5.1

Additional context Add any other context about the problem here.

MarcusElevait avatar Jun 23 '22 16:06 MarcusElevait

That's really weird, setImmediate is used in the xmlhttprequest-ssl package, but it should not be included in the browser build (since the XMLHttpRequest is already provided).

Reference: https://github.com/socketio/engine.io-client/blob/01804d59c3351f1c87d62ca0c9f942792f4f52e5/package.json#L91

Might be related: https://stackoverflow.com/questions/68708955/jest-test-error-browsertype-launch-setimmediate-is-not-defined-while-using-pl

darrachequesne avatar Jun 21 '23 12:06 darrachequesne

Can I work on this

NextThread avatar Aug 29 '23 06:08 NextThread