 |
Mocha test GitHub |
 |
Jasmine test homepage |
When you write tests for a new method, you probably just want to run relevant subset of tests instead of all the test cases. Let's see how to do it with Mocha and Jasmine
You can use only and skip to write exclusive and inclusive tests. With only, it will only run that block of tests. With skip, it will skip that block of tests.
import { assert, expect } from 'chai';
import { stub } from 'sinon';
import GeolocateService from '../src/geolocate';
describe.only('fn whereabout', () => {
let geolocateIpapiStub;
let geolocateFreeGeoIpStub;
const ipapiRes = 'US';
const freeGeoIpRes = { country_code: 'US' };
beforeEach(() => {
geolocateIpapiStub = stub(GeolocateService, 'geolocateIPAPI').resolves(ipapiRes)
geolocateFreeGeoIpStub = stub(GeolocateService, 'geolocateFreeGeoIp').resolves(freeGeoIpRes);
});
afterEach(() => {
geolocateIpapiStub.restore();
geolocateFreeGeoIpStub.restore();
});
function providerZeroTest(response) {
expect(geolocateIpapiStub.calledOnce).to.equal(true);
expect(geolocateFreeGeoIpStub.notCalled).to.equal(true);
expect(response).to.be.a('string');
expect(response).to.have.lengthOf(2);
expect(response).to.equal('US');
}
it.only('whereabout should call IPAPI when no provider specified', (done) => {
AmazonGeotargetService.whereabout()
.then((response) => {
providerZeroTest(response);
done();
})
.catch((err) => {
done(err);
});
});
it.skip('whereabout should call IPAPI when provider 0 specified', (done) => {
AmazonGeotargetService.whereabout({ provider: 0 })
.then((response) => {
providerZeroTest(response);
done();
})
.catch((err) => {
done(err);
});
});
});
In your spec files, you can add a prefix 'f' to run only that test or a prefix 'x' to skip that test like following:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MoviesComponent } from './movies.component';
import { DropDownListComponent } from '../../components/drop-down-list/drop-down-list.component';
import { SelectedDirective } from '../../directives/selected.directive';
import { AppComponent } from '../../app.component';
fdescribe('MoviesComponent', () => {
let component: MoviesComponent;
let fixture: ComponentFixture<MoviesComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MoviesComponent, DropDownListComponent, SelectedDirective],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MoviesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
xit('should create', () => {
expect(component).toBeTruthy();
});
fit(`should have as title 'Popular Movies'`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('Popular Movies');
}));
it('should render title in a h1 tag', async(() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('Popular Movies');
}));
});
Thanks for reading!
Jun
Comments
Post a Comment