Skip to content

Commit

Permalink
Origin/issue #183 add support grep option (#187)
Browse files Browse the repository at this point in the history
* - add support option grep
- add test

* - add support grep option
- add test for new option

* add test in list

* Add grep flag to runner and mocha ts

* back tests

* final fixes to make working grep according to pull request comments
  • Loading branch information
hennadiib authored and 1999 committed Jul 30, 2018
1 parent 7bdaca8 commit 6987222
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import applyExit from './options/exit';
import applyForbidOnly from './options/forbid-only';
import applyForbidPending from './options/forbid-pending';
import applyFullTrace from './options/full-trace';
import applyGrepPattern from './options/grep';
import applyMaxParallel from './options/max-parallel';
import applyNoTimeouts from './options/no-timeouts';
import applyReporter from './options/reporter';
Expand Down Expand Up @@ -65,6 +66,9 @@ const argv = yargs
.option('max-parallel', {
number: true,
})
.option('grep', {
string: true,
})
.option('recursive', {
boolean: true,
})
Expand Down Expand Up @@ -122,6 +126,9 @@ applyForbidPending(mocha, argv.forbidPending);
// --full-trace
applyFullTrace(mocha, argv.fullTrace);

// --grep option
applyGrepPattern(mocha, argv.grep);

// --max-parallel
applyMaxParallel(mocha, argv.maxParallel);

Expand Down
1 change: 1 addition & 0 deletions src/bin/options/grep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { applyGrepPattern as default } from '../../util';
4 changes: 4 additions & 0 deletions src/main/mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ export default class MochaWrapper extends Mocha {
forkArgs.push('--delay');
}

if (this.options.grep) {
forkArgs.push('--grep', this.options.grep.toString());
}

if (this.exitImmediately) {
forkArgs.push('--exit');
}
Expand Down
5 changes: 5 additions & 0 deletions src/subprocess/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { SUITE_OWN_OPTIONS } from '../config';
import {
applyCompilers,
applyDelay,
applyGrepPattern,
applyNoTimeouts,
applyRequires,
applyTimeouts,
Expand All @@ -33,6 +34,7 @@ const argv = yargs
default: [],
})
.boolean('delay')
.string('grep')
.boolean('enableTimeouts')
.option('exit', {
boolean: true,
Expand Down Expand Up @@ -204,6 +206,9 @@ applyCompilers(argv.compilers);
// --delay
applyDelay(mocha, argv.delay);

// --grep
applyGrepPattern(mocha, argv.grep);

// --enableTimeouts
applyNoTimeouts(mocha, argv.enableTimeouts);

Expand Down
6 changes: 6 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ export function applyDelay(mocha: Mocha, delay: boolean) {
}
}

export function applyGrepPattern(mocha: Mocha, stringPattern?: string) {
if (stringPattern) {
mocha.grep(stringPattern);
}
}

export function applyNoTimeouts(mocha: Mocha, allowTimeouts: boolean) {
if (allowTimeouts === false) {
mocha.enableTimeouts(false);
Expand Down
1 change: 1 addition & 0 deletions test/grep/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Check that --grep flag run tests by string pattern 'grep'
27 changes: 27 additions & 0 deletions test/grep/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env node

var assert = require('assert');
var path = require('path');
var exec = require('child_process').exec;
var libExecutable = path.resolve(__dirname, '../../dist/bin/cli.js');

exec(libExecutable + ' -R json --grep grep test/grep/tests', {
cwd: path.resolve(__dirname, '../../')
}, function (err, stderr) {
if (err) {
console.error(err);
process.exit(1);
}

var jsonReporterOutput = stderr.toString();

try {
jsonReporterOutput = JSON.parse(jsonReporterOutput);
} catch (ex) {
console.error('Native JSON reporter output is not valid JSON: ' + jsonReporterOutput);
process.exit(1);
}

assert.strictEqual(jsonReporterOutput.stats.passes, 3, 'Tests number is wrong. Expected 3. Actual: '
+ jsonReporterOutput.stats.passes);
});
19 changes: 19 additions & 0 deletions test/grep/tests/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const assert = require('assert');

describe('tests', () => {
it('should be run 1. grep', () => {
assert.ok(true);
});

it('should be skipped', () => {
assert.ok(true);
});
});

describe('tests should be run. grep', () => {
it('should be run 2', () => {
assert.ok(true);
});
});
14 changes: 14 additions & 0 deletions test/grep/tests/test1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const assert = require('assert');

describe('tests file 2', () => {
it('should be run 3. grep', () => {
assert.ok(true);
});

it('should be skipped', () => {
assert.ok(true);
});
});

1 change: 1 addition & 0 deletions test/index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ test 'TESTCASE: --retries and --bail should work well together' test/bail-and-re
test 'TESTCASE: subprocess exits before sending an end message' test/no-subprocess-end/index.js
test 'TESTCASE: unhandled rejections should not force subprocess to exit' test/q-promises/index.js
test 'TESTCASE: uncaught exceptions should not force subprocess to exit' test/uncaught-exception/index.js
test 'TESTCASE: grep option' test/grep/index.js

echo "Passes: $PASSES Failes: $FAILES"
echo ""
Expand Down

0 comments on commit 6987222

Please sign in to comment.