Skip to content

Instantly share code, notes, and snippets.

@linuxsimba
Last active February 25, 2021 08:51
Show Gist options
  • Save linuxsimba/27324f7c3c5c267f124e703a9990f446 to your computer and use it in GitHub Desktop.
Save linuxsimba/27324f7c3c5c267f124e703a9990f446 to your computer and use it in GitHub Desktop.
NG10/NG7 unit testing component unit test example
import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { customNsTestBedAfterEach, customNsTestBedBeforeEach } from './setup';
@Component({
selector: 'test-comp',
template: '' // template is blank, because code does not have NO_ERRORS_SCHEMA and also we don't care. only testing functions in component
})
class TestComponent{
hello(): string { return 'hello'; }
}
let component: TestComponent;
describe('Test Component', () => {
beforeEach(customNsTestBedBeforeEach(
[],
[ TestComponent ],
[]
));
afterEach(() => customNsTestBedAfterEach());
describe('Function Tests -', () => {
beforeEach(() => {
component = TestBed.inject(TestComponent);
});
describe('hello()', () => {
it('should say "hello"', () => {
expect(component.hello()).toEqual('hello');
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment