摩卡和相对path
我的代码是基于组件的,testing直接驻留在组件文件夹中。 一些组件依赖文件(或其他资源),如ConfigurationManager
组件:
class ConfigurationManager { constructor(protected configDir = 'config') { } addResource(file) { let fileUri = path.join(path.dirname(require.main.filename), this.configDir, file); let configuration = require(fileUri); // ... } }
config
目录位于项目的根目录下,因此fileUri
在这里是有意义的。 但是当运行testing时( mocha -r ts-node/register src/Components/**/Tests/*Test.ts
)会失败,因为根目录不再是项目根目录,而是在node_modules
的mocha目录。
如何解决这个问题?