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

351
Visualizações
How to test javascript in node repl (no browser)?

I have 2 files mycode.js and mycode.test.js. I want to test it in Node console (repl) - (no browser), in karma style.

mycode.test.js

var expect = require('expect');
var mycode = require("./mycode");

describe("mycode", () => {
    it("should properly run tests", () => {
        expect(1).toBe(1);
    });
});

What is the simplest possible setup to achieve this?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I don't know how you could make that work in the node repl because a call to require() assumes you load that function separately, before loading the file you want to test. The only way I see of testing code in the repl is by having the function you are using to test included in the loaded file. For example, to test how function override works, you could create function_override.js and then load it into the repl --

$ .load path/to/file/function_override.js

-- and have an assert() function that is used to test the output of other functions inside that file. E.g.:

            function assert(value, text) {
              if (value === true) {
                  console.log(text);
              } else {
                  console.log ("that is false");
              }
            }

            var fun = 3;

            function FuncType1(){
              assert(typeof fun === "function", "We access the function");
            }

            function FuncType2(){
              var fun = 3;
              assert(typeof fun === "number", "Now we access the number");
            }

            function FuncType3(){
              assert(typeof fun === "number", "Still a number");
            }

            function fun(){};

            FuncType1(); // returns "We access the function"
            FuncType2(); // returns "Now we access the number"
            FuncType3(); // returns  "that is false"
over 4 years ago · Santiago Trujillo 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