I'm missing something in my syntax here but not sure what:
I'm trying to nock this. But I get expected undefined to equal 'pXVCJ9.eyJpYXQ'
Test.js
describe('User API', () => {
let email, password, requestBody
beforeEach(() => {
email = '[email protected]'
password = 'password'
requestBody = {
session: '05833a20-4035',
token: 'pXVCJ9.eyJpYXQ' }
nock('https://someurl/')
.get('users/sessions')
.reply(200, requestBody)
})
it('returns user session for login', async () => {
const data = await UserApi.login(email, password)
expect(data.token).to.equal(requestBody.token)
expect(data.session).to.equal(requestBody.session)
})
})
UserApi.js
import request from 'superagent'
export const endpoint = 'https://someurl/'
const login = (email, password) => async () => {
try {
return await request
.get(`${endpoint}/users/sessions`)
.send({ email: email, password: password })
.set('Accept', 'application/json')
} catch (err) {
//todo: implement this
}
}
export default { login }
There's a few issues:
const login = (email, password) => async () => { ... }
This makes login
a function that returns an async function. It should be an async function itself, so this:
const login = async (email, password) => { ... }
Next, in your nock
setup, you're not prefixing the path with a /
, so nock
won't be able to match the request. Easy fix:
nock('https://someurl')
.get('/users/sessions')
.reply(...);
Lastly, superagent
will return the response data as response.body
or, in your case, data.body
. So the assertions should look like this:
expect(data.body.token).to.equal(requestBody.token)
expect(data.body.session).to.equal(requestBody.session)