Monday, 24 April 2017

Mocha and relative paths

My code is component-based and tests reside directly in the components folder. Some components rely on files (or other resources), like the ConfigurationManager component:

class ConfigurationManager {
    constructor(protected configDir = 'config') { }

    addResource(file) {
        let fileUri = path.join(path.dirname(require.main.filename), this.configDir, file);
        let configuration = require(fileUri);
        // ...
    }
}

The config directory is in the root directory of the project, hence fileUri makes sense here. But when running the tests (mocha -r ts-node/register src/Components/**/Tests/*Test.ts) it fails, since the root directory isn't the project root anymore but the mocha directory within node_modules.

How to fix that?



via nehalist

No comments:

Post a Comment