Skip to content

Instantly share code, notes, and snippets.

@Farenheith
Created February 29, 2020 06:22
Show Gist options
  • Save Farenheith/3e2e24c6510a5ca063d18f93c8fd56a7 to your computer and use it in GitHub Desktop.
Save Farenheith/3e2e24c6510a5ca063d18f93c8fd56a7 to your computer and use it in GitHub Desktop.
Example of test to the first example of code in https://visionmedia.github.io/superagent/
import { expect } from 'chai';
import { stub, match } from 'sinon';
import request = require('superagent');
import { superagentExample } from '../src/superagent-example';
describe('superAgentExample()', () => {
let requestResult: request.SuperAgentRequest;
beforeEach(() => {
requestResult = Promise.resolve({ body: 'expected body' }) as any;
requestResult.send = stub().returns(requestResult);
requestResult.set = stub().returns(requestResult);
stub(request, 'post').returns(requestResult);
stub(console, 'log');
});
it('should run request and log result', async () => {
const result = await superagentExample();
expect(request.post).to.have.been.calledOnceWithExactly('/api/pet');
expect(requestResult.send).to.have.been.calledOnceWithExactly(match.object);
expect(requestResult.set).to.have.been.calledTwice
.calledWithExactly('X-API-Key', 'foobar')
.calledWithExactly('Accept', 'application/json');
expect(console.log).to.have.been.calledOnceWithExactly('yay got "expected body"');
expect(result).to.be.undefined;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment