🐞 Testing - deependhamecha/angular GitHub Wiki
-
karma makes a web server that will run tests if file changes.
-
protractor.js is for end-to-end tests
-
Jasmine is the testing library
-
In
/src
there is a file called test.ts which has configuration of karma and our tests. -
Fixture
is the testing Component.DebugElement
is the html element. -
configureTestingModule
is similar toNgModule
where we have declarations. -
You write
configureTestingModule().compileComponents()
, if you want to compile the HTML, CSS. -
You can also write before and after fixture.detectChanges() to run test after changes.
Check Jasmine documentation
// Check Component expect(component).toBeTruthy() -> true (boolean value)
// Check content inside String expect(component.variableName).toContain('deepen');
// Check content is string expect(component.variableName).toBe('warn');
// Check greater than expect(component.variableName).toBeGreaterThan(2);
// Check element exist or not expect(debug.query(By.css('h1'))); expect(debug.query(By.css('.dude'))); expect(debug.query(By.css('#okay')));
// Check text iniside element is same expect(debug.query(By.css('h1')).nativeElement.innerText).toBe('Deepen Dhamecha');
expect(component.hideContent).toBeTruthy();
// Call component's method component.toggle() expect(component.hideContent).toBeFalsy();
Above cannot check for async tasks
it('should be bla bla', fakeAsync(() => { expect(component.hideContent).toBeTruthy(); component.methodName(); tick(500); // Wait for 500 sec, for setTimeout mentioned in the methodName() expect(component.hideContent).toBeFalsy(); }));