Skip to content

Commit 22017a5

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

22 files changed

+386
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.vscode
33

44
node_modules
5+
.tmp

README.md

-3
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

+7
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

+24
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

+39
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

+15
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

+13
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

+20
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

+32
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

+50
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;

lib/testPath.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
var isWindows = window.navigator.appVersion.includes('Win') || false;
4+
function tmpTestName(testFile) {
5+
var testPath = path_1.join(__dirname, '..', '.tmp', testFile + '.js');
6+
if (isWindows) {
7+
testPath = testPath.split('/').join('\\');
8+
}
9+
return testPath;
10+
}
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
exports.default = tmpTestName;

lib/writeTests/index.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
var fs_1 = require('fs');
3+
var path_1 = require('path');
4+
var js_coderoad_1 = require('js-coderoad');
5+
var tmpPath = path_1.join(__dirname, '..', '..', '.tmp');
6+
function writeTest(_a) {
7+
var dir = _a.dir, tests = _a.tests, testPath = _a.testPath;
8+
var output = js_coderoad_1.default({ dir: dir, content: tests });
9+
writeTestFile(testPath, output);
10+
}
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
exports.default = writeTest;
13+
function writeTestFile(testPath, output) {
14+
if (!fs_1.existsSync(tmpPath)) {
15+
fs_1.mkdirSync(tmpPath);
16+
}
17+
return new Promise(function (resolve, reject) {
18+
fs_1.writeFile(testPath, output, function (err) {
19+
if (err) {
20+
reject(err);
21+
}
22+
resolve();
23+
});
24+
});
25+
}

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "jest test runner for coderoad",
55
"main": "lib/index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "jest"
88
},
99
"repository": {
1010
"type": "git",
@@ -22,8 +22,12 @@
2222
"homepage": "https://github.com/coderoad/jest-coderoad#readme",
2323
"dependencies": {
2424
"babel-jest": "^15.0.0",
25+
"babel-preset-es2015": "^6.14.0",
2526
"jest": "^15.1.1",
2627
"js-coderoad": "^0.2.1",
2728
"node-file-exists": "1.1.0"
29+
},
30+
"babel": {
31+
"presets": ["es2015"]
2832
}
2933
}

src/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
export const signal = '@@@CodeRoad Results@@@';
44

55
// path to test runner executable from "node_modules"
6-
export const runnerPath = ['jest', 'bin', 'jest'];
6+
export const runnerPath = ['jest', 'bin', 'jest.js'];
77

88
// options passed in the runner command process
99
export const runnerOptions = [

src/runner/paths/runner.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ const flattenedPath = [__dirname, '..', '..', '..', 'node_modules'].concat(runne
88

99
export default function getRunner(): string {
1010
// runner, location may differ based on NPM version
11-
if (fileExists(join(...nestedPath))) {
12-
return join(...nestedPath);
13-
} else if (fileExists(join(...flattenedPath))) {
14-
return join(...flattenedPath);
11+
const nested = join.apply(this, nestedPath);
12+
const flattened = join.apply(this, flattenedPath);
13+
14+
console.log('nested: ', nested, 'flattened: ', flattened);
15+
16+
if (fileExists(nested)) {
17+
return nested;
18+
} else if (fileExists(flattened)) {
19+
return flattened;
1520
}
1621
throw new Error('Error finding test runner.');
1722
}

test/examples/code/fail-first.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function addOne(x) {
2+
return x;
3+
}

test/examples/code/pass-all.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function addOne(x) {
2+
return x + 1;
3+
}
4+
5+
function subtractOne(x) {
6+
return x - 1;
7+
}

test/examples/code/pass-first-only.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function addOne(x) {
2+
return x + 1;
3+
}
4+
5+
function subtractOne(x) {
6+
return x;
7+
}

test/examples/test/basic.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
(function(){'use strict';
3+
const _resolve = require('path').resolve;
4+
require('babel-register')({plugins:[['transform-es2015-modules-commonjs',{loose:true,sourceRoot:_resolve(__dirname,'..','test')}]]});
5+
function getType(e){switch(Object.prototype.toString.call(e)){case"[object Array]":return"array";case"[object Date]":return"date";case"[object Null]":return"null";case"[object Number]":return e!==e?"NaN":"number"}return typeof e}function assignTypes(e){return JSON.stringify(e.map(function(e){var t=getType(e);switch(t){case"object":case"array":e=JSON.stringify(e),e=util.inspect(e,inspectOptions),e=e.substring(1,e.length-1);break;case"undefined":case"null":case"NaN":return{type:t}}return{type:t,output:e}}))}var _this=this,util=require("util"),inspectOptions={depth:null};if(console&&console.log){var originalLog_1=console.log;console.log=function(){for(var e=[],t=0;t<arguments.length;t++)e[t-0]=arguments[t];(new Error).stack;setTimeout(originalLog_1.apply(_this,[assignTypes(e)]))}}
6+
let _fileExists = require('node-file-exists').default;
7+
global.exists = (p) => _fileExists(_resolve(__dirname,'..','test', p));
8+
require = require('rewire-coderoad');
9+
10+
// unit tests
11+
12+
// REPLACE_WITH_PATH_TO_CODE
13+
14+
describe('01 pass', () => {
15+
16+
it('should be true', () => {
17+
expect(true).toBe(true);
18+
});
19+
20+
});
21+
22+
describe('02 pass', () => {
23+
24+
it('should pass', () => {
25+
expect(true).toBe(true);
26+
});
27+
28+
});
29+
30+
31+
32+
}());

0 commit comments

Comments
 (0)