Skip to content

Instantly share code, notes, and snippets.

@wesleyduff
Last active March 11, 2018 02:07
Show Gist options
  • Save wesleyduff/a2108821caf6309a7f1a39bb6a2bef7a to your computer and use it in GitHub Desktop.
Save wesleyduff/a2108821caf6309a7f1a39bb6a2bef7a to your computer and use it in GitHub Desktop.
Mock a call to a service with npm nock and npm superagent using a POST
import nock from 'nock';
import request from 'superagent';
describe('Calling AWS SDK to sign up a user', () => {
let postData = {username: 'fake@fake.com', password: '@Password1'};
let _request = null;
beforeEach(() => {
nock('http://aws-sdk.aws.com/', {
reqheaders: {
"content-type": "application/json",
}
})
.post('/cognito/signUpUser')
.reply(200, {
ok: true,
cognito_id: 123456,
cognito_user: 'fake_name@fake.com'
});
_request = request
.post('http://aws-sdk.aws.com/cognito/signUpUser')
.set('accept', 'json');
}); //end beforeEach
it('Should return a 200', () => {
_request
.send(JSON.stringify(postData))
.end((err, res) => {
if(err){
console.log('------ ERROR : ')
console.log(err);
} else {
expect(res.statusCode).toEqual(200);
}
})
})
it('Should return a cognito id of 123456', () => {
_request
.send(JSON.stringify(postData))
.end((err, res) => {
if(err){
console.log('---- ERROR : ');
console.log(err);
} else {
expect(res.body.cognito_id).toBe('123456')
}
})
});
it('Should return a cognito user of "fake@fake.com', () => {
_request
.send(JSON.stringify(postData))
.end((err, res) => {
if(err){
console.log('---- ERROR : ');
console.log(err);
} else {
expect(res.body.cognito_user).toBe('fake@fake.com');
}
})
})
}) //end describe Calling AWS SDK to sign up a user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment