2017-03-27 31 views
1

當我在命令行上啓動grunt時,如何將參數傳遞給節點進程?通過grunt傳遞node.js選項

具體來說,我想將選項--expose-gc傳遞給運行grunt的節點。 我運行的命令是:

grunt mocha:mytests 

我想要實現,我的摩卡測試得到的垃圾收集接口暴露出來。

如果這是任何幫助:繁重的文件部分是這樣的:

myTest: { 
    src: [ 
     'mocha.hooks/*.spec.js', 
     'build/ch.actifsource.*/**/test/*.spec.js', 
     'mocha.hooks/*.spec.server.js', 
     'build/ch.actifsource.*/**/test/*.spec.server.js' 
    ], 
    options: { 
     timeout: 500, 
     logErrors: true 
    }, 
    ignore: [ 
     './src/**/RegisterResourceTypes.js' 
    ] 
} 
+1

什麼是摩卡:mytests任務看起來像在'Gruntfile.js'?也許在你的問題中包括代碼片段會增加你獲得答案的機會。 – RobC

+1

Grunt mocha插件API不提供選項來傳遞'--expose-gc'(..including this [one](https://github.com/kmiyashiro/grunt-mocha))。您可以使用[grunt-shell](https://github.com/sindresorhus/grunt-shell)代替。然後將其配置爲通過[CLI](https://mochajs.org/#usage)運行相同的mocha命令。你的_grunt-shell_命令是:'command:'./node_modules/mocha/bin/mocha -t 500 -gc path/to/files'',藉此'path/to/files'部分被替換爲'src'文件。請注意,該命令通過'bin'文件夾運行本地摩卡。您還需要安裝摩卡'$ npm i -D mocha' – RobC

+0

@RobC您所說的話將被認定爲答案! – Alfi

回答

1

的API對各種繁重的摩卡插件沒有提供一個選項,通過--expose-gc,包括本one

您可以安裝grunt-shell,並將其配置爲運行與您通過CLI相同的mocha命令。

咕嚕殼command是:

// ... 
shell: { 
    myTest: { 
     command: './node_modules/mocha/bin/mocha -t 500 -gc path/to/files' 
    } 
} 
// ... 

path/to/files部分應該與您src文件被替換。

注意,命令通過bin文件夾中運行本地摩卡所以你還需要安裝mocha

$ npm i -D mocha

+0

感謝您發佈這個答案。這是一個可憐的事,不可能通過這樣的選擇。然而,我不會將這個標記作爲公認的答案,因爲我仍然希望有人提出一個更加集成的解決方案。 – Alfi

+0

在相關說明中...在grunt-mocha項目回購(在GitHub上),有幾個請求/問題用於支持open [issues]中的其他_mocha_ CLI選項(https://github.com/kmiyashiro/咕嚕 - 摩卡/問題)。然而,由於'--expose-gc'是一個_mocha_ CLI選項,並且正如項目所有者在[回覆]中提到的那樣(https://github.com/kmiyashiro/grunt-mocha/issues/136#issuecomment-62059400) - 「插件不使用命令行摩卡」。所以如果有更集成的解決方案,我會感到驚訝。 – RobC

+0

那麼我該如何使用此選項啓用grunt本身?這真的只是爲了測試,我想明確觸發gc進行內存泄漏的運行時檢查。 (又名「確保所有實例都刪除了關於清理的引用」) – Alfi