为什么我的 api 的响应没有返回到我的单元测试?
Why is the response from my api not being returned to my unit test?
我强烈怀疑这与使用异步调用的 api 有关,但据我所知,我在所有正确的地方都有 'awaits'。
进行调用的单元测试:
const { expect } = require('@jest/globals');
const { getWorkerId } = require('../api');
describe("Workers api", () => {
test("it should return a status of 200", async () => {
let response = await getWorkerId(21882000)
expect(response.status).to.equal('200')
});
});
和 api 本身:
const axios = require('axios');
const getWorkerId = async (id) => {
await axios.get(`http://localhost:8080/v1/workers/${id}`)
.then(function (response) {
console.log(response.status);
return response;
})
.catch(function (error) {
console.log('Error number: ', error.errno);
console.log('Error code: ', error.code);
return error;
})
};
module.exports = { getWorkerId };
'console.log(response.status)' 行按预期输出“200”,如果我更改代码以显示 'response',果然就在那里。然而,我的单元测试的 expect 调用返回:'TypeError: Cannot read 属性 'equal' of undefined'.x
与 async/await 和 JS 中的 Promises 混淆是很常见的。我会在这里尽量简明扼要,但这是一个很大的话题,有很多细节,所以请耐心等待!
首先,使用async/await时,不需要使用.then()
。 await
关键字将(从字面上看)等待 Promise 被解析,并获得它的 returned 值。同样,.then()
将获得承诺的解析值,这是您收到的参数,在您的情况下,它是 .then()
.
中的 response
因此,await
和 .then()
的工作方式相似,您甚至可以一起使用它们,如下所示:
const response = await axios.get('https://jsonplaceholder.typicode.com/todos/1').then((data) => {
console.log(data)
return data
}).catch((err) => {
console.log(err)
return err
})
console.log(response.status) // Output is 200
上面发生的事情是 await
获取 .then()
的 return,并将其分配给 response
。因此,如果您将 async/await 与 .then()
一起使用,您应该有一个变量来接收由 .then()
编辑的值 return,以便您可以在范围。
但是,对于您的情况,我们有以下情况:
const getJsonPlaceholder = async (id) => {
await axios.get(`https://jsonplaceholder.typicode.com/todos/${id}`).then((data) => {
console.log(data)
return data
}).catch((err) => {
console.log(err)
return err
})
}
const response2 = await getJsonPlaceholder()
console.log(response2.status)// Output is "Cannot read property 'status' of undefined"
发生的事情是 await
接收到 .then()
的 returned 值,将其分配给任何东西,函数 getJsonPlaceholder
returns undefined,这是 JS 中没有 return 任何函数的默认值。
在这里你可以找到一个很棒的。
最后,为了让您的测试用例通过,删除 .then()
,将 axios
请求的 returned 值分配给一个变量,然后 return 这个函数上的变量getWorkerId
,这个应该够了吧!
我强烈怀疑这与使用异步调用的 api 有关,但据我所知,我在所有正确的地方都有 'awaits'。
进行调用的单元测试:
const { expect } = require('@jest/globals');
const { getWorkerId } = require('../api');
describe("Workers api", () => {
test("it should return a status of 200", async () => {
let response = await getWorkerId(21882000)
expect(response.status).to.equal('200')
});
});
和 api 本身:
const axios = require('axios');
const getWorkerId = async (id) => {
await axios.get(`http://localhost:8080/v1/workers/${id}`)
.then(function (response) {
console.log(response.status);
return response;
})
.catch(function (error) {
console.log('Error number: ', error.errno);
console.log('Error code: ', error.code);
return error;
})
};
module.exports = { getWorkerId };
'console.log(response.status)' 行按预期输出“200”,如果我更改代码以显示 'response',果然就在那里。然而,我的单元测试的 expect 调用返回:'TypeError: Cannot read 属性 'equal' of undefined'.x
与 async/await 和 JS 中的 Promises 混淆是很常见的。我会在这里尽量简明扼要,但这是一个很大的话题,有很多细节,所以请耐心等待!
首先,使用async/await时,不需要使用.then()
。 await
关键字将(从字面上看)等待 Promise 被解析,并获得它的 returned 值。同样,.then()
将获得承诺的解析值,这是您收到的参数,在您的情况下,它是 .then()
.
response
因此,await
和 .then()
的工作方式相似,您甚至可以一起使用它们,如下所示:
const response = await axios.get('https://jsonplaceholder.typicode.com/todos/1').then((data) => {
console.log(data)
return data
}).catch((err) => {
console.log(err)
return err
})
console.log(response.status) // Output is 200
上面发生的事情是 await
获取 .then()
的 return,并将其分配给 response
。因此,如果您将 async/await 与 .then()
一起使用,您应该有一个变量来接收由 .then()
编辑的值 return,以便您可以在范围。
但是,对于您的情况,我们有以下情况:
const getJsonPlaceholder = async (id) => {
await axios.get(`https://jsonplaceholder.typicode.com/todos/${id}`).then((data) => {
console.log(data)
return data
}).catch((err) => {
console.log(err)
return err
})
}
const response2 = await getJsonPlaceholder()
console.log(response2.status)// Output is "Cannot read property 'status' of undefined"
发生的事情是 await
接收到 .then()
的 returned 值,将其分配给任何东西,函数 getJsonPlaceholder
returns undefined,这是 JS 中没有 return 任何函数的默认值。
在这里你可以找到一个很棒的
最后,为了让您的测试用例通过,删除 .then()
,将 axios
请求的 returned 值分配给一个变量,然后 return 这个函数上的变量getWorkerId
,这个应该够了吧!