Skip to content

Commit

Permalink
#42: Added support of relative requires in mocha-runner. Added suppor…
Browse files Browse the repository at this point in the history
…t of shorthand options of mocha.opts (#43)

* Fixed relative requires in mocha runner. Added shorthand options in mocha < 6

* Corrected comment
  • Loading branch information
saurabh-prakash authored May 27, 2022
1 parent 7727ab0 commit 0b92995
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 28 deletions.
56 changes: 56 additions & 0 deletions packages/mocha-runner/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
import Mocha from "mocha";
import fs from "fs";
import crypto from "crypto";
import parser from "yargs-parser";
import { ID, TestResult, TASDate as Date, TestStatus, TestSuiteResult, Util } from "@lambdatest/test-at-scale-core";

const shortHandOptsMap = new Map<string, string>(
[
["-A", "--async-only"],
["-c", "--colors"],
["-C", "--no-colors"],
["-G", "--growl"],
["-O", "--reporter-options"],
["-R", "--reporter"],
["-S", "--sort"],
["-b", "--bail"],
["-d", "--debug"],
["-g", "--grep"],
["-f", "--fgrep"],
["-gc", "--expose-gc"],
["-i", "--invert"],
["-r", "--require"],
["-s", "--slow"],
["-t", "--timeout"],
["-u", "--ui"],
["-w", "--watch"]
]
);

export class MochaHelper {

// This gives parentSuite in reverse order i.e. innermost parent will be at 0th index
Expand Down Expand Up @@ -93,6 +118,37 @@ export class MochaHelper {
suiteStartTime
);
}

static getFilteredConfigs(argv: parser.Arguments): Mocha.MochaOptions {
const args = [];
if (argv.config !== undefined) {
args.push("--config", argv.config);
}
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const loadOptions = require('mocha/lib/cli/options').loadOptions;
const opts = loadOptions(args) as Mocha.MochaOptions;
opts.parallel = false;
return opts;
} catch (err) {
// implies user is using mocha version < 6
console.warn("Using mocha < 6");
const optsFilePath = argv.config ?? "./test/mocha.opts";
if (fs.existsSync(optsFilePath)) {
// Following code translates mocha opts file to longhand opts array
const opts = fs
.readFileSync(optsFilePath, 'utf8')
.replace(/^#.*$/gm, '')
.replace(/\\\s/g, '%20')
.split(/\s/)
.filter(Boolean)
.map(value => value.replace(/%20/g, ' '))
.map(value => shortHandOptsMap.get(value) ?? value);
return parser(opts) as Mocha.MochaOptions;
}
return {};
}
}
}

export class CustomRunner extends Mocha.Runner {
Expand Down
35 changes: 7 additions & 28 deletions packages/mocha-runner/src/mocha-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@ class MochaRunner implements TestRunner {

createMochaInstance(): Mocha {
const argv = parser(hideBin(process.argv), { array: ['diff', "locator"] });
const mocha = new Mocha(this.getFilteredConfigs(argv));
const mocha = new Mocha(MochaHelper.getFilteredConfigs(argv));
if (mocha.options.require !== undefined) {
const cwd = process.cwd();
module.paths.push(cwd, path.join(cwd, 'node_modules'));
if (!(mocha.options.require instanceof Array)) {
mocha.options.require = [mocha.options.require];
}
for (const file of mocha.options.require) {
require(file);
for (let mod of mocha.options.require) {
const abs = fs.existsSync(mod) || fs.existsSync(mod + '.js');
if (abs) {
mod = path.resolve(mod);
}
require(mod);
}
}
return mocha;
Expand Down Expand Up @@ -291,31 +295,6 @@ class MochaRunner implements TestRunner {
}
}
}

private getFilteredConfigs(argv: parser.Arguments): Mocha.MochaOptions {
const args = [];
if (argv.config !== undefined) {
args.push("--config", argv.config);
}
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const loadOptions = require('mocha/lib/cli/options').loadOptions;
const opts = loadOptions(args) as Mocha.MochaOptions;
opts.parallel = false;
return opts;
} catch (err) {
// implies user is using mocha version < 6
console.info("Using mocha < 6");
const optsFilePath = argv.config ?? "./test/mocha.opts";
if (fs.existsSync(optsFilePath)) {
// Following code translates newlines separated mocha opts file
// to space separated command-line opts string
const rawOpts = fs.readFileSync(optsFilePath).toString().split("\n").join(" ");
return parser(rawOpts) as Mocha.MochaOptions;
}
return {};
}
}
}

(async () => {
Expand Down

0 comments on commit 0b92995

Please sign in to comment.