Skip to content

Commit 22017a5

Browse files
committed
create first test and lib dir
1 parent ed2181f commit 22017a5

File tree

22 files changed

+386
-11
lines changed

22 files changed

+386
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.vscode
33

44
node_modules
5+
.tmp

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
# Jest CodeRoad (WIP)
22

33
[Atom-CodeRoad](https://github.com/coderoad/atom-coderoad) Javascript test runner & reporter.
4-
5-
6-

lib/constants.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
exports.signal = '@@@CodeRoad Results@@@';
3+
exports.runnerPath = ['jest', 'bin', 'jest.js'];
4+
exports.runnerOptions = [
5+
'--bail',
6+
'--notify'
7+
];

lib/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use strict";
2+
var writeTests_1 = require('./writeTests');
3+
var runner_1 = require('./runner');
4+
var testPath_1 = require('./testPath');
5+
function load(_a) {
6+
var dir = _a.dir, testFile = _a.testFile, tests = _a.tests;
7+
writeTests_1.default({
8+
dir: dir,
9+
tests: tests,
10+
testPath: testPath_1.default(testFile),
11+
});
12+
}
13+
exports.load = load;
14+
;
15+
function run(_a) {
16+
var dir = _a.dir, taskPosition = _a.taskPosition, handleResult = _a.handleResult, testFile = _a.testFile;
17+
runner_1.default({
18+
dir: dir,
19+
taskPosition: taskPosition,
20+
handleResult: handleResult,
21+
testPath: testPath_1.default(testFile),
22+
});
23+
}
24+
exports.run = run;

lib/reporter/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"use strict";
2+
var constants_1 = require('../constants');
3+
exports = module.exports = reporter;
4+
function reporter(runner) {
5+
var result = {
6+
passes: [],
7+
failures: [],
8+
pass: true,
9+
};
10+
runner.on('pass', function (test) {
11+
var index = getIndexAndTitle(test.fullTitle()).index;
12+
result.passes.push({
13+
msg: "Task " + index + " Complete",
14+
taskPosition: index,
15+
});
16+
});
17+
runner.on('fail', function (test, err) {
18+
var _a = getIndexAndTitle(test.fullTitle()), msg = _a.msg, index = _a.index;
19+
result.failures.push({
20+
msg: msg,
21+
taskPosition: index - 1,
22+
timedOut: test.timedOut,
23+
});
24+
result.pass = false;
25+
});
26+
runner.on('end', function () {
27+
process.stdout.write(constants_1.signal + JSON.stringify(result, null, 2));
28+
});
29+
}
30+
function getIndexAndTitle(title) {
31+
var indexString = title.match(/^[0-9]+/);
32+
if (!indexString) {
33+
throw 'Tests should begin with a number, indicating the task number';
34+
}
35+
return {
36+
index: parseInt(indexString[0], 10),
37+
msg: title.slice(indexString[0].length + 1),
38+
};
39+
}

lib/runner/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
var start_runner_1 = require('./start-runner');
3+
var runner_process_1 = require('./runner-process');
4+
var isWindows = window.navigator.appVersion.indexOf('Win') > -1;
5+
function runner(_a) {
6+
var dir = _a.dir, taskPosition = _a.taskPosition, handleResult = _a.handleResult, testPath = _a.testPath;
7+
if (isWindows) {
8+
testPath = testPath.split('\\').join('\\\\');
9+
testPath = testPath.split('/').join('\\\\');
10+
}
11+
var runner = runner_process_1.default({ dir: dir, taskPosition: taskPosition, testPath: testPath });
12+
return start_runner_1.default({ runner: runner, handleResult: handleResult, taskPosition: taskPosition });
13+
}
14+
Object.defineProperty(exports, "__esModule", { value: true });
15+
exports.default = runner;

lib/runner/paths/node.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
function getNode() {
4+
if (process.platform === 'darwin' && process.resourcesPath) {
5+
return path_1.join(process.resourcesPath, '..', 'Frameworks', 'Atom Helper.app', 'Contents', 'MacOS', 'Atom Helper');
6+
}
7+
else if (process.platform.match(/win/)) {
8+
return 'node';
9+
}
10+
return process.execPath;
11+
}
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
exports.default = getNode;

lib/runner/paths/runner.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
console.log('nested: ', nested, 'flattened: ', flattened);
11+
if (node_file_exists_1.default(nested)) {
12+
return nested;
13+
}
14+
else if (node_file_exists_1.default(flattened)) {
15+
return flattened;
16+
}
17+
throw new Error('Error finding test runner.');
18+
}
19+
Object.defineProperty(exports, "__esModule", { value: true });
20+
exports.default = getRunner;

lib/runner/runner-process.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
var child_process_1 = require('child_process');
4+
var runner_1 = require('./paths/runner');
5+
var node_1 = require('./paths/node');
6+
var constants_1 = require('../constants');
7+
var reporterPath = path_1.join(__dirname, '..', 'reporter', 'index.js');
8+
var node = node_1.default();
9+
var runner = runner_1.default();
10+
function spawnRunnerProcess(_a) {
11+
var dir = _a.dir, taskPosition = _a.taskPosition, testPath = _a.testPath;
12+
var options = {
13+
cwd: dir
14+
};
15+
if (options.env == null) {
16+
options.env = Object.create(process.env);
17+
}
18+
Object.assign(options.env, {
19+
ATOM_SHELL_INTERNAL_RUN_AS_NODE: 1,
20+
DIR: dir,
21+
TASK_POSITION: taskPosition,
22+
NODE_PATH: path_1.join(dir, 'node_modules'),
23+
});
24+
return child_process_1.spawn(node, [
25+
runner,
26+
("--reporter=" + reporterPath)
27+
]
28+
.concat(constants_1.runnerOptions)
29+
.concat(testPath), options);
30+
}
31+
Object.defineProperty(exports, "__esModule", { value: true });
32+
exports.default = spawnRunnerProcess;

lib/runner/start-runner.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"use strict";
2+
var process_console_log_1 = require('process-console-log');
3+
var constants_1 = require('../constants');
4+
var signalMatch = new RegExp(constants_1.signal);
5+
function startRunner(_a) {
6+
var runner = _a.runner, handleResult = _a.handleResult, taskPosition = _a.taskPosition;
7+
var final = null;
8+
new Promise(function run(resolve, reject) {
9+
runner.stdout.on('data', function onData(data) {
10+
data = data.toString();
11+
var match = signalMatch.exec(data);
12+
if (!match) {
13+
try {
14+
process_console_log_1.parseLog(data);
15+
}
16+
catch (e) {
17+
process_console_log_1.parseLog(data);
18+
}
19+
return;
20+
}
21+
var resultString = data.substring(match.index + constants_1.signal.length);
22+
var result = JSON.parse(JSON.stringify(resultString));
23+
if (typeof result === 'string') {
24+
result = JSON.parse(result);
25+
}
26+
switch (result.pass) {
27+
case true:
28+
final = result.passes[result.passes.length - 1];
29+
break;
30+
case false:
31+
final = result.failures[0];
32+
break;
33+
default:
34+
console.log('error processing result: ', result);
35+
}
36+
final.change = final.taskPosition - taskPosition;
37+
final.pass = final.change > 0;
38+
final.completed = result.pass;
39+
handleResult(final);
40+
});
41+
runner.stderr.on('data', function onError(data) {
42+
console.log('test error', data.toString());
43+
});
44+
runner.on('close', function onClose(code) {
45+
resolve(final);
46+
});
47+
});
48+
}
49+
Object.defineProperty(exports, "__esModule", { value: true });
50+
exports.default = startRunner;

0 commit comments

Comments
 (0)