ridthyself
ridthyself

Reputation: 839

Test driven node (without testing frameworks)

I have my code:

var User = function() {
    ...
}

and the test code using IIFE:

(function() { // test new user
    var user = new User();
    if (typeof user === 'undefined') {
        console.log("Error: user undefined");
    }
    ...
}());

both in the same js file. Works great! But, as the program grows, this is becoming too refractory for me to manage, as I have a piece of test code for every piece of business logic.

I've been taught to keep all my js in the same file, (minified is good) in production, but is there a best-practical way to keep my test code in a different file during development?

I was thinking I could use a shell script to append the test code to the production code when I want to run the tests, but I'd prefer a cross-platform solution.

I don't want or need a framework solution, I want to keep it light -- does node have anything built-in for this sort of thing?

Upvotes: 1

Views: 69

Answers (1)

Kornelia Kobiela
Kornelia Kobiela

Reputation: 477

Node has two expressions for this case. First:

module.exports = name_of_module;

Which is to export module for example function, object or something similar. And the second:

var module_name = require('Path/to/module');

to import it from other file. If you want to export IIFE you must assign it to global variable and module.export name of variable.

Upvotes: 1

Related Questions