Scaffolding for existing .ts files can be created by using https://github.com/smnbbrv/ngx-spec
To install in an Angular project:
npm i -D ngx-spec@^2.0.0
(-D is the shorthand for --save-dev)
example usage (for a service):
ng g ngx-spec:spec path/my.service
or
ng g ngx-spec:spec path/my.service.ts
For a service, this doesn’t set up a test to be created via injection. Adapt so that the test looks something like this:
import { TestBed, inject } from '@angular/core/testing';
import { DataService } from './data.service';
import { AuthService } from './auth.service';
import { HttpClient, HttpHandler } from '@angular/common/http';
describe('DataService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [DataService, AuthService, HttpClient, HttpHandler
]
});
});
it('should be created', inject([DataService, AuthService], (service: DataService) => {
expect(service).toBeTruthy();
}));
});
It's also meant to be possible to generate tests using a wildcard, e.g.
ng g ngx-specs '**/*
That didn't work for me - see the GitHub issue:
https://github.com/smnbbrv/ngx-spec/issues/10
Note - As a strategy to implement test-driven development, I found it easiest to search for and remove all existing *.spec.ts
files that had automatically been created in the Angular project as part of initial artifact creation (by searching in Windows explorer), then as a starting point I created a single test for the main Angular data provider service, using ngx-spec