Unit tests with JEST, on Node.Js, without using module.Exports in Javascript code files

Asked

Viewed 105 times

0

With both files below (ePar.js and ePar.test.js) in the same folder, how to perform the unit test, with the JEST, via Node.Js, of function ePar (in the archive ePar.js) WITHOUT ALTERING THE SAME?

I know adding module.exports = ePar; at the end of the archive ePar.js everything will work as expected. But how to perform the test in it, with JEST, without changing it?

Without changing it, is returning the error:

Typeerror: epar is not a Function

Below the code of the files:

file: epar.js

var ePar = function(num){ 
    return ((num % 2) === 0) ;
}

file: epar.test.js

var ePar = require('./ePar');

test('Testando função que informa se um número é par ou não', () => {
    let ar_dados = [

        //falsos
        {"input": 1, "valido": false},
        {"input": 3, "valido": false},
        {"input": 5, "valido": false},
        {"input": 15, "valido": false},
        {"input": 5579, "valido": false},

        // verdadeiros
        {"input": 2, "valido": true},
        {"input": 4, "valido": true},
        {"input": 10, "valido": true},
        {"input": 654896, "valido": true}
    ];

    for (let i = 0; i < ar_dados.length; i++) {
        expect(ePar(ar_dados[i].input)).toBe(ar_dados[i].valido);
    }
});

From now on, I appreciate any help!

  • If the test is unitary and you are not exporting the module, what is the function of the module first? Incidentally, use require in a file that does not export anything (a "non-model") is something that really should not work.

  • The intention is simply to perform the unit test. It can be with or without require, just run the test. Have any idea how to test the epar.js file without changing it?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.