Permalink
Browse files

chore(tests): replaced Nock with AxiosMockAdapter, which seems to wor…

…k better

affects: @serenity-js/core, @serenity-js/rest
  • Loading branch information...
jan-molak committed Jan 10, 2018
1 parent 6ea298d commit fe7e4c84aaebbadaaac1601aaec98c095442bf7a
@@ -2,12 +2,10 @@ import chai = require('chai');
import sinonChai = require('sinon-chai');
import chaiAsPromised = require('chai-as-promised');
import chaiSmoothie = require('chai-smoothie');
import { RecordedActivity, SourceLocation } from '../src/domain/model';
chai.use(sinonChai);
chai.use(chaiAsPromised);
chai.use(chaiSmoothie);
declare global {
namespace Chai { // tslint:disable-line:no-namespace
@@ -30,7 +30,7 @@
},
"devDependencies": {
"@types/axios": "0.14.0",
"nock": "9.1.4"
"axios-mock-adapter": "1.10.0"
},
"dependencies": {
"@serenity-js/core": "^1.5.4",
@@ -1,21 +1,30 @@
import nock = require('nock');
import 'mocha';
import {CallAnApi} from '../../../src/screenplay/abilities';
import {expect} from '../../expect';
import axios, { AxiosInstance } from 'axios';
import AxiosMockAdapter = require('axios-mock-adapter');
import { CallAnApi } from '../../../src/screenplay/abilities';
import { expect } from '../../expect';
describe('Abilities', () => {
const baseUrl = 'https://dum.my';
const baseURL = 'https://dum.my';
describe('Call an Api', () => {
const axiosInstance: AxiosInstance = axios.create({baseURL});
const dummyApi = new AxiosMockAdapter(axiosInstance);
describe('Call an Api at baseUrl', () => {
const callAnApi = CallAnApi.at(baseUrl);
const callAnApi = CallAnApi.using(axiosInstance);
describe('performs a get on a resource and get 200', () => {
after(() => dummyApi.restore());
describe('performs a get on a resource and gets 200', () => {
const resource = '/get200';
const dummyRest = nock(baseUrl)
.get(resource)
dummyApi
.onGet(resource)
.reply(200, {
username: '1337',
});
@@ -27,55 +36,56 @@ describe('Abilities', () => {
it('and LastResponse has data', () => callAnApi.getLastResponse().then(response => expect(response.data).to.not.be.empty));
});
describe('performs a get on a resource and get 404', () => {
describe('performs a get on a resource and gets 404', () => {
const resource = '/get404';
const dummyRest = nock(baseUrl)
.get(resource)
dummyApi
.onGet(resource)
.reply(404);
before(() => expect(callAnApi.get(resource)).to.eventually.be.fulfilled);
it('and eventually has a LastResponse', () => expect(callAnApi.getLastResponse()).to.eventually.not.be.undefined);
it('and LastResponse status is 404', () => callAnApi.getLastResponse().then(response => expect(response.status).to.equal(404)));
it('and LastResponse has no data', () => callAnApi.getLastResponse().then(response => expect(response.data).to.be.empty));
});
describe('performs a post on a resource and get 201', () => {
describe('performs a post on a resource and gets 201', () => {
const resource = '/post201';
const dummyRest = nock(baseUrl)
.post(resource)
.reply(201, {
dummyApi
.onPost(resource, {
username: '1337',
});
})
.reply(201);
before(() => expect(callAnApi.post(resource, {username: '1337'})).to.eventually.be.fulfilled);
it('and eventually has a LastResponse', () => expect(callAnApi.getLastResponse()).to.eventually.not.be.undefined);
it('and LastResponse status is 404', () => callAnApi.getLastResponse().then(response => expect(response.status).to.equal(201)));
it('and LastResponse has no data', () => callAnApi.getLastResponse().then(response => expect(response.data).to.not.be.empty));
it('and LastResponse status is 201', () => callAnApi.getLastResponse().then(response => expect(response.status).to.equal(201)));
it('and LastResponse has no data', () => callAnApi.getLastResponse().then(response => expect(response.data).to.be.empty));
});
describe('performs a post on a resource and get 403', () => {
describe('performs a post on a resource and gets 403', () => {
const resource = '/post403';
const dummyRest = nock(baseUrl)
.post(resource)
dummyApi
.onPost(resource)
.reply(403);
before(() => expect(callAnApi.post(resource, {username: '1337'})).to.eventually.be.fulfilled);
it('and eventually has a LastResponse', () => expect(callAnApi.getLastResponse()).to.eventually.not.be.undefined);
it('and LastResponse status is 404', () => callAnApi.getLastResponse().then(response => expect(response.status).to.equal(403)));
it('and LastResponse status is 403', () => callAnApi.getLastResponse().then(response => expect(response.status).to.equal(403)));
it('and LastResponse has no data', () => callAnApi.getLastResponse().then(response => expect(response.data).to.be.empty));
});
describe('performs a delete on a resource and get 200', () => {
describe('performs a delete on a resource and gets 200', () => {
const resource = '/delete200';
const dummyRest = nock(baseUrl)
.delete(resource)
dummyApi
.onDelete(resource)
.reply(200, {
username: '1337',
});
@@ -87,25 +97,26 @@ describe('Abilities', () => {
it('and LastResponse has data', () => callAnApi.getLastResponse().then(response => expect(response.data).to.not.be.empty));
});
describe('performs a delete on a resource and get 404', () => {
describe('performs a delete on a resource and gets 404', () => {
const resource = '/delete404';
const dummyRest = nock(baseUrl)
.delete(resource)
dummyApi
.onDelete(resource)
.reply(404);
before(() => expect(callAnApi.delete(resource)).to.eventually.be.fulfilled);
it('and eventually has a LastResponse', () => expect(callAnApi.getLastResponse()).to.eventually.not.be.undefined);
it('and LastResponse status is 404', () => callAnApi.getLastResponse().then(response => expect(response.status).to.equal(404)));
it('and LastResponse has no data', () => callAnApi.getLastResponse().then(response => expect(response.data).to.be.empty));
});
describe('performs a put on a resource and get 200', () => {
describe('performs a put on a resource and gets 200', () => {
const resource = '/put200';
const dummyRest = nock(baseUrl)
.put(resource)
dummyApi
.onPut(resource)
.reply(200, {
username: '1337',
});
@@ -117,19 +128,47 @@ describe('Abilities', () => {
it('and LastResponse has data', () => callAnApi.getLastResponse().then(response => expect(response.data).to.not.be.empty));
});
describe('performs a put on a resource and get 404', () => {
const resource = '/404';
const dummyRest = nock(baseUrl)
.put(resource)
describe('performs a put on a resource and gets 404', () => {
const resource = '/put404';
dummyApi
.onPut(resource)
.reply(404);
before(() => expect(callAnApi.put(resource, {username: '1337'})).to.eventually.be.fulfilled);
it('and eventually has a LastResponse', () => expect(callAnApi.getLastResponse()).to.eventually.not.be.undefined);
it('and LastResponse status is 404', () => callAnApi.getLastResponse().then(response => expect(response.status).to.equal(404)));
it('and LastResponse has no data', () => callAnApi.getLastResponse().then(response => expect(response.data).to.be.empty));
});
describe('performs a patch on a resource and gets 200', () => {
const resource = '/patch200';
dummyApi
.onPatch(resource, { username: '1337' })
.reply(200);
before(() => expect(callAnApi.patch(resource, { username: '1337' })).to.eventually.be.fulfilled);
it('and eventually has a LastResponse', () => expect(callAnApi.getLastResponse()).to.eventually.not.be.undefined);
it('and LastResponse status is 200', () => callAnApi.getLastResponse().then(response => expect(response.status).to.equal(200)));
it('and LastResponse has no data', () => callAnApi.getLastResponse().then(response => expect(response.data).to.be.empty));
});
describe('performs a patch on a resource and gets 404', () => {
const resource = '/patch404';
dummyApi
.onPatch(resource, { username: '1337' })
.reply(404);
before(() => expect(callAnApi.put(resource, { username: '1337' })).to.eventually.be.fulfilled);
it('and eventually has a LastResponse', () => expect(callAnApi.getLastResponse()).to.eventually.not.be.undefined);
it('and LastResponse status is 404', () => callAnApi.getLastResponse().then(response => expect(response.status).to.equal(404)));
it('and LastResponse has no data', () => callAnApi.getLastResponse().then(response => expect(response.data).to.be.empty));
});
});
});

0 comments on commit fe7e4c8

Please sign in to comment.