I’d like some help on a coding error I am encountering with Mocha. I am having a hard time figuring out what I am doing wrong.
I am attempting to test an API with the Mocha framework using data from a JSON file that contains an array and validate the request body and response body using chai. Using a forLoop, each request in the array should be read and the response for each should be returned. However, only the first request is returning using the following code:
describe('Validate the Response Body for Store Orders', async function(){
let request = require('../config/data/testFile1.json');
chai.expect(request.storeOrders.collectedOrders[0]).to.exist
response = await apiEndpoint.post(request);
for(let i=0; i < response.body[0].orderLookup.orderNumber.length; ++i){
console.log(response.body[0].orderLookup.orderNumber.length)
chai.expect(response.body[0].orderLookup.orderNumber.length).to.exist
}
})
The first response is coming back 4 times rather than the responses for each request coming in once.
Here is the expected response:
The Responses should be:
[
{
"orderLookup":{
"orderNumber":"22222"
"stateCode": "IA"
}
},{
"orderLookup":{
"orderNumber":"22392"
"stateCode": "IA"
},{ "orderLookup":{
"orderNumber":"444"
"stateCode": "RI"
},{
"orderLookup":{
"orderNumber":"239129"
"stateCode": "MI"
}
]
I am receiving this instead.
[
{
"orderLookup":{
"orderNumber":"22222"
"stateCode": "IA"
}
},{
"orderLookup":{
"orderNumber":"22222"
"stateCode": "IA"
},{ "orderLookup":{
"orderNumber":"22222"
"stateCode": "IA"
},{
"orderLookup":{
"orderNumber":"22222"
"stateCode": "IA"
}
]
Here is the JSON request body:
[
{
"storeOrders": {
"orderType": "2",
"orderID": "22222",
"orderDetails": {
"productType": "Standard"
},
"collectedOrders": [
{
"orderDate": "2023-09-03",
"customerAge": "22"
}, {
"orderDate": "2024-29-03",
"customerAge": "22"
}
],
"storeDetails": {
"storeState": "IA"
}
}
},
{
"storeOrders": {
"orderType": "1",
"orderID": "444",
"orderDetails": {
"productType": "Standard"
},
"collectedOrders": [
{
"orderDate": "2023-06-03",
"customerAge": "60"
}
],
"storeDetails": {
"storeState": "RI"
}
}
}, {
"storeOrders": {
"orderType": "22392",
"orderID": "34922",
"orderDetails": {
"productType": "Standard"
},
"collectedOrders": [
{
"orderDate": "2023-09-03",
"customerAge": "22"
}, {
"orderDate": "2024-29-03",
"customerAge": "22"
}
],
"storeDetails": {
"storeState": "IA"
}
}
},
{
"storeOrders": {
"orderType": "1",
"orderID": "239129",
"orderDetails": {
"productType": "Standard"
},
"collectedOrders": [
{
"orderDate": "2023-06-03",
"customerAge": "60"
}
],
"storeDetails": {
"storeState": "MI"
}
}
}
]
I tried using forEach instead of a for Loop and encountered the same issue.