Skip to content

Commit ed2181f

Browse files
committed
replicate mocha-coderoad with jest config
1 parent 3cd1c96 commit ed2181f

File tree

27 files changed

+3196
-7
lines changed

27 files changed

+3196
-7
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_STORE
2+
.vscode
3+
4+
node_modules

.npmignore

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

.vscode/cSpell.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// cSpell Settings
2+
{
3+
4+
// Version of the setting file. Always 0.1
5+
"version": "0.1",
6+
7+
// language - current active spelling language
8+
"language": "en",
9+
10+
// words - list of words to be always considered correct
11+
"words": [
12+
"coderoad"
13+
],
14+
15+
// flagWords - list of words to be always considered incorrect
16+
// This is useful for offensive words and common spelling errors.
17+
// For example "hte" should be "the"
18+
"flagWords": [
19+
"hte"
20+
]
21+
}

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,11 @@
1919
"bugs": {
2020
"url": "https://github.com/coderoad/jest-coderoad/issues"
2121
},
22-
"homepage": "https://github.com/coderoad/jest-coderoad#readme"
22+
"homepage": "https://github.com/coderoad/jest-coderoad#readme",
23+
"dependencies": {
24+
"babel-jest": "^15.0.0",
25+
"jest": "^15.1.1",
26+
"js-coderoad": "^0.2.1",
27+
"node-file-exists": "1.1.0"
28+
}
2329
}

src/constants.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// test results are taken after this signal
2+
// this helps to avoid parsing console.logs before the output
3+
export const signal = '@@@CodeRoad Results@@@';
4+
5+
// path to test runner executable from "node_modules"
6+
export const runnerPath = ['jest', 'bin', 'jest'];
7+
8+
// options passed in the runner command process
9+
export const runnerOptions = [
10+
'--bail', // quit on first fail
11+
'--notify'
12+
];

src/elements.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
- runner name
2+
- runner path
3+
- capture output
4+
5+
- js-coderoad
6+
- jest
7+
8+
9+
npm install -g jest-cli

src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import writeTests from './writeTests';
2+
import runner from './runner';
3+
import getTestPath from './testPath';
4+
5+
export function load({ dir, testFile, tests }) {
6+
writeTests({
7+
dir,
8+
tests,
9+
testPath: getTestPath(testFile),
10+
});
11+
};
12+
13+
export function run({ dir, taskPosition, handleResult, testFile }) {
14+
runner({
15+
dir,
16+
taskPosition,
17+
handleResult,
18+
testPath: getTestPath(testFile),
19+
});
20+
}

src/reporter/index.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { signal } from '../constants';
2+
3+
exports = module.exports = reporter;
4+
function reporter(runner) {
5+
let result = {
6+
passes: [],
7+
failures: [],
8+
pass: true,
9+
};
10+
11+
runner.on('pass', (test) => {
12+
const {index} = getIndexAndTitle(test.fullTitle());
13+
// add pass
14+
result.passes.push({
15+
msg: `Task ${index} Complete`,
16+
taskPosition: index,
17+
});
18+
});
19+
20+
runner.on('fail', (test, err: Error) => {
21+
const {msg, index} = getIndexAndTitle(test.fullTitle());
22+
// add fail
23+
result.failures.push({
24+
msg,
25+
taskPosition: index - 1,
26+
// body: test.body,
27+
timedOut: test.timedOut,
28+
// duration: test.duration
29+
});
30+
result.pass = false;
31+
});
32+
33+
runner.on('end', function() {
34+
// anything before the signal will be captured as log
35+
process.stdout.write(signal + JSON.stringify(result, null, 2));
36+
});
37+
}
38+
39+
interface IndexTitle {
40+
index: number;
41+
msg: string;
42+
}
43+
44+
function getIndexAndTitle(title: string): IndexTitle {
45+
// tests prefixed with task number: "01 title"
46+
const indexString = title.match(/^[0-9]+/);
47+
if (!indexString) {
48+
throw 'Tests should begin with a number, indicating the task number';
49+
}
50+
return {
51+
index: parseInt(indexString[0], 10),
52+
msg: title.slice(indexString[0].length + 1),
53+
};
54+
}

src/runner/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import startRunner from './start-runner';
2+
import spawnRunnerProcess from './runner-process';
3+
4+
const isWindows = window.navigator.appVersion.indexOf('Win') > -1;
5+
6+
export default function runner({dir, taskPosition, handleResult, testPath}) {
7+
8+
if (isWindows) {
9+
testPath = testPath.split('\\').join('\\\\');
10+
testPath = testPath.split('/').join('\\\\');
11+
}
12+
13+
const runner = spawnRunnerProcess({dir, taskPosition, testPath});
14+
return startRunner({runner, handleResult, taskPosition});
15+
}

0 commit comments

Comments
 (0)