Skip to content

Commit

Permalink
feat: use create-jest-runner (#7)
Browse files Browse the repository at this point in the history
* chore: upgrade Jest and Babel to latest versions

* feat: use create-jest-runner
  • Loading branch information
SimenB authored and azz committed Sep 16, 2018
1 parent d16e78c commit ac2730f
Show file tree
Hide file tree
Showing 8 changed files with 2,194 additions and 1,643 deletions.
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ['@babel/preset-env'],
};
6 changes: 0 additions & 6 deletions jest.eslint.config.js

This file was deleted.

3 changes: 0 additions & 3 deletions jest.test.config.js

This file was deleted.

33 changes: 19 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,35 @@
},
"jest": {
"projects": [
"<rootDir>/jest.test.config.js",
"<rootDir>/jest.eslint.config.js"
{
"displayName": "test"
},
{
"runner": "jest-runner-eslint",
"displayName": "lint",
"testMatch": ["<rootDir>/**/*.js"],
"testPathIgnorePatterns": ["node_modules/"]
}
]
},
"devDependencies": {
"@babel/cli": "^7.0.0-beta.35",
"@babel/core": "^7.0.0-beta.35",
"@babel/preset-env": "^7.0.0-beta.35",
"babel-core": "^7.0.0-bridge.0",
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.1",
"@babel/preset-env": "^7.0.0",
"babel-core": "7.0.0-bridge.0",
"eslint": "^4.13.1",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-jest": "^21.5.0",
"eslint-plugin-prettier": "^2.4.0",
"jest": "^22.0.1",
"jest-runner-eslint": "^0.3.0",
"jest": "^23.6.0",
"jest-runner-eslint": "^0.6.0",
"prettier": "^1.9.2",
"typescript": "^2.6.2",
"semantic-release": "^8.2.0"
"semantic-release": "^8.2.0",
"typescript": "^2.6.2"
},
"dependencies": {
"@babel/code-frame": "7.0.0-beta.35",
"pify": "^3.0.0",
"throat": "^4.1.0",
"worker-farm": "^1.5.2"
"@babel/code-frame": "^7.0.0",
"create-jest-runner": "^0.4.0"
},
"repository": {
"type": "git",
Expand Down
87 changes: 2 additions & 85 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,3 @@
// Copied from https://github.com/jest-community/jest-runner-eslint/blob/2e2b65ef7bf698e501d6e175a210fd7f7b6bbffc/src/index.js
import { createJestRunner } from 'create-jest-runner';

const throat = require('throat');
const pify = require('pify');
const workerFarm = require('worker-farm');
const path = require('path');

const TEST_WORKER_PATH = path.join(__dirname, 'runTsc.js');

class CancelRun extends Error {
constructor(message) {
super(message);
this.name = 'CancelRun';
}
}

module.exports = class TscTestRunner {
constructor(globalConfig) {
this._globalConfig = globalConfig;
}

runTests(tests, watcher, onStart, onResult, onFailure) {
const farm = workerFarm(
{
autoStart: true,
maxConcurrentCallsPerWorker: 1,
maxConcurrentWorkers: this._globalConfig.maxWorkers,
maxRetries: 2, // Allow for a couple of transient errors.
},
TEST_WORKER_PATH
);

const mutex = throat(this._globalConfig.maxWorkers);
const worker = pify(farm);

const runTestInWorker = test =>
mutex(() => {
if (watcher.isInterrupted()) {
throw new CancelRun();
}
return onStart(test).then(() => {
return worker({
config: test.context.config,
globalConfig: this._globalConfig,
testPath: test.path,
rawModuleMap: watcher.isWatchMode()
? test.context.moduleMap.getRawModuleMap()
: null,
});
});
});

const onError = (err, test) => {
return onFailure(test, err).then(() => {
if (err.type === 'ProcessTerminatedError') {
// eslint-disable-next-line no-console
console.error(
'A worker process has quit unexpectedly! ' +
'Most likely this is an initialization error.'
);
process.exit(1);
}
});
};

const onInterrupt = new Promise((_, reject) => {
watcher.on('change', state => {
if (state.interrupted) {
reject(new CancelRun());
}
});
});

const runAllTests = Promise.all(
tests.map(test =>
runTestInWorker(test)
.then(testResult => onResult(test, testResult))
.catch(error => onError(error, test))
)
);

const cleanup = () => workerFarm.end(farm);

return Promise.race([runAllTests, onInterrupt]).then(cleanup, cleanup);
}
};
module.exports = createJestRunner(require.resolve('./runTsc'));
12 changes: 6 additions & 6 deletions src/runTsc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path');
const codeFrame = require('@babel/code-frame').codeFrameColumns;
const ts = require('typescript');
const fs = require('fs');
import path from 'path';
import { codeFrameColumns as codeFrame } from '@babel/code-frame';
import ts from 'typescript';
import fs from 'fs';

const appendCodeFrame = ({ filePath, errorMessage, location }) => {
if (typeof location === 'undefined') {
Expand Down Expand Up @@ -70,7 +70,7 @@ const runTsc = ({ testPath, config: jestConfig }, workerCallback) => {

const options = Object.assign({}, { noEmit: true }, settings.options);

const start = +new Date();
const start = Date.now();
const program = ts.createProgram([testPath], options);

const emitResult = program.emit();
Expand Down Expand Up @@ -124,7 +124,7 @@ const runTsc = ({ testPath, config: jestConfig }, workerCallback) => {
}
});

const end = +new Date();
const end = Date.now();
workerCallback(null, convertErrors({ start, end, errors, testPath }));
} catch (e) {
workerCallback(e);
Expand Down

0 comments on commit ac2730f

Please sign in to comment.