You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library aims to reduce boilerplate π and provides high-level toolsοΈ π₯ for testing Component, Guard, Interceptor and everything else related to the Angular mechanism.
It makes tests easier to read π and faster to write β‘οΈ!
Quick examples
Testing Component
describe('AppComponent',()=>{consttb=componentTestBed(AppComponent)// π οΈ Create the test bed which is re-compiled for each test.inject('prefs',Preferences);// ποΈ Link a key to an injection for all tests, see below πit('should render title',tb(({ component, query })=>{// π Access enhanced tools for testing components expect(component.title).toEqual('app-v17');constspan=query.findElement('.content span');expect(span.textContent).toContain('app-v17 app is running!');}));it('should update preferences on click',tb(({ action,injected: { prefs }})=>{// π€― Retrieve injections by autocompletionexpect(prefs.approved).toBeFalse();action.click('#my-button');expect(prefs.approved).toBeTrue();}));});
π«‘ (The redundant "should create" test is even called up for you!)
Testing Service
describe('AppService',()=>{consttb=serviceTestBed(AppService,{httpTesting: true});// π οΈ Create the test bed and enable http testingit('should fetch cat fact',tb(({ service, http, rx },done)=>{constmockRes={fact: 'string',length: 6};rx.remind=service.getCatFact().subscribe({// π§― Use rx.remind to auto unsubscribe after the end of the testnext: (res)=>{expect(res).toEqual(mockRes);done();},});http.emitSuccessResponse({url: service.CAT_FACT_URL,body: mockRes});// π Fake the http response of the request that matches the url}));});