Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fa27d0e

Browse files
committedSep 22, 2016
refactor mocha specific code into constants
1 parent aded22d commit fa27d0e

File tree

9 files changed

+84
-44
lines changed

9 files changed

+84
-44
lines changed
 

‎.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
.vscode
2+
.DS_Store
3+
14
node_modules
25
src/*.js
36
test/**/*.ts
4-
.tmp/*.js
7+
.tmp/*.js

‎lib/constants.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
"use strict";
2+
var path_1 = require('path');
3+
var reporterPath = path_1.join(__dirname, 'reporter', 'index.js');
24
exports.signal = '@@@CodeRoad Results@@@';
5+
exports.runnerPath = ['mocha', 'bin', 'mocha'];
6+
exports.runnerOptions = [
7+
'--bail',
8+
'--harmony',
9+
'--no-colors',
10+
'--timeout=3000',
11+
'--compilers js:babel-register',
12+
("--reporter=" + reporterPath),
13+
];

‎lib/runner/paths/runner.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
var node_file_exists_1 = require('node-file-exists');
3+
var path_1 = require('path');
4+
var constants_1 = require('../../constants');
5+
var nestedPath = [__dirname, '..', '..', '..', '..'].concat(constants_1.runnerPath);
6+
var flattenedPath = [__dirname, '..', '..', '..', 'node_modules'].concat(constants_1.runnerPath);
7+
function getRunner() {
8+
var nested = path_1.join.apply(this, nestedPath);
9+
var flattened = path_1.join.apply(this, flattenedPath);
10+
if (node_file_exists_1.default(nested)) {
11+
return nested;
12+
}
13+
else if (node_file_exists_1.default(flattened)) {
14+
return flattened;
15+
}
16+
throw new Error('Error finding test runner.');
17+
}
18+
Object.defineProperty(exports, "__esModule", { value: true });
19+
exports.default = getRunner;

‎lib/runner/runner-process.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"use strict";
22
var path_1 = require('path');
33
var child_process_1 = require('child_process');
4-
var mocha_1 = require('./paths/mocha');
4+
var constants_1 = require('../constants');
5+
var runner_1 = require('./paths/runner');
56
var node_1 = require('./paths/node');
6-
var reporterPath = path_1.join(__dirname, '..', 'reporter', 'index.js');
77
var node = node_1.default();
8-
var mocha = mocha_1.default();
8+
var runner = runner_1.default();
99
function spawnRunnerProcess(_a) {
1010
var dir = _a.dir, taskPosition = _a.taskPosition, testPath = _a.testPath;
1111
var options = {
@@ -20,16 +20,9 @@ function spawnRunnerProcess(_a) {
2020
TASK_POSITION: taskPosition,
2121
NODE_PATH: path_1.join(dir, 'node_modules'),
2222
});
23-
return child_process_1.spawn(node, [
24-
mocha,
25-
'--bail',
26-
'--harmony',
27-
'--no-colors',
28-
'--timeout=3000',
29-
'--compilers js:babel-register',
30-
("--reporter=" + reporterPath),
31-
testPath,
32-
], options);
23+
return child_process_1.spawn(node, [runner]
24+
.concat(constants_1.runnerOptions)
25+
.concat(testPath), options);
3326
}
3427
Object.defineProperty(exports, "__esModule", { value: true });
3528
exports.default = spawnRunnerProcess;

‎src/constants.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
import { join } from 'path';
2+
3+
const reporterPath = join(__dirname, 'reporter', 'index.js');
4+
15
// test results are taken after this signal
26
// this helps to avoid parsing console.logs before the output
37
export const signal = '@@@CodeRoad Results@@@';
8+
9+
// path to test runner executable from "node_modules"
10+
export const runnerPath = ['mocha', 'bin', 'mocha'];
11+
12+
// options passed in the runner command process
13+
export const runnerOptions = [
14+
'--bail', // quit on first fail
15+
'--harmony', // es6 features
16+
'--no-colors',
17+
'--timeout=3000',
18+
'--compilers js:babel-register', // for import/export of modules
19+
`--reporter=${reporterPath}`, // test feedback
20+
];

‎src/runner/paths/mocha.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎src/runner/paths/runner.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import fileExists from 'node-file-exists';
2+
import { join } from 'path';
3+
import { runnerPath } from '../../constants';
4+
5+
const nestedPath = [__dirname, '..', '..', '..', '..'].concat(runnerPath);
6+
const flattenedPath = [__dirname, '..', '..', '..', 'node_modules'].concat(runnerPath);
7+
8+
export default function getRunner(): string {
9+
// runner, location may differ based on NPM version
10+
const nested = join.apply(this, nestedPath);
11+
const flattened = join.apply(this, flattenedPath);
12+
13+
if (fileExists(nested)) {
14+
return nested;
15+
} else if (fileExists(flattened)) {
16+
return flattened;
17+
}
18+
throw new Error('Error finding test runner.');
19+
}

‎src/runner/runner-process.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { join } from 'path';
22
import { spawn } from 'child_process';
33

4-
import getMocha from './paths/mocha';
4+
import { runnerOptions } from '../constants';
5+
import getRunner from './paths/runner';
56
import getNode from './paths/node';
67

7-
const reporterPath = join(__dirname, '..', 'reporter', 'index.js');
88
const node = getNode();
9-
const mocha = getMocha();
9+
const runner = getRunner();
1010

1111
export default function spawnRunnerProcess({dir, taskPosition, testPath}) {
1212
// node electron setup
@@ -25,15 +25,8 @@ export default function spawnRunnerProcess({dir, taskPosition, testPath}) {
2525
});
2626

2727
// spawn child process calling mocha test runner
28-
return spawn(node, [
29-
// into shared node_modules directory
30-
mocha,
31-
'--bail', // quit on first fail
32-
'--harmony', // es6 features
33-
'--no-colors',
34-
'--timeout=3000',
35-
'--compilers js:babel-register', // for import/export of modules
36-
`--reporter=${reporterPath}`, // test feedback
37-
testPath, // unit tests
38-
], options);
28+
return spawn(node, [runner]
29+
.concat(runnerOptions)
30+
.concat(testPath) // unit tests
31+
, options);
3932
}

‎tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"src/testPath.ts",
2222
"src/reporter/index.ts",
2323
"src/runner/index.ts",
24-
"src/runner/paths/mocha.ts",
24+
"src/runner/paths/runner.ts",
2525
"src/runner/paths/node.ts",
2626
"src/runner/runner-process.ts",
2727
"src/runner/start-runner.ts",

0 commit comments

Comments
 (0)
Please sign in to comment.