Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
Undefined array elements in my automated javascript/chai tests

I am using chai and javascript to run some automated tests against API endpoints.

In one of my tests, I am pushing results into an array (shown below, the array is named acceptanceCriteriaHashes). If I put a breakpoint in the before code then I can see the array, and all the elements within the array, are fully populated with the object returned from the db call.

However when I actually come to verify the contents of the array later in the test, the array, although it has the correct number of elements, contains only undefined?

Have tried to push directly with the results of the db call and that doesn't work either?

const getAcceptanceCriteria = (testCase) => {
let getAcceptanceCriteriaResponse;
let myLicenceChecksPassed = false;

const acceptanceCriteriaHashes = new Array();

describeTest(`${testCase.testCaseId} - ${testCase.testDescription}`, async () => {
    before(async () => {
        if (testCase.myLicenceChecksPassed !== undefined) {
            myLicenceChecksPassed = testCase.myLicenceChecksPassed;
        }
        getAcceptanceCriteriaResponse = await getAcceptanceCriteriaAPICall(testCase.productId, testCase.assetType, testCase.underwriter, myLicenceChecksPassed);
        if (getAcceptanceCriteriaResponse != undefined && getAcceptanceCriteriaResponse.body.Detail.length > 0) {
            for (let i = 0; i < getAcceptanceCriteriaResponse.body.Detail.length; i++) {
                // eslint-disable-next-line prefer-const
                let response = await readAcceptanceCriteriaHashes(getAcceptanceCriteriaResponse.body.Detail[i].ContentHash);
                if (response != undefined) {
                    let record = { Hash: response.Hash, Criteria: response.Criteria };
                    acceptanceCriteriaHashes.push(record);
                }
            }
            console.log(acceptanceCriteriaHashes);
        }
    });

// other tests successfully run here

    it(`${testCase.testCaseId} - Record for contentHash exists in the AcceptanceCriteriaHashes table`, async () => {
        expect(acceptanceCriteriaHashes).to.not.be.empty;
        expect(getAcceptanceCriteriaResponse.body.Detail.length).to.eql(acceptanceCriteriaHashes.length);
        for (let i = 0; i < acceptanceCriteriaHashes.length; i++) {
            expect(acceptanceCriteriaHashes[i].to.not.be('undefined', `Returned undefined for ContentHash: ${getAcceptanceCriteriaResponse.body.Detail[i].ContentHash}`));
            expect(acceptanceCriteriaHashes[i].Criteria.to.not.be.empty);
        }
    });
});

};

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The issue was I needed to declare the record variable outside of the before code block otherwise, when we exited the before function, it ceased to exist by the time I came to query it in the subsequent test.. Declaration goes just before the before function:

const getAcceptanceCriteria = (testCase) => {
let getAcceptanceCriteriaResponse;
let myLicenceChecksPassed = false;
// eslint-disable-next-line prefer-const
// eslint-disable-next-line no-array-constructor
const acceptanceCriteriaHashes = new Array();
let record;

describeTest(`${testCase.testCaseId} - ${testCase.testDescription}`, async () => {
    before(async () => {
        if (testCase.myLicenceChecksPassed !== undefined) {
            myLicenceChecksPassed = testCase.myLicenceChecksPassed;
        }
        getAcceptanceCriteriaResponse = await getAcceptanceCriteriaAPICall(testCase.productId, testCase.assetType, testCase.underwriter, myLicenceChecksPassed);
        if (getAcceptanceCriteriaResponse != undefined && getAcceptanceCriteriaResponse.body.Detail.length > 0) {
            for (let i = 0; i < getAcceptanceCriteriaResponse.body.Detail.length; i++) {
                // eslint-disable-next-line prefer-const
                let response = await readAcceptanceCriteriaHashes(getAcceptanceCriteriaResponse.body.Detail[i].ContentHash);
                if (response != undefined) {
                    record = { Hash: response.Hash, Criteria: response.Criteria };
                    acceptanceCriteriaHashes.push(record);
                }
            }
            console.log(acceptanceCriteriaHashes);
        }
    });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!