1

I'm trying to use the following gulpfile.js in order to watch for changes, restart my server, and re-run my tests (cucumber.js). Changing a cucumber spec, or a file in my application both successfully trigger the process. However, the updated content of the specs is ignored! It's like the spec is cached. How can I ensure the latest specs are executed?

File content:

"use strict";
var gulp = require( "gulp" );
var cucumber = require( "gulp-cucumber" );
var watch = require( "gulp-watch" );
var server = require( 'gulp-develop-server' );

gulp.task( "cukes", function( done ) {

    return gulp.src( "specs/**/*.feature" ).pipe( cucumber( {

        "steps" : "specs/steps/**/*.js,support/*_hooks.js",
        "format" : "pretty"

    } ) );

} );


gulp.task( "test", [ "server:start", "cukes" ] );

gulp.task( "server:start", function() {

    server.listen( { path: "./index.js" } );

} );

gulp.task( "watch", [ "server:start" ], function() {

    watch( [ "lib/**/*", "specs/**/*" ], function() {

        server.restart( function() {

            setTimeout( function() { gulp.start( "cukes" ); }, 500 );

        } );

    } );

} );
4

1 回答 1

3

节点模块缓存在 中require.cache,因此您需要重置缓存以不包含 cucumber 所需的内容。您可以通过Object.keys(require.cache)在调用之前保存watch(),然后require.cache在开始“cukes”任务之前删除任何新键来做到这一点。像这样的东西应该可以工作:

gulp.task( "watch", [ "server:start" ], function() { var originalKeys = Object.keys(require.cache); watch( [ "lib/**/*", "specs/**/*" ], function() { server.restart( function() { var newKeys = Object.keys(require.cache); while (newKeys.length > originalKeys.length) { delete require.cache[newKeys.pop()]; } setTimeout( function() { gulp.start( "cukes" ); }, 500 ); } ); } ); } );

(通过拉取请求向 gulp-cucumber 添加类似的内容可能也是一个好主意。)

于 2015-05-23T22:16:42.340 回答