2015-10-17 57 views
10

我是新來的節點並在任何「適當」環境下開發。我已經爲我當前的項目安裝了gulp,還有mocha和其他一些模塊。這裏是我的gulpfile.js:「Finishing」之後的吞嚥掛鉤

var gulp = require('gulp'); 
var mocha = require('gulp-mocha'); 
var eslint = require('gulp-eslint'); 

gulp.task('lint', function() { 
    return gulp.src(['js/**/*.js']) 
     // eslint() attaches the lint output to the eslint property 
     // of the file object so it can be used by other modules. 
     .pipe(eslint()) 
     // eslint.format() outputs the lint results to the console. 
     // Alternatively use eslint.formatEach() (see Docs). 
     .pipe(eslint.format()) 
     // To have the process exit with an error code (1) on 
     // lint error, return the stream and pipe to failOnError last. 
     .pipe(eslint.failOnError()); 
}); 

gulp.task('test', function() { 
    return gulp.src('tests/test.js', {read: false}) 
     // gulp-mocha needs filepaths so you can't have any plugins before it 
     .pipe(mocha({reporter: 'list'})); 
}); 

gulp.task('default', ['lint','test'], function() { 
    // This will only run if the lint task is successful... 
}); 

當我運行「一飲而盡」,這似乎完成所有工作,但掛起。我必須按Ctrl + C返回到命令提示符。我如何才能正確完成?

+0

您自己運行的任何任務('一飲而盡test','一飲而盡lint'),難道他們掛?我已經在這裏剪切和粘貼你的代碼,並且沒有任何問題可以運行它。沒有東西掛起。 – Louis

+0

我會稍後再試,謝謝。 – Ooberdan

回答

15

道歉,鄉親們!原來,這是在gulp-mocha FAQ解決。引述:

測試套件不退出

如果您的測試套件沒有退出可能是因爲你還有一個揮之不去的回調,最經常的開放數據庫連接 造成的。您應該關閉此連接或執行以下操作:

gulp.task('default', function() { 
    return gulp.src('test.js') 
     .pipe(mocha()) 
     .once('error', function() { 
      process.exit(1); 
     }) 
     .once('end', function() { 
      process.exit(); 
     }); 
}); 
2

在gulp任務中添加return語句。或者運行回調。

gulp.task('default', ['lint','test'], function (next) { 
    // This will only run if the lint task is successful... 
    next(); 
}); 
+0

我已經嘗試了回調,並添加一個返回無效。 – Ooberdan

3

如果沒有gulp-mocha後運行任何東西,接受的解決方案會爲你工作。但是,如果你需要gulp-mocha後運行任務(例如部署構建之前運行摩卡測試),這裏是一個將防止gulp無限期地掛起,同時仍允許任務運行gulp-mocha後一種解決方案:

gulp.on('stop',() => { process.exit(0); }); 
gulp.on('err',() => { process.exit(1); }); 

這工作,因爲gulpinheritsorchestrator其中emits the events分別在所有任務完成或錯誤後運行。

2

升級到摩卡4後,我可以通過將--exit傳遞給摩卡來解決此問題。

請參閱https://boneskull.com/mocha-v4-nears-release/#mochawontforceexit瞭解更多信息。

當使用一飲而盡,摩卡,添加選項,exit: true爲:

gulp.task('test', function() { 
    return gulp.src(['tests/**/*.spec.js'], {read: false}) 
    .pipe(mocha({ exit: true })); 
});