gulp-lazy-pipelines
Version:
gulp lazy pipelines for frontend development
177 lines (158 loc) • 6.08 kB
JavaScript
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const app = require('../out/index.js');
const gutil = require('gulp-util');
const gulp = require('gulp');
const tap = require('gulp-tap');
const StreamTest = require('streamtest');
const fs = require('fs');
const pathOS = require('path');
chai.use(chaiAsPromised);
const expect = chai.expect;
// Global test parameters
const PARAMS = (function() {
// Written parametes
let params =
{
jsOnNode:
{
src: 'mock/on_node.js',
expectationContent: 'test_expectations/on_node.js',
expectationMap: 'test_expectations/on_node.js.map'
},
jsOnBrowser: {
src: 'mock/browser/scripts/main.js',
expectationContentPath: 'test_expectations/browser/bundle.js',
expectationMapPath: 'test_expectations/browser/bundle.js.map'
},
stylus: {
src: 'mock/browser/styles/main.styl',
expectationContentPath: 'test_expectations/browser/test.css',
expectationMapPath: 'test_expectations/browser/test.css.map'
},
template: {
src: 'mock/browser/templates/index.pug',
expectationContentPath: 'test_expectations/browser/index.html',
expectationMapPath: 'test_expectations/browser/index.html.map'
}
};
// Derivated parameters
// js on node
params.jsOnNode.expectedContent = fs.readFileSync(params.jsOnNode.expectationContent, 'utf8');
params.jsOnNode.expectedMap = fs.readFileSync(params.jsOnNode.expectationMap, 'utf8');
// js on Browser
params.jsOnBrowser.expectedContent = fs.readFileSync(params.jsOnBrowser.expectationContentPath, 'utf8');
params.jsOnBrowser.expectedMap = fs.readFileSync(params.jsOnBrowser.expectationMapPath, 'utf8');
// stylus
params.stylus.expectedContent = fs.readFileSync(params.stylus.expectationContentPath, 'utf8');
params.stylus.expectedMap = fs.readFileSync(params.stylus.expectationMapPath, 'utf8');
// Template
params.template.expectedContent = fs.readFileSync(params.template.expectationContentPath, 'utf8');
params.template.expectedMap = fs.readFileSync(params.template.expectationMapPath, 'utf8');
return params;
}());
/**
* Test pipeline constructos with arguments
*/
function streamWrapper(pipeConstructor, args, src) {
// object, which will be returned on resolve
const results = {
contentArr: [],
historyArr: []
};
// construct pipeline and typecheck
let pipeline = pipeConstructor.apply(null, args);
if (typeof(pipeline) !== 'function') throw Error('Constructed pipeline must be a function');
// Let the stream flow
let outputPromise = new Promise(function(resolve, reject) {
gulp.src(src)
.pipe(pipeline())
.on('data', function (chunk) {
results.historyArr.push(chunk.history);
results.contentArr.push(chunk._contents.toString('utf8'));
})
.on('end', function streamEnd() {
resolve(results);
})
.on('error', err => {
reject(err);
});
});
return outputPromise;
}
describe('gulp pipelines', function nodeBuildPipelineTest() {
/** @test {buildPipeline} */
it('Use build pipeline', function useBuildPipelineTest() {
let promisedContect = expect(streamWrapper(app.buildPipeline,
['result.js', 'es2015-node6'], PARAMS.jsOnNode.src))
.to.eventually.have.property('contentArr').which.be.an('array')
.with.deep.property('[1]')
.which.equal(PARAMS.jsOnNode.expectedContent);
let promisedMap = expect(streamWrapper(app.buildPipeline,
['result.js', 'es2015-node6'], PARAMS.jsOnNode.src))
.to.eventually.have.property('contentArr').which.be.an('array')
.with.deep.property('[0]')
.which.equal(PARAMS.jsOnNode.expectedMap);
return Promise.all(
[
promisedContect,
promisedMap
]);
});
/** @test {bundlePipeline} */
it('Use bundle pipeline', function useBundlePipelineTest() {
let promisedContent = expect(streamWrapper(app.bundlePipeline, ['bundle.js'], PARAMS.jsOnBrowser.src))
.to.eventually.have.property('contentArr').which.be.an('array')
.with.deep.property('[1]')
.which.equal(PARAMS.jsOnBrowser.expectedContent);
let promisedMap = expect(streamWrapper(app.bundlePipeline, ['bundle.js'], PARAMS.jsOnBrowser.src))
.to.eventually.have.property('contentArr').which.be.an('array')
.with.deep.property('[0]')
.which.equal(PARAMS.jsOnBrowser.expectedMap);
return Promise.all(
[
promisedContent,
promisedMap
]
);
});
/** @test {stylusPipeline} */
it('Use stylus pipeline', function useStylusPipelineTest() {
let pipeline = app.stylusPipeline({}, 'test.css');
gulp.src(PARAMS.stylus.src)
.pipe(pipeline())
.pipe(gulp.dest('./.tmp'));
let promisedContent = expect(streamWrapper(app.stylusPipeline, [{}, 'test.css'], PARAMS.stylus.src))
.to.eventually.have.property('contentArr').which.be.an('array')
.with.deep.property('[1]')
.which.equal(PARAMS.stylus.expectedContent);
let promisedMap = expect(streamWrapper(app.stylusPipeline, [{}, 'test.css'], PARAMS.stylus.src))
.to.eventually.have.property('contentArr').which.be.an('array')
.with.deep.property('[0]')
.which.equal(PARAMS.stylus.expectedMap);
return Promise.all(
[
promisedContent,
promisedMap
]);
});
/** @test {templatePipeline} */
it('UsetemplatePipeline', function usetemplatepipelineTest() {
let pipeline = app.templatePipeline({pretty: true});
gulp.src(PARAMS.template.src)
.pipe(pipeline())
.pipe(gulp.dest('.tmp'));
let promisedContent = expect(streamWrapper(app.templatePipeline, [{pretty: true}], PARAMS.template.src))
.to.eventually.have.property('contentArr').which.be.an('array')
.with.deep.property('[1]')
.which.equal(PARAMS.template.expectedContent);
let promisedMap = expect(streamWrapper(app.templatePipeline, [{pretty: true}], PARAMS.template.src))
.to.eventually.have.property('contentArr').which.be.an('array')
.with.deep.property('[0]')
.which.equal(PARAMS.template.expectedMap);
return Promise.all([
promisedContent,
promisedMap
]);
});
});