tasks.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* jshint node: true, esnext: true */
  2. 'use strict';
  3. const gulp = require( 'gulp' );
  4. const istanbul = require( 'gulp-istanbul' );
  5. const gutil = require( 'gulp-util' );
  6. const mocha = require( 'gulp-mocha' );
  7. const chai = require( 'chai' );
  8. const filterBy = require( 'gulp-filter-by' );
  9. const filter = require( 'gulp-filter' );
  10. const sinon = require( 'sinon' );
  11. const devTools = require( '../dev/utils/tools' );
  12. const semver = require( 'semver' );
  13. const buildUtils = require( '../build/utils' );
  14. /**
  15. * Defines Node.js testing task.
  16. *
  17. * To run tests under node:
  18. *
  19. * gulp test:node
  20. *
  21. * To run build before testing:
  22. *
  23. * gulp test:node:build
  24. *
  25. * To run testing with code coverage:
  26. *
  27. * gulp test:node:coverage
  28. */
  29. module.exports = () => {
  30. const ignoreRegexp = /\/\* ?bender-tags:.*\bbrowser-only\b.*\*\//;
  31. // Inject globals before running tests.
  32. global.should = chai.should;
  33. global.expect = chai.expect;
  34. global.assert = chai.assert;
  35. global.sinon = sinon;
  36. const tasks = {
  37. /**
  38. * Is set to `true` when code coverage report will be displayed.
  39. *
  40. * @type {Boolean}
  41. */
  42. coverage: false,
  43. /**
  44. * Prepares files for coverage report.
  45. *
  46. * @returns {Stream}
  47. */
  48. prepareCoverage() {
  49. tasks.coverage = true;
  50. return gulp.src( 'build/cjs/ckeditor5/**/*.js' )
  51. .pipe( istanbul() )
  52. .pipe( istanbul.hookRequire() );
  53. },
  54. /**
  55. * Runs tests in Node.js environment.
  56. *
  57. * @returns {Stream}
  58. */
  59. testInNode() {
  60. const minVersion = '6.0.0';
  61. const src = [
  62. 'build/cjs/tests/**/*.js',
  63. '!**/_utils/**/*.js',
  64. '!build/cjs/tests/{ui,ui-*}/**/*.js',
  65. '!build/cjs/tests/theme-*/**/*.js'
  66. ];
  67. if ( semver.lt( process.version, minVersion ) ) {
  68. throw new gutil.PluginError( {
  69. plugin: 'test-node',
  70. message: `Wrong Node.js version. Please use Node.js in version v${ minVersion } or higher.`
  71. } );
  72. }
  73. return gulp.src( src )
  74. .pipe( tasks.skipManual() )
  75. .pipe( tasks.skipIgnored() )
  76. .pipe( mocha( { reporter: 'progress' } ) )
  77. .pipe( tasks.coverage ? istanbul.writeReports() : buildUtils.noop() );
  78. },
  79. /**
  80. * Removes manual test files from source stream. It checks if the markdown file with the same name exists.
  81. *
  82. * @returns {Stream}
  83. */
  84. skipManual() {
  85. return filter( ( file ) => {
  86. return !devTools.isFile( file.path.slice( 0, -3 ) + '.md' );
  87. } );
  88. },
  89. /**
  90. * Skips test files that are marked to be ignored when testing outside browser.
  91. * To ignore file, add `browser-only` to bender-tags comment in test file.
  92. *
  93. * @returns {Stream}
  94. */
  95. skipIgnored() {
  96. return filterBy( file => !file.contents.toString().match( ignoreRegexp ) );
  97. }
  98. };
  99. gulp.task( 'test:node:pre-coverage', [ 'build:js:cjs' ], tasks.prepareCoverage );
  100. gulp.task( 'test:node', tasks.testInNode );
  101. gulp.task( 'test:node:build', [ 'build:js:cjs' ] , tasks.testInNode );
  102. gulp.task( 'test:node:coverage', [ 'build:js:cjs', 'test:node:pre-coverage' ], tasks.testInNode );
  103. return tasks;
  104. };