test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { join } from 'path';
  2. import { dest, src, task } from 'gulp';
  3. import { DIST_VENDOR_ROOT, NPM_VENDOR_FILES, PROJECT_ROOT, SCRIPTS_ROOT } from '../constants';
  4. task('test', ['test.assembleVendorJs', 'compile.karma'], (done: Function) => {
  5. karmaTest(false, done);
  6. });
  7. task('test.fast', ['compile.karma'], (done: Function) => {
  8. karmaTest(false, done);
  9. });
  10. task('test.watch', ['test.assembleVendorJs', 'compile.karma'], (done: Function) => {
  11. karmaTest(true, done);
  12. });
  13. task('test.coverage', ['test.assembleVendorJs', 'compile.karma'], (done: Function) => {
  14. karmaTest(false, () => {
  15. createKarmaCoverageReport(done);
  16. });
  17. });
  18. task('test.imageserver', () => {
  19. const http = require('http');
  20. const url = require('url');
  21. const port = 8900;
  22. const requestedUrls = [];
  23. let start = Date.now();
  24. function handleRequest(req, res) {
  25. const urlParse = url.parse(req.url, true);
  26. res.setHeader('Access-Control-Allow-Origin', '*');
  27. res.setHeader('Access-Control-Allow-Methods', 'GET');
  28. res.setHeader('Connection', 'keep-alive');
  29. res.setHeader('Age', '0');
  30. res.setHeader('cache-control', 'no-store');
  31. if (urlParse.pathname === '/reset') {
  32. console.log('Image Server Reset');
  33. console.log('---------------------------');
  34. requestedUrls.length = 0;
  35. start = Date.now();
  36. res.setHeader('Content-Type', 'text/plain');
  37. res.end('reset');
  38. return;
  39. }
  40. const delay = urlParse.query.d || 1000;
  41. const id = urlParse.query.id || Math.round(Math.random() * 1000);
  42. const width = urlParse.query.w || 80;
  43. const height = urlParse.query.h || 80;
  44. const color = urlParse.query.c || 'yellow';
  45. requestedUrls.push(req.url);
  46. console.log(`id: ${id}, requested: ${requestedUrls.filter(f => f === req.url).length}, timestamp: ${Date.now() - start}`);
  47. setTimeout(() => {
  48. res.setHeader('Content-Type', 'image/svg+xml');
  49. res.end(`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
  50. viewBox="0 0 ${width} ${height}" style="background-color: ${color};">
  51. <text x="5" y="22" style="font-family: Courier; font-size: 24px">${id}</text>
  52. </svg>`);
  53. }, delay);
  54. }
  55. http.globalAgent.maxSockets = 1;
  56. http.createServer(handleRequest).listen(port, () => {
  57. console.log(` Mock image server listening on: http://localhost:${port}/?d=2000&id=99`);
  58. console.log(` Possible querystrings:`);
  59. console.log(` id: the text to go in the svg image, defaults to a random number`);
  60. console.log(` d: how many milliseconds it should take to respond, defaults to 1000`);
  61. console.log(` w: image width, defaults to 80`);
  62. console.log(` h: image height, defaults to 80`);
  63. console.log(` c: image color, defaults to yellow`);
  64. });
  65. });
  66. function karmaTest(watch: boolean, done: Function) {
  67. const karma = require('karma');
  68. const argv = require('yargs').argv;
  69. let karmaConfig = {
  70. configFile: join(SCRIPTS_ROOT, 'karma/karma.conf.js'),
  71. singleRun: true,
  72. };
  73. if (watch) {
  74. (karmaConfig as any).singleRun = false;
  75. }
  76. if (argv.testGrep) {
  77. (<any>karmaConfig).client = {
  78. args: ['--grep', argv.testGrep]
  79. };
  80. }
  81. if (typeof argv.debug !== 'undefined') {
  82. karmaConfig.singleRun = false;
  83. }
  84. new karma.Server(karmaConfig, done).start();
  85. }
  86. task('test.assembleVendorJs', () => {
  87. const files = NPM_VENDOR_FILES.map((root) => {
  88. const glob = join(root, '**/*.+(js|js.map)');
  89. return src(join('node_modules', glob))
  90. .pipe(dest(join(DIST_VENDOR_ROOT, root)));
  91. });
  92. const gulpMerge = require('merge2');
  93. return gulpMerge(files);
  94. });
  95. /* creates a karma code coverage report */
  96. function createKarmaCoverageReport(done: Function) {
  97. console.log('Generating Unit Test Coverage Report...');
  98. let exec = require('child_process').exec;
  99. let command = `node_modules/.bin/remap-istanbul -i coverage/coverage-final.json -o coverage -t html`;
  100. exec(command, function(err: any, stdout: any, stderr: any) {
  101. console.log(`file://${PROJECT_ROOT}/coverage/index.html`);
  102. done(err);
  103. });
  104. }