Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mocha-runner): improve Mocha support #1237

Merged
merged 5 commits into from
Nov 13, 2018

Conversation

bharaninb
Copy link
Contributor

closes #1046

@ghost ghost added the 🔎 Needs review label Nov 12, 2018
@bharaninb
Copy link
Contributor Author

bharaninb commented Nov 12, 2018

  • use your mocha.opts file by default (if exists)
  • Make sure relative imports when using --require (same as mocha does it)
  • Make sure const { describe } = require('mocha'); works as well.

@bharaninb bharaninb changed the title [WIP] feat(mocha-runner): improve Mocha support feat(mocha-runner): improve Mocha support Nov 12, 2018
Copy link
Member

@simondel simondel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome fixes! Could you take a look at my comments?

@@ -17,6 +17,7 @@ export default class MochaTestFramework implements TestFramework {
public filter(testSelections: TestSelection[]) {
const selectedTestNames = testSelections.map(selection => selection.name);
return `var Mocha = window.Mocha || require('mocha');
var describe = Mocha.describe;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to save this?

Copy link
Contributor Author

@bharaninb bharaninb Nov 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[x] Make sure const { describe } = require('mocha'); works as well.

#1046 (comment)

Based on my understanding, sometimes describe is not on the global scope.
Correct me if I am wrong.


public load(config: StrykerOptions): MochaRunnerOptions {
const mochaOptions = Object.assign({}, config[mochaOptionsKey]) as MochaRunnerOptions;
let optsFileName = path.resolve(this.DEFAULT_MOCHA_OPTS);

if (mochaOptions && mochaOptions.opts) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice that we always check if we have mochaOptions, but there should always be an object, right? We define it on line 13.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, mochaOptions always be an object. I failed to check existing conditions, will update it.

}

if (mochaOptions && mochaOptions.opts) {
throw new Error(`Could not load opts from "${optsFileName}". Please make sure opts file exists.`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really strange to me. Because we return on line 23, we throw an error here.

If we get an error while reading or parsing the Opts file, we will not log this error because we have no try-catch.

Could you change this to:

if (fs.existsSync(optsFileName)) {
   ...
} else if (mochaOptions.opts) {
   this.log.error(`Could not load opts from "${optsFileName}". Please make sure opts file exists.`);
}

(The stryker-karma-runner handles an error like this by logging it instead of throwing an error)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the previous codebase, if the specified file doesn't exist, it throws an error while reading the options file. I try to achieve the same here.
Will change above statements

@@ -116,7 +116,11 @@ export default class MochaTestRunner implements TestRunner {

private additionalRequires() {
if (this.mochaRunnerOptions.require) {
this.mochaRunnerOptions.require.forEach(LibWrapper.require);
const stuffToRequire = this.mochaRunnerOptions.require
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rename this to modulesToRequire?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sure.

@@ -116,7 +116,11 @@ export default class MochaTestRunner implements TestRunner {

private additionalRequires() {
if (this.mochaRunnerOptions.require) {
this.mochaRunnerOptions.require.forEach(LibWrapper.require);
const stuffToRequire = this.mochaRunnerOptions.require
.map(theModule => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rename this to module?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

@simondel
Copy link
Member

Looks great! Thanks again!

@simondel simondel merged commit 2711c2b into stryker-mutator:master Nov 13, 2018
@ghost ghost removed the 🔎 Needs review label Nov 13, 2018
@bharaninb bharaninb deleted the feat/mocha branch November 14, 2018 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Mocha support
2 participants