Skip to content

Instantly share code, notes, and snippets.

@seacloud9
Created May 19, 2017 20:14
Show Gist options
  • Save seacloud9/38f79278d62ed8ceb2370f137d6c42ad to your computer and use it in GitHub Desktop.
Save seacloud9/38f79278d62ed8ceb2370f137d6c42ad to your computer and use it in GitHub Desktop.
function makeRequest(){
const auth0BaseURL = Config.AUTH_URL
const mock = { username: 'test03@222222.com', password: 'password!test', deviceTesting: true }
const data = {'client_id': Config.AUTH_CLIENT, 'username': mock.username, 'password': mock.password, 'connection': '', 'device': 'testdevice', 'scope': ''}
const api = apisauce.create({
baseURL: authBaseURL,
headers: {'content-type': 'application/json'},
timeout: 100000
})
debugger
var response = api.post('/auth/test', data, api.headers)
debugger
return response
}
test('signup:Success', async t => {
t.plan(1);
const res = await makeRequest()
debugger
t.is(res.status, 200);
});
Error: Promise returned by test never resolved\
@skellock
Copy link

Change line 1 to mark the function with async .
Change line 11 to include await before `api.post

@seacloud9
Copy link
Author

seacloud9 commented May 19, 2017

When I do that I receive a promise

[[PromiseStatus]]
:
"pending"
[[PromiseValue]]
:
undefined

but I can't seem to resolve it with then?

If I do this api.post('/oauth/ro', data, api.headers).then((response) => response) it never resolves ??

@skellock
Copy link

Can you update your code with what you have now?

@seacloud9
Copy link
Author

seacloud9 commented May 19, 2017

async function makeRequest(){
  const auth0BaseURL = Config.AUTH_URL
  const mock = { username: 'test03@222222.com', password: 'password!test', deviceTesting: true }
  const data = {'client_id': Config.AUTH_CLIENT, 'username': mock.username, 'password': mock.password, 'connection': '', 'device': 'testdevice', 'scope': ''}
  const api = apisauce.create({
    baseURL: authBaseURL,
    headers: {'content-type': 'application/json'},
    timeout: 100000
  })
  debugger
  var response = await api.post('/auth/test', data, api.headers)
  /* var response =await api.post('/auth/test', data, api.headers).then((response) => response)
  if I add this 'Promise returned by test never resolved', when I don't I always end up with pending promise I want to resolve that promise */
  debugger
  return response
}

test('signup:Success', t => {
  t.plan(1);
  const res = makeRequest()
  debugger
  t.is(res.status, 200);
});

@seacloud9
Copy link
Author

test('signup:Test', t => {
  const authBaseURL = Config.AUTH_URL
  const timeOUT = 100000
  const mock = { username: 'test03@test.com', password: 'test!!9', deviceTesting: true }
  const data = {'client_id': Config.AUTH_CLIENT, 'username': mock.username, 'password': mock.password, 'connection': 'test', 'device': 'testdevice', 'scope': 'openid'}
  const api = apisauce.create({
    baseURL: authBaseURL,
    headers: {'content-type': 'application/json'},
    timeout: timeOUT
  })

  api.addAsyncRequestTransform(req => {
    return new Promise((resolve, reject) => {
        console.log(req)
        debugger
        resolve(req)
    })
  })
  
  return api.post('/auth/test', data, api.headers).then(response => {
    console.log(response)
    debugger
    t.is(response.status, 200)
  })
})

//Error: Promise returned by test never resolved\n    processEmit [as emit] (node_modules/signal-exit/index.js:155:32)\n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment