Skip to content

Commit 61761e8

Browse files
committed
WIP
1 parent 7edb0fd commit 61761e8

File tree

10 files changed

+413
-353
lines changed

10 files changed

+413
-353
lines changed

README.md

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,100 @@
1-
# jest-runner-mocha
1+
# create-jest-runner
22

3-
[![Build Status](https://travis-ci.org/rogeliog/jest-runner-mocha.svg?branch=master)](https://travis-ci.org/rogeliog/jest-runner-mocha) [![npm version](https://badge.fury.io/js/jest-runner-mocha.svg)](https://badge.fury.io/js/jest-runner-mocha)
3+
## Install
44

5-
An experimental Mocha runner for Jest
6-
7-
This makes it easy to integrate existing Mocha projects with Jest.
5+
```bash
6+
yarn add create-jest-runner
7+
```
88

9-
![runner](https://user-images.githubusercontent.com/574806/30088955-728bf97e-925e-11e7-9b25-6aac237085ca.gif)
9+
## Usage
1010

11+
create-jest-runner takes care of handling the appropriate parallelization and creating a worker farm for your runner.
1112

12-
## Usage
1313

14-
### Install
14+
You simply need two files:
15+
* Entry file: Used by Jest as an entrypoint to your runner.
16+
* Run file: Contains the domain logic of your runner.
1517

16-
Install `jest`_(it needs Jest 21+)_ and `jest-runner-mocha`
18+
### 1) Create your entry file
1719

18-
```bash
19-
yarn add --dev jest jest-runner-mocha
20+
```js
21+
// index.js
22+
const createRunner = require('create-jest-runner');
23+
module.exports = createRunner('/path/to/run.js')
24+
```
2025

21-
# or with NPM
26+
### 2) Create your run file
2227

23-
npm install --save-dev jest jest-runner-mocha
2428

29+
```js
30+
// run.js
31+
module.exports = (options, workerCallback) => {
32+
// ...
33+
}
2534
```
2635

27-
### Add it to your Jest config
36+
## Run File API
2837

29-
In your `package.json`
30-
```json
31-
{
32-
"jest": {
33-
"runner": "jest-runner-mocha"
38+
This file should export a function that receives two parameters `(options, workerCallback)`
39+
40+
### `options: { testPath, config, globalConfig }`
41+
- `testPath`: Path of the file that is going to be tests
42+
- `config`: Jest Project config used by this file
43+
- `globalConfig`: Jest global config
44+
45+
### `workerCallback: (error, testResult) => void`
46+
_Use this callback function to report back the results (needs to be called exactly one time)._
47+
- `error`: Any Javascript error or a string.
48+
- `testResult`: Needs to be an object of type https://github.com/facebook/jest/blob/master/types/TestResult.js#L131-L157
49+
50+
### Reporting test results
51+
52+
#### Passing test suite
53+
```js
54+
// run.js
55+
module.exports = (options, workerCallback) => {
56+
if (/* something */) {
57+
workerCallback(new Error('my message'));
3458
}
3559
}
3660
```
61+
#### Failing test suite
3762

38-
Or in `jest.config.js`
63+
### Reporting an error
64+
You can report other errors by calling the `workerCallback` with the appropriate error.
3965
```js
40-
module.exports = {
41-
runner: 'jest-runner-mocha',
66+
// run.js
67+
module.exports = (options, workerCallback) => {
68+
if (/* something */) {
69+
workerCallback(new Error('my message'));
70+
}
4271
}
4372
```
4473

45-
### Run Jest
46-
```bash
47-
yarn jest
48-
```
49-
50-
### Coverage
5174

52-
Coverage works outside of the box, simply `yarn jest -- --coverage`
5375

54-
You can also use other Jest options like [coveragePathIgnorePatterns](http://facebook.github.io/jest/docs/en/configuration.html#coveragepathignorepatterns-array-string) and [coverageReporters](http://facebook.github.io/jest/docs/en/configuration.html#coveragereporters-array-string)
5576

56-
## Custom config options
77+
## Add your runner to Jest config
5778

58-
Create a `jest-runner-mocha.config.js` at the `<rootDir>` or your Jest project.
79+
Once you have your Jest runner you can add it to your Jest config.
5980

60-
- `ui`: (Optional) the UI used by mocha
61-
```js
62-
// example
63-
module.exports = {
64-
ui: 'tdd',
81+
In your `package.json`
82+
```json
83+
{
84+
"jest": {
85+
"runner": "/path/to/my-runner"
86+
}
6587
}
6688
```
6789

68-
- `compiler`: (Optional) the used for adding a compile step to your mocha tests
69-
90+
Or in `jest.config.js`
7091
```js
71-
// example
7292
module.exports = {
73-
compiler: '/absolute/path/to/babel-register/or/other/compiler',
93+
runner: '/path/to/my-runner',
7494
}
7595
```
7696

77-
_NOTE: Eventually Jest will eventually have an option for configuring runners that will eliminate the need for `jest-runner-mocha.config.js`_
78-
79-
80-
## Known issues
81-
- It does not support any Mocha options except for `ui` and `compiler`
82-
- Support for compilers is very limited.
83-
- Does not support `jest --runInBand`
97+
### Run Jest
98+
```bash
99+
yarn jest
100+
```

Untitled-1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
docker tag docker/ucp-swarm:2.2.3 docker/ucp-swarm:2.2.4
2+
docker tag docker/ucp-metrics:2.2.3 docker/ucp-metrics:2.2.4
3+
docker tag docker/ucp-hrm:2.2.3 docker/ucp-hrm:2.2.4
4+
docker tag docker/ucp-etcd:2.2.3 docker/ucp-etcd:2.2.4
5+
docker tag docker/ucp-dsinfo:2.2.3 docker/ucp-dsinfo:2.2.4
6+
docker tag docker/ucp-compose:2.2.3 docker/ucp-compose:2.2.4
7+
docker tag docker/ucp-controller:2.2.3 docker/ucp-controller:2.2.4
8+
docker tag docker/ucp-auth-store:2.2.3 docker/ucp-auth-store:2.2.4
9+
docker tag docker/ucp:2.2.3 docker/ucp:2.2.4
10+
docker tag docker/ucp-cfssl:2.2.3 docker/ucp-cfssl:2.2.4
11+
docker tag docker/ucp-agent:2.2.3 docker/ucp-agent:2.2.4
12+
docker tag docker/ucp-auth:2.2.3 docker/ucp-auth:2.2.4

a.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.rogelio = 'edda';
2+
exports.default = 1;

lib/getRunner.js

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

lib/index.js

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,101 @@
1-
import getRunner from './getRunner';
1+
require('babel-polyfill');
2+
const throat = require('throat');
3+
const pify = require('pify');
4+
const workerFarm = require('worker-farm');
5+
const path = require('path');
26

3-
module.exports = {
4-
getRunner,
7+
const runPath = path.resolve(__dirname, 'run.js');
8+
9+
class CancelRun extends Error {
10+
constructor(message) {
11+
super(message);
12+
this.name = 'CancelRun';
13+
}
14+
}
15+
16+
const createRunner = (fn, { extraOptions } = {}) => {
17+
class BaseTestRunner {
18+
constructor(globalConfig) {
19+
this._globalConfig = globalConfig;
20+
}
21+
22+
// eslint-disable-next-line
23+
async runTests(tests, watcher, onStart, onResult, onFailure, options) {
24+
const farm = workerFarm(
25+
{
26+
autoStart: true,
27+
maxConcurrentCallsPerWorker: 1,
28+
maxConcurrentWorkers: this._globalConfig.maxWorkers,
29+
maxRetries: 2, // Allow for a couple of transient errors.
30+
},
31+
runPath,
32+
);
33+
34+
const mutex = throat(this._globalConfig.maxWorkers);
35+
const worker = pify(farm);
36+
37+
const runTestInWorker = test =>
38+
mutex(async () => {
39+
if (watcher.isInterrupted()) {
40+
throw new CancelRun();
41+
}
42+
await onStart(test);
43+
const baseOptions = {
44+
config: test.context.config,
45+
globalConfig: this._globalConfig,
46+
testPath: test.path,
47+
rawModuleMap: watcher.isWatchMode()
48+
? test.context.moduleMap.getRawModuleMap()
49+
: null,
50+
options,
51+
};
52+
53+
if (extraOptions) {
54+
return Object.assign(
55+
{},
56+
baseOptions,
57+
extraOptions(baseOptions, tests),
58+
);
59+
}
60+
61+
return worker(baseOptions);
62+
});
63+
64+
const onError = async (err, test) => {
65+
await onFailure(test, err);
66+
if (err.type === 'ProcessTerminatedError') {
67+
// eslint-disable-next-line no-console
68+
console.error(
69+
'A worker process has quit unexpectedly! ' +
70+
'Most likely this is an initialization error.',
71+
);
72+
process.exit(1);
73+
}
74+
};
75+
76+
const onInterrupt = new Promise((_, reject) => {
77+
watcher.on('change', state => {
78+
if (state.interrupted) {
79+
reject(new CancelRun());
80+
}
81+
});
82+
});
83+
84+
const runAllTests = Promise.all(
85+
tests.map(test =>
86+
runTestInWorker(test)
87+
.then(testResult => onResult(test, testResult))
88+
.catch(error => onError(error, test)),
89+
),
90+
);
91+
92+
const cleanup = () => workerFarm.end(farm);
93+
94+
return Promise.race([runAllTests, onInterrupt]).then(cleanup, cleanup);
95+
}
96+
}
97+
98+
return BaseTestRunner;
599
};
100+
101+
module.exports = createRunner;

lib/run.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
exports.run = options => {
2+
console.log('\n\n', options, '\n\n');
3+
process.exit(1);
4+
};

lib/utils.js

Whitespace-only changes.

message.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
{
4+
button_save: 'Save',
5+
button_cancel: 'Cancel',
6+
service_list_header: 'Header',
7+
}

0 commit comments

Comments
 (0)