gpt4 book ai didi

unit-testing - 如何测试Gulp任务

转载 作者:行者123 更新时间:2023-12-03 07:29:10 25 4
gpt4 key购买 nike

自从我开始使用Gulp以来,我的项目就变得越来越大。现在我有一些非常花哨的任务,现在我想知道是否应该建立一些单元测试以保持理智?

有没有一种很好的简单方法来加载Gulpfile并确保我的任务正在执行我希望它们执行的操作?

有人曾经测试过他们的脚本,还是绝对浪费时间?

最佳答案

我的方法是创建一个测试实例,并使用execyeoman-assert。尽管它看起来更像是一个集成测试,但是我发现确保任务正确运行很有帮助(my use case being a yeoman-generator)。一些( Mocha )示例:

'use strict';
var path = require('path');
var helpers = require('yeoman-generator').test;
var assert = require('yeoman-generator').assert;
var exec = require('child_process').exec;
var fs = require('fs');
var injectStyles = require('../.test-instance/tasks/dev');


describe('gulp inject', function ()
{
var instancePath = path.join(__dirname, '../.test-instance');
var mainScss = path.join(instancePath, 'app/styles/main.scss');
var gulpfile = path.join(instancePath, 'gulpfile.js');
var gulp = '$(which gulp)';
var injectStylesCmd = gulp + ' injectStyles';

describe('scss partials in styles folder', function ()
{
var expectedContent = [
[mainScss, /_variables/],
[mainScss, /base\/_buttons\.scss/],
[mainScss, /base\/_fonts\.scss/],
[mainScss, /base\/_forms\.scss/],
[mainScss, /base\/_icons\.scss/],
[mainScss, /base\/_lists\.scss/],
[mainScss, /base\/_page\.scss/],
[mainScss, /base\/_tables\.scss/],
[mainScss, /base\/_typography\.scss/],
[mainScss, /functions\/_some-function\.scss/],
[mainScss, /mixins\/_some-mixin\.scss/],
[mainScss, /placeholders\/_some-placeholder\.scss/]
];
var expected = [
mainScss
];


beforeEach(function (done)
{
this.timeout(10000);
fs.truncateSync(mainScss);
fs.writeFileSync(mainScss, '// inject:sass\n\n// endinject');
exec(injectStylesCmd + ' injectStyles', {
cwd: instancePath
}, function (err, stdout)
{
done();
});
});

it('creates expected files', function ()
{
assert.file([].concat(
expected
));
assert.fileContent([].concat(
expectedContent
));
});
});
});

当然,您需要确保已设置测试实例。例如,您可以通过 fs.writeFileSync创建测试文件。在大多数情况下,您需要确保实例具有相同的目录结构,并且至少要存在gulpfile。

关于unit-testing - 如何测试Gulp任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24567417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com