I'm running tests on angular and they are failing for my socket service thats using ngx-socket-io. It works in development, just not for testing. Its not creating the component correctly.
my service.spec.ts
import { TestBed } from '@angular/core/testing';
import { SocketService } from './socket.service';
describe('SocketService', () => {
let service: SocketService;
beforeEach(() => {
TestBed.configureTestingModule({
});
service = TestBed.inject(SocketService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
my service
import { Injectable } from '@angular/core';
import {Socket} from 'ngx-socket-io';
@Injectable({
providedIn: 'root'
})
export class SocketService {
constructor(private socket: Socket) { }
getD1Data() {
return this.createObserver('d1');
}
private createObserver(event: string) {
return this.socket.fromEvent(event);
}
I have tried adding the provider [WrappedSocket] I'll be honest that was an auto fix option, but that caused way more errors.
import {WrappedSocket} from 'ngx-socket-io/src/socket-io.service';
describe('SocketService', () => {
let service: SocketService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [WrappedSocket]
});
service = TestBed.inject(SocketService);
});
Error: ./src/app/modules/socket.service.spec.ts
Module not found: Error: Can't resolve 'ngx-socket-io/src/socket-io.service' in 'C:\Users\catha\project - Copy\src\app\modules'
I'm actually not sure how it couldn't exist, considering it was an autofill.
Any ideas on how to fix this? I've tried a range of providers [Socket] , [SocketIOModule] but nothing seems to work
We have to configure the SocketIOModule using the object
config
of typeSocketIoConfig
.Hopefully this helps!