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

318
Visualizações
Mocha.js - How to save a global variable?

I'm working with Mocha.js for testing in a Node.js - Express.js - Firebase

I need a token from Firebase to access the API endpoints, I have a before hook in all my files, but after about 250 tests, probably calling the authentication endpoint multiple times, I'm getting rate limited by firebase.

I want to get the token once and use it in all my tests.

The tests are spread in different files, I have an index.js that requires them all. I'm aware of Root Level Hooks, but how can I save the token and use it in all my separate files?

Thanks!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

you can create a function that gets the token. then call it. then create your test suite only after that

function getToken(callback) {
  //
}

// define tests
function allTests(token) {
    describe(xxxxxx, function () {
        it(xxxxxxxxx, function() {
            //
        })
    });
}

// start all
getToken(function(token) {
    allTests(token);
});

about 4 years ago · Juan Pablo Isaza Relatório

0

  • Create a JSON file in your test root directory.
  • Import the file.
  • Append a token property with the token value.
  • Then import it anywhere to access the token property .
about 4 years ago · Juan Pablo Isaza Relatório

0

I managed to solve it myself, if anyone needs an answer on how to approach it, take a look at this.

I have multiple files where we write our unit testing, we unite them in an index.spec.js that we execute for testing ($ mocha index.spec.js)

I created a utility file that looks like this:

let token;
(() => { token = getYourToken() })()
module.exports = {
getToken: () => {
    return new Promise((resolve) => {
        const interval = setInterval(() => {
            if (token) {
                clearInterval(interval);
                resolve(token);
            }
        }, 100);
    });
}
};

Basically, it's a singleton, in the index.spec.js I require this file, executing the 'getYourToken()' once (add your logic to get token here). Then I store it in a variable that then I export. In the export, I use an interval because my current code is not using promises, use your best practice method, interval + Promise worked for me.

This way I require this file in my tests and get the token I got at the beginning once, avoiding rate-limiting and any issue with firebase.

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