Sherif eldeeb
Sherif eldeeb

Reputation: 2186

testing an angular service without an application

I have only one .ts file (not angular project) containing a single service

it is just one file, not a complete angular project, and it is even not an angular library (doesn't contain angular.json at all)

I can import this service to any angular application and it works fine. I want to add a test file beside it, and then run this test via jest as a single file, i.e not inside an angular application

I use TestBed but I always got getComponentFromError

how can I run the test without a complete angular project (app or lib)

Upvotes: 0

Views: 229

Answers (1)

IAfanasov
IAfanasov

Reputation: 4993

TestBed would ensure the usage of Angular dependency injection and ngZone. If you don't need those for the service under the test, you can instantiate the service using the constructor and proceed as with any other Angular tests.

const sut: MyClass;
beforeEach(() => {
   sut = new MyClass();
})
if(`kill all humans when time has come`, () => {
   const doomsDay = new Date(year, month, day);
   sut.killAllHumanWhenTimeHasCome(doomsDay);
})

If you also don't have a test runner and assertion engine, you might just add jasmine as it is described in the getting started instruction.

Upvotes: 1

Related Questions