socketio / socket.io

Realtime application framework (Node.JS server)
https://socket.io
MIT License
61.11k stars 10.11k forks source link

ReferenceError: setImmediate is not defined while unittesting #4402

Closed MarcusElevait closed 1 week ago

MarcusElevait commented 2 years ago

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 :

Additional context Add any other context about the problem here.

darrachequesne commented 1 year ago

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

NextThread commented 1 year ago

Can I work on this

buddhikajay commented 2 months ago

I'm getting the same error with "socket.io-client": "^4.7.5". Any update on this ?

lounsbrough commented 1 month ago

This is still an issue for me as well.

darrachequesne commented 1 week ago

For future readers:

This should be fixed by https://github.com/socketio/socket.io/commit/93010ca3c4daf203c7b2c343158ab102f65aee9f, included in engine.io-client@6.6.0 and socket.io-client@4.8.0.