Last active
July 1, 2020 07:38
-
-
Save ctrlplusb/190e426a01f1b9dc1140d7b9273175b8 to your computer and use it in GitHub Desktop.
Stripped down and basic example of testing superagent based code using superagent-mock and mocha.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { expect } from 'chai'; | |
import superagent from 'superagent'; | |
import mockSuperagent from 'superagent-mock'; | |
import * as Errors from '../../../common/utils/errors'; | |
// Under test. | |
import login from './login'; | |
describe(`login api`), () => { | |
const serverEndPoint = `http://foobar.com/login`; | |
describe(`successful login request`, () => { | |
let superagentMock; | |
const expectedResult = { | |
username: `bob`, | |
firstname: `foo`, | |
surname: `bar` | |
}; | |
let actualParams; | |
before(() => { | |
superagentMock = mockSuperagent(superagent, [{ | |
pattern: `${serverEndPoint}`, | |
fixtures: (match, params) => { | |
actualParams = params; | |
return expectedResult; | |
}, | |
post: (match, data) => ({ body: data }) | |
}]); | |
}); | |
after(() => { | |
superagentMock.unset(); | |
}); | |
it(`should return the expected result`, () => | |
login(`foo`, `bar`).then(actual => { | |
expect(actual).eql(expectedResult); | |
}) | |
); | |
it(`should have sent the expected parameters`, () => { | |
const expectedFormPostParams = { username: `foo`, password: `bar` }; | |
login(`foo`, `bar`).then(() => { | |
expect(actualParams).equals(expectedFormPostParams); | |
}); | |
}); | |
}); | |
describe(`invalid response data received`, () => { | |
const formFailureData = { | |
password: `invalid` | |
}; | |
let superagentMock; | |
before(() => { | |
superagentMock = mockSuperagent(superagent, [{ | |
pattern: `${serverEndPoint}`, | |
fixtures: () => { | |
const serverError = new Error(400); | |
// the server we connect to uses 400 to indicate form | |
// post validation failures. | |
serverError.status = 400; | |
serverError.body = formFailureData; | |
throw serverError; | |
} | |
}]); | |
}); | |
after(() => { | |
superagentMock.unset(); | |
}); | |
it(`should reject with a FormError`, () => | |
login(`foo`, `bar`).then( | |
function success() {}, | |
function rejected(actual) { | |
expect(actual).instanceOf(FormError); | |
expect(actual.failure).equal(formFailureData); | |
} | |
) | |
); | |
}); | |
describe(`network issues`, () => { | |
let superagentMock; | |
before(() => { | |
superagentMock = mockSuperagent(superagent, [{ | |
pattern: `${serverEndPoint}`, | |
fixtures: () => { | |
const serverError = new Error(); | |
// with superagent, an err with no status indicates | |
// a network related error. | |
serverError.status = undefined; | |
throw serverError; | |
} | |
}]); | |
}); | |
after(() => { | |
superagentMock.unset(); | |
}); | |
it(`should reject with a NetworkError`, () => { | |
const expected = Errors.networkError(); | |
return login(`foo`, `bar`).then( | |
function success() {}, | |
function rejected(actual) { | |
expect(actual).eql(expected); | |
} | |
); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment