Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda