testingGrunt任务

我正在使用Yeoman来生成一些项目和咕-任务。 现在我也想使用Mocha来testing生成的grunt任务,但是我只能find一些如何在Grunt中使用Mochatesting的信息;-)

任何人都可以帮忙吗?

不是一个优雅的解决scheme,但我采取了安装我的依赖( npm install )的方法,并因此运行相应的咕噜任务(例如grunt less ),然后编写testing逻辑后操作。 我已经使用嵌套exec调用这个。

 describe('less grunt tasks tests', function () { var prompts = { workFolder: 'temp', fiddleDesc: 'mocha test' }; var testGlobal = {}; beforeEach(function(done) { testGlobal.app = helpers.run(path.join(__dirname, '../app')) .inTmpDir(function(dir, err) { if(err) { done(err); return; } testGlobal.dir = dir; // console.log(dir); }) .withArguments(['skip-install']) .withOptions({ less: true }) .withPrompts(prompts) .on('end', function(){ done(); }); }); it('should modify app/styles/style.css', function(done){ this.timeout(60000 * 10); //10 minutes - my network is f**ked up var opts = { cwd : testGlobal.dir, env: process.env, detached: true }; var gen = testGlobal.app.generator; var devdeps = gen.devDependencies.join(' '); var rootPath = testGlobal.dir; var getPath = function(fpath) { var s = path.join(rootPath, fpath); // console.log(s); ; return s; }; exec('npm install ' + devdeps, opts, function(err, stdout, stderr) { if(err) { done(err); return; } var h1 = fs.readFileSync(getPath('app/less/h1.less'), 'utf8'); var css = fs.readFileSync(getPath('app/styles/style.css'), 'utf8'); // expect(css).to.not.contain(h1); expect(css).to.not.contain('h1'); exec('grunt less', opts, function(e, out, serr){ if(e) { done(e); return; } // console.log(out); var h1 = fs.readFileSync(getPath('app/less/h1.less'), 'utf8'); var css = fs.readFileSync(getPath('app/styles/style.css'), 'utf8'); // expect(css).to.contain(h1); //this expect fails since for some reason \r are stripped out expect(css).to.contain('h1'); done(); }); }); }); }); 

有关更多参考,您可以在我参与的回购中看到更多的testing代码。

Ps:感谢您对我采取的方法提出的意见。