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

166
Visualizações
Mocha: How to assert after an event?

I'm working with Electron and Johnny-Five to process some data I read with my Arduino Mega 2560 and I'm having some trouble testing my Arduino's connection.

This is the method I want to test (ignore the awful signature):

export let board: Board | null = null;

export function setArduinoBoard(f: (num: number, ...etc: any[]) => void, num: number, ...etc: any[]) {
    if (board == null)
        board = new Board({...});

    board = board.on("ready", () => f(num, etc));
}

And this is my test:


describe.only("setArduinoBoard()", function() {
    it("can communicate with the Arduino device", function(done) {
        const f = (num: number) => console.log(num);
        const spy = sinon.spy(f);
        setArduinoBoard(f, 0);
        assert(spy.called);
        done();
    });
});

What I want is for the assertion to wait until the function has been called. And I know it's called eventually because the console outputs 0 but it's only after the assertion fails.

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

0

The typical approach would be moving the done callback into the event handler. In this way, the test will wait until the callback is called. If the ready event is not fired, the callback won't be called and the test will timeout with an error after 2 seconds.

This means that you don't need to explicitly assert that the event f is called, and in fact, you don't event need to spy on it.

    it("can communicate with the Arduino device", function(done) {
        const f = (num: number) => {
            console.log(num);
            done();
        };
        setArduinoBoard(f, 0);
    });

If you need to assert something after the ready event has fired, you can add an assertion in the event handler and use a catch block to capture exceptions and pass them to the done callback.

    it("can communicate with the Arduino device", function(done) {
        const f = (num: number) => {
            try {
                assert.equal(num, 0);
                done();
                return;
            } catch (err) {
                done(err);
            }
        };
        setArduinoBoard(f, 0);
    });
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