Condition for differentiating testing with a helper node

How can we differentiate the normal usage and testing, at runtime based on some condition?
Something similar to :

if (process.env.NODE_ENV === 'test') {

So that we can prevent some initialization which may cause error while testing using npm run test.

Why not use that?

I sometimes add other tests at least temporarily, often to a small function that wraps console so that I can carry on dumping a gazillion log statements without worrying whether I left some in when I published the code :slight_smile:

Thank you for the response. I have used it. But I thought there would some difference when we start a test with helper node.

Seems it is a dumb question. :laughing:

btw, could you please show me an example on what you have told about the temporary tests? That will be really helpful.

I have this dirty code in one of my helper libraries:

const mylog = (process.env.TI_ENV === 'debug') ? console.log : function() {}

and this:

    /** Dump process memory use to console
     * @param {string} prefix Text to output before the memory info
    dumpMem: (prefix) => {
        let mem = process.memoryUsage()
        const formatMem = (m) => ( m/1048576 ).toFixed(2)
        mylog(`${prefix} Memory Use (MB): RSS=${formatMem(mem.rss)}. Heap: Used=${formatMem(mem.heapUsed)}, Tot=${formatMem(mem.heapTotal)}. Ext C++=${formatMem(mem.external)}`)

So I can use those in my code without much worry. I've occasionally used an IP address check as well during development of editor or front-end code such that running on localhost (e.g. my development environment) outputs copious information but running from the server doesn't. Lets me do UAT type tests without the noise.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.