Last active
March 11, 2018 02:07
-
-
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
This file contains 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 nock from 'nock'; | |
import request from 'superagent'; | |
describe('Calling AWS SDK to sign up a user', () => { | |
let postData = {username: '[email protected]', 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: '[email protected]' | |
}); | |
_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 "[email protected]', () => { | |
_request | |
.send(JSON.stringify(postData)) | |
.end((err, res) => { | |
if(err){ | |
console.log('---- ERROR : '); | |
console.log(err); | |
} else { | |
expect(res.body.cognito_user).toBe('[email protected]'); | |
} | |
}) | |
}) | |
}) //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