Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda