J.R.
J.R.

Reputation: 809

nodejs: how to ignore the "import" error while testing file with mocha?

let's say I have a nodejs project. In the app.js file I have to read some property file with the propertiesReader.

var propertiesReader = require('properties-reader')
var property = getPropertyFile('/fileName.properties')

The thing is this fileName.properties does not exist in my Host PC but only on the target machine.

When I run this code on the target machine everything is fine but when I tried to require('./app.js') in my mocha test file, the mocha reports error that this property file does not exist. So it says an exception outside of the test frame has been raised.

I tried:

try{
    const test = require('./app.js')
}catch(err)
{}

But the exception will still be shown and it messed up with my test result message.

Is there any method that I can 'import' this app.js without printing these 'import' errors?

Upvotes: 0

Views: 796

Answers (1)

Dustin Evans
Dustin Evans

Reputation: 65

You could use env.

if(process.env == 'remote'){
    var property = getPropertyFile('/fileName.properties')
}

$> NODE_ENV=local node app.js

In this case, the file would not be required.

Upvotes: 1

Related Questions