tasks.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global describe, it, beforeEach, afterEach */
  6. 'use strict';
  7. const mockery = require( 'mockery' );
  8. const sinon = require( 'sinon' );
  9. const Vinyl = require( 'vinyl' );
  10. const utils = require( '../../tasks/gulp/build/utils' );
  11. const babel = require( 'babel-core' );
  12. const chai = require( 'chai' );
  13. const expect = chai.expect;
  14. const gutil = require( 'gulp-util' );
  15. describe( 'build-tasks', () => {
  16. let sandbox;
  17. const config = {
  18. ROOT_DIR: '.',
  19. DIST_DIR: 'dist'
  20. };
  21. beforeEach( () => {
  22. mockery.enable( {
  23. warnOnReplace: false,
  24. warnOnUnregistered: false
  25. } );
  26. sandbox = sinon.sandbox.create();
  27. } );
  28. afterEach( () => {
  29. mockery.disable();
  30. sandbox.restore();
  31. } );
  32. describe( 'build', () => {
  33. it( 'should return build stream', ( done ) => {
  34. const code = 'export default {};';
  35. sandbox.stub( gutil, 'log' );
  36. mockery.registerMock( 'minimist', () => {
  37. return {
  38. formats: 'amd',
  39. watch: false
  40. };
  41. } );
  42. const tasks = require( '../../tasks/gulp/build/tasks' )( config );
  43. const build = tasks.build;
  44. const stream = require( 'stream' );
  45. const files = [
  46. new Vinyl( {
  47. cwd: './',
  48. path: './src/file.js',
  49. contents: new Buffer( code )
  50. } )
  51. ];
  52. // Stub input stream.
  53. sandbox.stub( tasks.src, 'all', () => {
  54. const fakeInputStream = new stream.Readable( { objectMode: true } );
  55. fakeInputStream._read = () => {
  56. fakeInputStream.push( files.pop() || null );
  57. };
  58. return fakeInputStream;
  59. } );
  60. // Stub output stream.
  61. sandbox.stub( utils, 'dist', () => {
  62. const fakeOutputStream = new stream.Writable( { objectMode: true } );
  63. fakeOutputStream._write = ( file, encoding, done ) => {
  64. const result = babel.transform( code, { plugins: [ 'transform-es2015-modules-amd' ] } );
  65. // Check if provided code was transformed by babel.
  66. expect( file.contents.toString() ).to.equal( result.code );
  67. done();
  68. };
  69. return fakeOutputStream;
  70. } );
  71. const conversionStream = build();
  72. conversionStream.on( 'finish', () => done() );
  73. } );
  74. } );
  75. } );