Getting async/await to work with superagent

Viewed 4526

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 = 'someperson@gmail.com'
      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 }
1 Answers
Related