Since I started using Gulp, my project got bigger. Now I have a few quite fancy tasks and now I'm wondering maybe I should build some unit tests to keep some sanity?
Is there a good and simple way to load Gulpfile and make sure my tasks are doing what I want them to do?
Anybody ever tested their scripts, or it's absolute waste of time?
in the Before launch area and choose Run Gulp task from the list. In the Gulp task dialog that opens, specify the Gulpfile. js where the required task is defined, select the task to execute, and specify the arguments to pass to the Gulp tool. Specify the location of the Node.
Gulp enables you to encode in JavaScript repetitive tasks that you perform regularly. This could be starting a web server, JavaScript minification or Testing which will be the focus of this blog. One of the core benefits of Gulp is that it encodes these tasks in a JavaScript file which is typically named gulp.
Install Gulp into your local project To install Gulp locally, navigate to your project directory and run npm install gulp . You can save it to your package. json dependencies by running npm install gulp --save-dev . Once you have Gulp installed locally, you can then proceed to create your gulpfile.
My approach is to create a test instance and use exec
and yeoman-assert. Though it feels more like an integration test, I find it helpful to be sure the tasks run properly (my use case being a yeoman-generator). Some (mocha) example:
'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 )); }); }); });
Of course you need to make sure, that you have a test-instance set up. You can create your testing files via fs.writeFileSync
for example. In most cases you need to make sure that the instance has the same directory structure and that at the very least the gulpfile is present.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With