1

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?

2
  • 1
    Actually it's better to split up your code over multiple files, you can always concatenate it together for production. Y Commented Jan 7, 2017 at 18:30
  • 1
    But to answer your question, just do it yourself by placing a placeholder in every file and use a simple replace command. With nodejs, making a command line tool is super easy! (Or just make a bash script) Commented Jan 7, 2017 at 18:31

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.