I'm in need of some assistance. I am currently working with Nx along with the latest versions of Angular/NestJS (February 26)
...
"@nestjs/common": "^7.0.0",
"@nestjs/config": "^0.6.3",
"@nestjs/core": "^7.0.0",
"@nestjs/platform-express": "^7.0.0",
"@nestjs/platform-socket.io": "^7.6.7",
"@nestjs/websockets": "^7.6.7",
"jest": "26.2.2",
"@nrwl/jest": "11.4.0",
...
I'm encountering issues running unit tests using NestJS and Jest I specifically want to test the following service:
@Injectable()
export class CoreApiService {
logger = new Logger('CoreApiService');
apiEndpoint;
constructor(private httpService: HttpService, configService: ConfigService) {
this.apiEndpoint = configService.get('API_SERVICE_ENDPOINT');
}
}
However, I keep getting this error:
TypeError: Cannot read property 'get' of undefined
It appears that both ConfigService and HttpService are always undefined.
Even when attempting to create new instances like
new CoreApiService(new HttpService(), new ConfigService())
I've also tried variations such as new CoreApiService({} as any, {get: (...params} => {return 'foo'})
within the test itself
The error persists despite these efforts.
Here's the test file:
import { Test, TestingModule } from '@nestjs/testing';
import { CoreApiService } from './core-api.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { HttpModule } from '@nestjs/common';
class ConfigServiceMock {
get(key: string): string {
switch (key) {
case 'API_SERVICE_ENDPOINT':
return '';
}
}
}
describe('CoreApiService', () => {
let module: TestingModule;
let service: CoreApiService;
beforeEach(async () => {
module = await Test.createTestingModule({
imports: [HttpModule, ConfigModule],
providers: [
CoreApiService,
{ provide: ConfigService, useClass: ConfigServiceMock },
],
}).compile();
service = module.get<CoreApiService>(CoreApiService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
I've even attempted:
.overrideProvider(ConfigService).useClass(ConfigServiceMock)
Thank you for your help!
Edit 03/01
It seems that the module compilation step is failing... hence, the log "COMPILED" will not be executed in the current test file.
CoreApiService › should be defined
TypeError: Cannot read property 'get' of undefined
22 |
23 | constructor(private httpService: HttpService, configService: ConfigService) {
> 24 | this.apiEndpoint = configService.get('API_SERVICE_ENDPOINT');
| ^
25 | }
26 |
27 | private static createRequestConfig(options: RequestHeader): RequestConfig {
at new CoreApiService (src/app/core-api/core-api.service.ts:24:38)
at Injector.instantiateClass (../../node_modules/@nestjs/core/injector/injector.js:286:19)
at callback (../../node_modules/@nestjs/core/injector/injector.js:42:41)
at Injector.resolveConstructorParams (../../node_modules/@nestjs/core/injector/injector.js:114:24)
at Injector.loadInstance (../../node_modules/@nestjs/core/injector/injector.js:46:9)
at Injector.loadProvider (../../node_modules/@nestjs/core/injector/injector.js:68:9)
at async Promise.all (index 5)
at InstanceLoader.createInstancesOfProviders (../../node_modules/@nestjs/core/injector/instance-loader.js:43:9)
at ../../node_modules/@nestjs/core/injector/instance-loader.js:28:13
at async Promise.all (index 1)
at InstanceLoader.createInstances (../../node_modules/@nestjs/core/injector/instance-loader.js:27:9)
at InstanceLoader.createInstancesOfDependencies (../../node_modules/@nestjs/core/injector/instance-loader.js:17:9)
at TestingModuleBuilder.compile (../../node_modules/@nestjs/testing/testing-module.builder.js:43:9)
● CoreApiService › should be defined
expect(received).toBeDefined()
Received: undefined
44 | it('should be defined', () => {
45 | console.log('SHOULD BE DEFINED')
> 46 | expect(service).toBeDefined();
| ^
47 | });
48 | });
49 |
at Object.<anonymous> (src/app/core-api/core-api.service.spec.ts:46:21)
The current test file looks like this:
import { Test, TestingModule } from '@nestjs/testing';
import { CoreApiService } from './core-api.service';
import { ConfigService } from '@nestjs/config';
import { HttpService, INestApplication } from '@nestjs/common';
class ConfigServiceMock {
get(key: string): string {
switch (key) {
case 'API_SERVICE_ENDPOINT':
return '';
}
}
}
class HttpServiceMock {
get(): any {
}
}
describe('CoreApiService', () => {
let module: TestingModule;
let service: CoreApiService;
let app: INestApplication;
beforeEach(async () => {
console.log('beforeEach')
module = await Test.createTestingModule({
imports: [],
providers: [
{ provide: ConfigService, useClass: ConfigServiceMock },
{ provide: HttpService, useClass: HttpServiceMock },
CoreApiService,
],
})
.compile();
console.log('COMPILED');
app = module.createNestApplication();
await app.init();
service = module.get<CoreApiService>(CoreApiService);
});
it('should be defined', () => {
console.log('SHOULD BE DEFINED')
expect(service).toBeDefined();
});
});
I have also experimented with changing the order of the provider section, but it doesn't seem to make a difference...