Skip to content

Instantly share code, notes, and snippets.

View kylewhitaker's full-sized avatar

Kyle Whitaker kylewhitaker

View GitHub Profile
@kylewhitaker
kylewhitaker / some.component.spec.ts
Last active September 8, 2017 19:37
Example Unit Test: "Some Component: when someMethod is called and someCondition is true then something should happen"
describe('SomeComponent:', () => {
describe('when someMethod is called', () => {
describe('and someCondition is true', () => {
it('then something should happen', () => {
// expect: something to happen
});
});
});
});
@kylewhitaker
kylewhitaker / some-component.spec.ts
Last active September 8, 2017 19:38
Import only the thing you want to test. Do not import dependencies!
import { SomeComponent } from './some.component.js'; // only import the thing you want to test
describe('SomeComponent:', () => {
let component: SomeComponent;
let mockDepOne: any; // do not import DepOne, 'any' it
let mockDepTwo: any; // do not import DepTwo, 'any' it
let mockDepThree: any; // do not import DepThree, 'any' it
beforeEach(() => {
@kylewhitaker
kylewhitaker / some-component.spec.ts
Created September 8, 2017 19:34
Step #1 to writing a great unit test file is scaffolding!
describe('SomeComponent:', () => {
describe('someMethod', () => {
describe('true', () => {
it('should call depOne.doSomething', () => {
});
@kylewhitaker
kylewhitaker / some.component.spec.ts
Last active September 8, 2017 20:02
Example: Unit test failure due to undefined spy test double!
import { SomeComponent } from './some.component';
describe('SomeComponent:', () => {
let component: SomeComponent;
let mockDepOne: any;
beforeEach(() => {
mockDepOne = {
doSomething: () => '0123456789' // We correctly define a return value for our mock...
};
@kylewhitaker
kylewhitaker / some.component.spec.ts
Last active September 8, 2017 23:53
Harness the power of Jasmine's createSpy() and createSpyObj() functions to mock & spy all at once!
import { SomeComponent } from './some.component.js';
describe('SomeComponent:', () => {
let component: SomeComponent;
let mockDepOne: any;
let mockDepTwo: any;
beforeEach(() => {
mockDepOne = {
doSomething: jasmine.createSpy('doSomething').and.returnValue('0123456789')
@kylewhitaker
kylewhitaker / some.component.spec.ts
Created September 9, 2017 00:20
Unit testing an Observable with Jasmine's done() function
import { SomeComponent } from './some.component';
describe('SomeComponent:', () => {
let component: SomeComponent;
beforeEach(() => {
component = new SomeComponent();
});
describe('returnSomeObservable', () => {
@kylewhitaker
kylewhitaker / some.component.spec.ts
Last active September 9, 2017 00:34
Synchronously unit testing asynchronous functions can produce false positive passing tests!
import { SomeComponent } from './some.component';
describe('SomeComponent:', () => {
let component: SomeComponent;
beforeEach(() => {
component = new SomeComponent();
});
describe('returnSomeObservable', () => {
@kylewhitaker
kylewhitaker / some.component.spec.ts
Created September 11, 2017 15:24
Wrap your observable subscriptions inside a FakeAsync zone and make it Tick!
import { fakeAsync, tick } from '@angular/core/testing';
import { Observable } from 'rxjs/Observable';
import { SomeComponent } from './some.component';
describe('SomeComponent:', () => {
let component: SomeComponent;
let mockDepThree: any;
beforeEach(() => {

Cypress

Install

  • yarn add cypress --dev

Configuration

  • Typescript