|
2 | 2 | 'use strict';
|
3 | 3 |
|
4 | 4 | /**
|
5 |
| - * Spawns the karam start command. |
| 5 | + * Spawns the karma test command. |
6 | 6 | * @name test
|
7 | 7 | */
|
8 | 8 | function test(command, argv) {
|
9 |
| - const path = require('path'); |
10 |
| - const spawn = require('cross-spawn'); |
11 |
| - |
12 |
| - const karmaConfigPath = path.resolve( |
13 |
| - __dirname, |
14 |
| - '..', |
15 |
| - 'config/karma/' + command + '.karma.conf.js' |
16 |
| - ); |
17 |
| - |
18 |
| - const flags = [ |
19 |
| - '--max-old-space-size=4096', |
20 |
| - 'node_modules/karma/bin/karma', |
21 |
| - 'start', |
22 |
| - karmaConfigPath, |
23 |
| - '--command', |
24 |
| - command |
25 |
| - ]; |
26 |
| - |
27 |
| - if (argv && argv.coverage === false) { |
28 |
| - flags.push('--no-coverage'); |
29 |
| - } else { |
30 |
| - flags.push('--coverage'); |
31 |
| - } |
32 |
| - |
33 |
| - const options = { |
34 |
| - stdio: 'inherit' |
| 9 | + const logger = require('../utils/logger'); |
| 10 | + const Server = require('karma').Server; |
| 11 | + const tsLinter = require('./utils/ts-linter'); |
| 12 | + const skyPagesConfigUtil = require('../config/sky-pages/sky-pages.config'); |
| 13 | + |
| 14 | + argv = argv || process.argv; |
| 15 | + argv.command = command; |
| 16 | + |
| 17 | + const karmaConfigUtil = require('karma').config; |
| 18 | + const karmaConfigPath = skyPagesConfigUtil.outPath(`config/karma/${command}.karma.conf.js`); |
| 19 | + const karmaConfig = karmaConfigUtil.parseConfig(karmaConfigPath); |
| 20 | + |
| 21 | + let lintResult; |
| 22 | + |
| 23 | + const onRunStart = () => { |
| 24 | + lintResult = tsLinter.lintSync(); |
| 25 | + }; |
| 26 | + |
| 27 | + const onRunComplete = () => { |
| 28 | + if (lintResult.exitCode > 0) { |
| 29 | + // Pull the logger out of the execution stream to let it print |
| 30 | + // after karma's coverage reporter. |
| 31 | + setTimeout(() => { |
| 32 | + logger.error('Process failed due to linting errors:'); |
| 33 | + lintResult.errors.forEach(error => logger.error(error)); |
| 34 | + }, 10); |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + const onExit = (exitCode) => { |
| 39 | + if (exitCode === 0) { |
| 40 | + exitCode = lintResult.exitCode; |
| 41 | + } |
| 42 | + |
| 43 | + logger.info(`Karma has exited with ${exitCode}.`); |
| 44 | + process.exit(exitCode); |
35 | 45 | };
|
36 | 46 |
|
37 |
| - // Pass our exitCode up |
38 |
| - const test = spawn('node', flags, options); |
39 |
| - test.on('exit', exitCode => process.exit(exitCode)); |
| 47 | + const server = new Server(karmaConfig, onExit); |
| 48 | + server.on('run_start', onRunStart); |
| 49 | + server.on('run_complete', onRunComplete); |
| 50 | + server.start(); |
40 | 51 | }
|
41 | 52 |
|
42 | 53 | module.exports = test;
|
0 commit comments