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

183
Visualizações
How do I test this very old legacy Javascript module without rewriting it?

How do I load and instantiate this legacy javascript module for testing, without modifying it?

The legacy module (foo.js):

var Smurf = {};

Smurf.Foo = function() {};
Smurf.Foo.prototype = {
    bar: function() {
        return 'baz';
    }
};

The legacy front-end (working):

<script src="foo.js"></script>

<script type="text/javascript">
$(function(){
    var foo = new Smurf.Foo();
    var qux = foo.bar();
});
</script>

The new test (failing):

const something = require('foo.js');
var foo = new Smurf.Foo();

Result:

Error: Failed to load file test/test.js
ReferenceError: Smurf is not defined
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

In NodeJS, you can use the vm module https://nodejs.org/api/vm.html#vmruninthiscontextcode-options.

const { runInThisContext } = require('vm');
const { readFileSync } = require("fs");
function legacyRequire(path) { return runInThisContext(readFileSync(path)); }

var something = legacyRequire('./foo.js');
var foo = new Smurf.Foo();
console.assert(foo.bar() === 'baz');

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório

0

What you can do is include both the file you need to test and the file with the tests in a new html file. This way, first the file that needs testing is loaded and everything it has in it can be tested in the test file.

test.hml:

<html>
    <head>
        <script scr="foo.js">
        <script src="test.js">
    </head>
</html>

Every time you need to run a test, open test.html in a browser. Of course you need to rewrite the test file so it shows some output, either in the console or in the html itself.

about 4 years ago · Juan Pablo Isaza Relatório

0

The legacy module isn't a CommonJS module, therefore require() will not work. I recommend reading through this post to understand how require() works.

The short answer would be - you cannot "require()" foo.js and access Smurf unless the foo.js is exporting it (using something like module.exports).

Since you mentioned not wanting to rewrite the legacy code, I'd like to point you to this neat trick to Import non-ESM libraries in ES Modules, with client-side vanilla JS

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