3
I have a Wordpress site with many JS files that have not been structured to be tested - they have not been written as modules that can be imported nor is there a app.js
that loads them all as a framework.
The files are only compiled and minified for use on the site, and I want to start rewriting little by little as I maintain the site, adding tests for solved bugs and new features.
All files have a structure similar to:
( function( window ) {
'use strict';
var document = window.document;
var objeto = {
params : {
// etc
},
init: function() {
// etc
},
outroMetodo: function() {
// etc
}
}
objeto.init();
} )(this);
I was suggested to use Jest and the setup was very simple - the test environment is ready - but I don’t know how to load the files that need to be tested. My current configuration on package.json
is this:
{
"scripts": {
"test": "jest"
},
"jest": {
"verbose": true,
"testMatch": [
"<rootDir>/tests/jest/**/*.test.js"
]
}
}
I imagine you need to refactor the files in some way to be able to load them into Jest before running the tests, but how would be the simplest way to allow this integration without rewriting the features? I tried using the settings setupFiles
and setupTestFrameworkScriptFile
but as I don’t have a single setup file it seems to me that is not the ideal option.
There is a way to include the file to be tested at the beginning of each test to test the methods?
include( '/arquivo.js' ); // pseudocodigo
describe("Testes para arquivo.js", function() {
it("testes do metodo X", function() {
expect(true).toBe(true);
});
});
The example code that you gave is completely private, in order to test this, this self-invoking Function has to assign this "module" to the window - of some way. Then you just access the
window.MODULE_NAME.metodo(arg)
for testing; checka o module Pattern since this code is good looking and the refactor would not be too difficult :)– MoshMage