gulpTasks-tests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // Import Dependencies.
  2. var gulp = require("gulp");
  3. var typescript = require("gulp-typescript");
  4. var karmaServer = require('karma').Server;
  5. var webpack = require('webpack');
  6. var webpackStream = require("webpack-stream");
  7. var rename = require("gulp-rename");
  8. // Import Helpers.
  9. var rmDir = require("../../NodeHelpers/rmDir");
  10. // Read the full config.
  11. var config = require("../../Config/config.json");
  12. var relativeRootDir = "../../../";
  13. var rootDir = __dirname + "/" + relativeRootDir;
  14. /**
  15. * Launches the KARMA validation tests in chrome in order to debug them.
  16. */
  17. gulp.task("tests-validation-karma", function(done) {
  18. var kamaServerOptions = {
  19. configFile: rootDir + "tests/validation/karma.conf.js",
  20. singleRun: false
  21. };
  22. var server = new karmaServer(kamaServerOptions, done);
  23. server.start();
  24. });
  25. /**
  26. * Launches the KARMA validation tests in ff or virtual screen ff on travis for a quick analysis during the build.
  27. */
  28. gulp.task("tests-validation-virtualscreen", function(done) {
  29. var kamaServerOptions = {
  30. configFile: rootDir + "tests/validation/karma.conf.js",
  31. singleRun: true,
  32. browsers: ['Firefox']
  33. };
  34. var server = new karmaServer(kamaServerOptions, done);
  35. server.start();
  36. });
  37. /**
  38. * Launches the KARMA validation tests in browser stack for remote and cross devices validation tests.
  39. */
  40. gulp.task("tests-validation-browserstack", function(done) {
  41. if (!process.env.BROWSER_STACK_USERNAME) {
  42. done();
  43. return;
  44. }
  45. // not in safe build
  46. if (process.env.BROWSER_STACK_USERNAME === "$(babylon.browserStack.userName)") {
  47. done();
  48. return;
  49. }
  50. var kamaServerOptions = {
  51. configFile: rootDir + "tests/validation/karma.conf.browserstack.js",
  52. singleRun: true
  53. };
  54. var server = new karmaServer(kamaServerOptions, done);
  55. server.start();
  56. });
  57. /**
  58. * Transpiles typescript unit tests.
  59. */
  60. gulp.task("tests-unit-transpile", function(done) {
  61. var tsProject = typescript.createProject(rootDir + "tests/unit/tsconfig.json");
  62. var tsResult = gulp.src(rootDir + "tests/unit/**/*.ts", { base: relativeRootDir })
  63. .pipe(tsProject());
  64. tsResult.once("error", function() {
  65. tsResult.once("finish", function() {
  66. console.log("Typescript compile failed");
  67. process.exit(1);
  68. });
  69. });
  70. return tsResult.js.pipe(gulp.dest(relativeRootDir));
  71. });
  72. /**
  73. * Launches the KARMA unit tests in Chrome.
  74. */
  75. gulp.task("tests-unit-debug", gulp.series("tests-unit-transpile", function(done) {
  76. var kamaServerOptions = {
  77. configFile: rootDir + "tests/unit/karma.conf.js",
  78. singleRun: false,
  79. browsers: ['Chrome']
  80. };
  81. var server = new karmaServer(kamaServerOptions, done);
  82. server.start();
  83. }));
  84. /**
  85. * Launches the KARMA unit tests in chrome headless.
  86. */
  87. gulp.task("tests-babylon-unit", gulp.series("tests-unit-transpile", function(done) {
  88. var kamaServerOptions = {
  89. configFile: rootDir + "tests/unit/karma.conf.js",
  90. singleRun: true
  91. };
  92. var server = new karmaServer(kamaServerOptions, done);
  93. server.start();
  94. }));
  95. /**
  96. * Transpiles viewer typescript unit tests.
  97. */
  98. gulp.task("tests-viewer-validation-transpile", function() {
  99. let wpBuild = webpackStream(require(relativeRootDir + 'Viewer/webpack.gulp.config.js'), webpack);
  100. // clean the built directory
  101. rmDir("../../Viewer/tests/build/");
  102. return wpBuild
  103. .pipe(rename(function(path) {
  104. if (path.extname === '.js') {
  105. path.basename = "test";
  106. }
  107. }))
  108. .pipe(gulp.dest("../../Viewer/tests/build/"));
  109. });
  110. /**
  111. * Launches the viewer's KARMA validation tests in chrome in order to debug them.
  112. * (Can only be launch locally.)
  113. */
  114. gulp.task("tests-viewer-validation-karma", gulp.series("tests-viewer-validation-transpile", function(done) {
  115. var kamaServerOptions = {
  116. configFile: rootDir + "Viewer/tests/validation/karma.conf.js",
  117. singleRun: false
  118. };
  119. var server = new karmaServer(kamaServerOptions, done);
  120. server.start();
  121. }));
  122. /**
  123. * Launches the KARMA validation tests in ff or virtual screen ff on travis for a quick analysis during the build.
  124. * (Can only be launch on any branches.)
  125. */
  126. gulp.task("tests-viewer-validation-virtualscreen", gulp.series("tests-viewer-validation-transpile", function(done) {
  127. var kamaServerOptions = {
  128. configFile: rootDir + "Viewer/tests/validation/karma.conf.js",
  129. singleRun: true,
  130. browsers: ['Firefox']
  131. };
  132. var server = new karmaServer(kamaServerOptions, done);
  133. server.start();
  134. }));
  135. /**
  136. * Launches the KARMA validation tests in browser stack for remote and cross devices validation tests.
  137. * (Can only be launch from secure branches.)
  138. */
  139. gulp.task("tests-viewer-validation-browserstack", gulp.series("tests-viewer-validation-transpile", function(done) {
  140. if (!process.env.BROWSER_STACK_USERNAME) {
  141. done();
  142. return;
  143. }
  144. var kamaServerOptions = {
  145. configFile: rootDir + "Viewer/tests/validation/karma.conf.browserstack.js",
  146. singleRun: true
  147. };
  148. var server = new karmaServer(kamaServerOptions, done);
  149. server.start();
  150. }));
  151. /**
  152. * Transpiles viewer typescript unit tests.
  153. */
  154. gulp.task("tests-viewer-transpile", function() {
  155. let wpBuild = webpackStream(require(relativeRootDir + 'Viewer/tests/unit/webpack.config.js'), webpack);
  156. // clean the built directory
  157. rmDir("../../Viewer/tests/build/");
  158. return wpBuild
  159. .pipe(rename(function(path) {
  160. if (path.extname === '.js') {
  161. path.basename = "test";
  162. }
  163. }))
  164. .pipe(gulp.dest("../../Viewer/tests/build/"));
  165. });
  166. /**
  167. * Launches the KARMA unit tests in chrome.
  168. * (Can be launch on any branches.)
  169. */
  170. gulp.task("tests-viewer-unit-debug", gulp.series("tests-viewer-transpile", function(done) {
  171. var kamaServerOptions = {
  172. configFile: rootDir + "Viewer/tests/karma.conf.js",
  173. singleRun: false,
  174. browsers: ['Chrome']
  175. };
  176. var server = new karmaServer(kamaServerOptions, done);
  177. server.start();
  178. }));
  179. /**
  180. * Launches the KARMA unit tests in chrome headless.
  181. */
  182. gulp.task("tests-viewer-unit", gulp.series("tests-viewer-transpile", function(done) {
  183. var kamaServerOptions = {
  184. configFile: rootDir + "Viewer/tests/karma.conf.js",
  185. singleRun: true
  186. };
  187. var server = new karmaServer(kamaServerOptions, done);
  188. server.start();
  189. }));
  190. /**
  191. * Launches the KARMA unit tests in chrome headless.
  192. */
  193. gulp.task("tests-unit", gulp.series("tests-babylon-unit", "tests-viewer-unit"));
  194. /**
  195. * Launches the KARMA module tests in chrome headless.
  196. */
  197. gulp.task("tests-modules", function() {
  198. let testsToRun = require(relativeRootDir + 'tests/modules/tests.json');
  199. let sequencePromise = Promise.resolve();
  200. testsToRun.tests.forEach(test => {
  201. sequencePromise = sequencePromise.then(() => {
  202. console.log("Running " + test.name);
  203. let basePath = relativeRootDir + 'tests/modules/' + test.name + '/';
  204. rmDir("../../tests/modules/build/");
  205. let compilePromise = Promise.resolve();
  206. if (test.dependencies) {
  207. compilePromise = new Promise(function(resolve, reject) {
  208. let counter = 0;
  209. let copyTask = gulp.src(test.dependencies.map(dep => config.build.outputDirectory + '/' + dep)).pipe(rename(function(path) {
  210. path.basename = (counter++) + '';
  211. })).pipe(gulp.dest("../../tests/modules/build/dependencies/"))
  212. copyTask.once("finish", resolve);
  213. })
  214. }
  215. // any compilation needed?
  216. if (test.typescript || test.bundler) {
  217. //typescript only
  218. if (test.typescript && !test.bundler) {
  219. compilePromise = compilePromise.then(() => {
  220. return new Promise(function(resolve, reject) {
  221. var gulpRelativeRootDir = "../../";
  222. var gulpBasePath = gulpRelativeRootDir + 'tests/modules/' + test.name + '/';
  223. var tsProject = typescript.createProject(gulpBasePath + (test.tsconfig || 'tsconfig.json'));
  224. var tsResult = gulp.src(gulpBasePath + '/src/**/*.ts', { base: gulpBasePath })
  225. .pipe(tsProject());
  226. let error = false;
  227. tsResult.once("error", function() {
  228. error = true;
  229. });
  230. let jsPipe = tsResult.js.pipe(gulp.dest(gulpRelativeRootDir + "tests/modules/"));
  231. jsPipe.once("finish", function() {
  232. if (error)
  233. reject('error compiling test');
  234. else
  235. resolve();
  236. });
  237. });
  238. });
  239. } else {
  240. if (test.bundler === 'webpack') {
  241. console.log("webpack");
  242. compilePromise = compilePromise.then(() => {
  243. return new Promise(function(resolve, reject) {
  244. let wpBuild = webpackStream(require(basePath + '/webpack.config.js'), webpack);
  245. wpBuild = wpBuild
  246. .pipe(rename(function(path) {
  247. if (path.extname === '.js') {
  248. path.basename = "tests-loader";
  249. }
  250. }))
  251. .pipe(gulp.dest("../../tests/modules/build/"));
  252. wpBuild.once("finish", resolve);
  253. })
  254. });
  255. }
  256. }
  257. }
  258. return compilePromise.then(() => {
  259. return new Promise(function(resolve, reject) {
  260. var kamaServerOptions = {
  261. configFile: rootDir + "tests/modules/karma.conf.js",
  262. junitReporter: {
  263. outputDir: '.temp/testResults', // results will be saved as $outputDir/$browserName.xml
  264. outputFile: test.reportName + '.xml', // if included, results will be saved as $outputDir/$browserName/$outputFile
  265. suite: test.displayName, // suite will become the package name attribute in xml testsuite element
  266. useBrowserName: false, // add browser name to report and classes names
  267. nameFormatter: undefined, // function (browser, result) to customize the name attribute in xml testcase element
  268. classNameFormatter: undefined, // function (browser, result) to customize the classname attribute in xml testcase element
  269. properties: {} // key value pair of properties to add to the <properties> section of the report
  270. },
  271. };
  272. var server = new karmaServer(kamaServerOptions, (err) => {
  273. if (err) {
  274. reject(err);
  275. }
  276. else {
  277. resolve();
  278. }
  279. });
  280. server.start();
  281. });
  282. })
  283. })
  284. });
  285. return sequencePromise;
  286. });