I am having problems while performing a simple unit test in my NX workspace. What I am attempting to do is to 1) configure TestBed and 2) Inject service. However, even if I use scaffolded dummy service, the test still fails as the injected service is said to be undefined
test.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class TestService {
constructor() { }
}
test.service.spec.ts
import { TestBed } from '@angular/core/testing';
import { TestService } from './test.service';
describe('TestService', () => {
let service: TestService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ TestService ]
});
service = TestBed.inject(TestService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
Does anybody have an idea why is this happening? What exactly is the problem here?
Edit: Yes, I do know I can create a mock. However, the issue still seems strange to me so I would like to get to the bottom of it rather than finding a workaround.
Edit 2:
Using ng test <project> --test-file=<filename> directly yields the same results are using NXCLI to perform tests
Edit 3: I shared my code on StackBlitz as requested: https://stackblitz.com/edit/ng-unittest-issue