
Security News
High Salaries No Longer Enough to Attract Top Cybersecurity Talent
A survey of 500 cybersecurity pros reveals high pay isn't enough—lack of growth and flexibility is driving attrition and risking organizational security.
shallow-render
Advanced tools
Angular testing made easy with shallow rendering and easy mocking.
Hey all, I do plan on continuing to update this library as Angular provides updates. This is not easy for me to do as I don't work with Angular in my current job.
Any additonal support would be very appreciated! Please reach out if you want to be a help!
Angular | shallow-render |
---|---|
19x | 19x |
18x | 18x |
17x | 17x |
16x | 16x |
15x | 15x |
14x | 14x |
13x | 13x |
12x | 12x |
11x | 11x |
10x | 10x |
9x | 9x |
6x-8x | 8x |
5x | <= 7.2.0 |
describe('ColorLinkComponent', () => {
let shallow: Shallow<ColorLinkComponent>;
beforeEach(() => {
shallow = new Shallow(ColorLinkComponent, MyModule);
});
it('renders a link with the name of the color', async () => {
const { find } = await shallow.render({ bind: { color: 'Blue' } });
// or shallow.render(`<color-link color="Blue"></color-link>`);
expect(find('a').nativeElement.textContent).toBe('Blue');
});
it('emits color when clicked', async () => {
const { element, outputs } = await shallow.render({ bind: { color: 'Red' } });
element.click();
expect(outputs.handleClick.emit).toHaveBeenCalledWith('Red');
});
});
Testing in Angular is HARD. TestBed is powerful but its use in component specs ends with lots of duplication.
Here's a standard TestBed spec for a component that uses a few other components, a directive and a pipe and handles click events:
describe('MyComponent', () => {
beforeEach(async => {
return TestBed.configureTestModule({
imports: [SomeModuleWithDependencies],
declarations: [
TestHostComponent,
MyComponent, // <-- All I want to do is test this!!
// We either must list all our dependencies here
// -- OR --
// Use NO_ERRORS_SCHEMA which allows any HTML to be used
// even if it is invalid!
ButtonComponent,
LinkComponent,
FooDirective,
BarPipe,
],
providers: [MyService],
})
.compileComponents()
.then(() => {
let myService = TestBed.get(MyService); // Not type safe
spyOn(myService, 'foo').and.returnValue('mocked foo');
});
});
it('renders a link with the provided label text', () => {
const fixture = TestBed.createComponent(TestHostComponent);
fixture.componentInstance.labelText = 'my text';
fixture.detectChanges();
const link = fixture.debugElement.query(By.css('a'));
expect(a.nativeElement.textContent).toBe('my text');
});
it('sends "foo" to bound click events', () => {
const fixture = TestBed.createComponent(TestHostComponent);
spyOn(fixture.componentInstance, 'handleClick');
fixture.detectChanges();
const myComponentElement = fixture.debugElement.query(By.directive(MyComponent));
myComponentElement.click();
expect(fixture.componentInstance.handleClick).toHaveBeenCalledWith('foo');
});
});
@Component({
template: '<my-component [linkText]="linkText" (click)="handleClick($event)"></my-component>',
})
class TestHostComponent {
linkLabel: string;
handleClick() {}
}
Whew!!! That was a lot of boilerplate. Here's just some of the issues:
NgModule
I've probably already added MyComponent
too. Total module duplication.TestBed
module.TestHostComponent
so I could pass bindings into my actual component.TestBed
boilerplate code-length exceeded my actual test code-length.We should mock everything we can except for the component in test and that should be EASY. Our modules already define the environment in which our components live. They should be reused, not rebuilt in our specs.
Here's the same specs using shallow-render
:
describe('MyComponent', () => {
let shallow: Shallow<MyComponent>;
beforeEach(() => {
shallow = new Shallow(MyComponent, MyModule);
});
it('renders a link with the provided label text', async () => {
const { find } = await shallow.render({ bind: { linkText: 'my text' } });
// or shallow.render(`<my-component linkText="my text"></my-component>`);
expect(find('a').nativeElement.textContent).toBe('my text');
});
it('sends "foo" to bound click events', async () => {
const { element, outputs } = await shallow.render();
element.click();
expect(outputs.handleClick).toHaveBeenCalledWith('foo');
});
});
Here's the difference:
MyModule
contains your component and all its dependencies.MyModule
are mocked. This is what makes the rendering "shallow".FAQs
Shallow rendering test utility for Angular
We found that shallow-render demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A survey of 500 cybersecurity pros reveals high pay isn't enough—lack of growth and flexibility is driving attrition and risking organizational security.
Product
Socket, the leader in open source security, is now available on Google Cloud Marketplace for simplified procurement and enhanced protection against supply chain attacks.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.