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

Improve Mocha support #1046

Closed
prsn670 opened this issue Jul 26, 2018 · 22 comments · Fixed by #1237
Closed

Improve Mocha support #1046

prsn670 opened this issue Jul 26, 2018 · 22 comments · Fixed by #1237
Labels
🚀 Feature request New feature request 👶 Good first issue Good for newcomers hacktoberfest https://hacktoberfest.digitalocean.com/

Comments

@prsn670
Copy link

prsn670 commented Jul 26, 2018

Summary

Hello all, I was hoping somebody could let me know what I'm doing wrong here. I keep getting a syntax error on CSS files. We're using Stryker version 0.14.1. We are also using react/redux with the mocha test framework. We are not using webpack. I'm unsure if it is some configuration error, version issue, or something completely different. The error is as follows:

[2018-07-26 13:02:05.874] [ERROR] InitialTestExecutor - One or more tests resulted in an error:
SyntaxError: Unexpected token .
C:\code_base\poi\node_modules\form\lib\util\block.css:1
(function (exports, require, module, __filename, __dirname) { .a-form-v3-10-0-block {

Stryker config

module.exports = function(config){
  config.set({
   files: [
        "src/**/*.js",
        "test/**/*.js"
    ],
    mutate:[
      "src/pages/poi/*.js"
    ],
    testRunner: "mocha",
    mutator: "javascript",
    transpilers: ["babel"],
    reporter: ["html", "clear-text", "progress"],
    testFramework: "mocha",
    coverageAnalysis: "off",
    babelrcFile: ".babelrc"
  });
}

Stryker environment

+-- stryker@x0.14.1
+-- stryker-api@x0.11.0
+--stryker-babel-transpiler@0.2.2
+--stryker-html-reporter@0.11.1
+--stryker-javascript-mutator@0.2.3
+--stryker-mocha-framework@0.7.0
+==stryker-mocha-runner@0.10.0

Your Environment

software version(s)
node 6.1.0
npm 3.8.6
Operating System Windows 7 Enterprise
@nicojs
Copy link
Member

nicojs commented Jul 26, 2018

Thanks for reporting this issue. I see that you have both a webpack configuration and a Babel configuration. You normally won't need webpack when using mocha as a test runner. What does your babelrc and npm test command look like?

@simondel
Copy link
Member

@nicojs the post was edited after placing it. The webpack config is now gone and the post mentions they don't use webpack. (I was about to ask the same question)

@prsn670
Copy link
Author

prsn670 commented Jul 26, 2018

Sorry about the mistake in the config file, I did edit it right after I noticed it. Here is my babelrc and npm test command.

babelrc

{
  "env": {
    "test": {
      "plugins": [
        [
          "istanbul",
          {
            "exclude": [
              "src/main.js",
              "src/redux/configureStore.js"
            ],
            "include": [
              "src/"
            ]
          }
        ]
      ]
    }
  },
  "ignore": ["dist/", "coverage/"],
  "presets": ["env", "stage-2", "react"]

npm test command

"test": "cross-env NODE_ENV=test mocha "./test/**/*-tests.js"",

@simondel
Copy link
Member

As a check, could you upgrade the Stryker dependencies to their latest versions? If you use Git for this project, you may be able to remove the files property from the config as well.

@prsn670
Copy link
Author

prsn670 commented Jul 27, 2018

Sorry for taking so long to reply. I was able to update Stryker to the latest version, but may have to wait till Monday till the other dependencies can be updated. I tried running with just the updated Stryker and got this error:

module.js:440
throw err;
^

Error: Cannot find module 'stryker-api/logging'
at Function.Module._resolveFilename (module.js:438:15)
at Function.Module._load (module.js:386:25)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
at Object. (C:\code_base\pc-property-renters-proof-of-insurance\node_modules\stryker\src\config\ConfigReader.js:5:17)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)

Trying to delete the old conf file and running init again also results in the same error. I'm thinking I'll have to update stryker-api at least before I can do anything more.

@simondel
Copy link
Member

Yeah, we've recently revamped the logging so you'll have to update all Stryker dependencies (including the plugins such as stryker-mocha-runner).

Don't worry about taking a while to respond. We're here to help you and if you need some time that's perfectly fine. We also don't have time to reply or fix things right away :)

@prsn670
Copy link
Author

prsn670 commented Jul 30, 2018

I installed the upgraded dependencies this morning and reran the init command. The only thing the setup asked me was what type of reporters I wanted and the package manager I was using. The config file had a few labels with nothing filled in so I filled it in. I'm still receiving an error:

08:01:56 (13016) ERROR TestRunnerChildProcessAdapter Child process exited with non-zero exit code 1. Last 10 message from the child process were:

08:01:56 (13016) ERROR InitialTestExecutor One or more tests resulted in an error:
SyntaxError: Unexpected token .
C:\code_base\poi.stryker-tmp\sandbox5164784\src\pages\poi-trap\poi-trap.css:10
.trap-section-container
^
SyntaxError: Unexpected token .
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:511:25)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
at Object. (C:\code_base\poi.stryker-tmp\sandbox5164784\src\pages\poi-trap\poi-trap.js:13:1)
at Module._compile (module.js:541:32)
08:01:56 (13016) ERROR StrykerCli an error occurred Error: Something went wrong in the initial test run
at InitialTestExecutor.validateResult (C:\code_base\poi\node_modules\stryker\src\process\InitialTestExecutor.js:129:15)
at InitialTestExecutor. (C:\code_base\poi\node_modules\stryker\src\process\InitialTestExecutor.js:45:30)
at step (C:\code_base\poi\node_modules\stryker\node_modules\tslib\tslib.js:133:27)
at Object.next (C:\code_base\poi\node_modules\stryker\node_modules\tslib\tslib.js:114:57)
at fulfilled (C:\code_base\poi\node_modules\stryker\node_modules\tslib\tslib.js:104:62)
at process._tickCallback (internal/process/next_tick.js:103:7)

Stryker config

module.exports = function(config){
  config.set({
   mutate: [
        "src/pages/poi-email/*.js"
    ],
    testRunner: "mocha",
    mutator: "javascript",
    transpilers: ["babel"],
    reporter: ["clear-text", "progress"],
    packageManager: "npm",
    coverageAnalysis: "off",
    babelrcFile: ".babelrc"
  });
}

Stryker environment

+-- stryker@x0.25.1
+-- stryker-api@x0.18.0
+--stryker-babel-transpiler@0.6.0
+--stryker-javascript-mutator@0.8.0
+--stryker-mocha-framework@0.11.0
+==stryker-mocha-runner@0.13.0

@nicojs
Copy link
Member

nicojs commented Jul 30, 2018

@prsn670

From your test command I conclude that you're not using babel for your mocha tests, is this correct? Seeing as this is your test command:
"cross-env NODE_ENV=test mocha \"./test/**/*-tests.js\""

Or am I missing something? A mocha.opts file perhaps?

Could you try this configuration:

module.exports = function(config){
  config.set({
    mutate:[
      "src/pages/poi/*.js"
    ],
    testRunner: "mocha",
    testFramework: "mocha",
    mutator: "javascript",
    reporter: ["html", "clear-text", "progress"],
    coverageAnalysis: "off",
    mochaOptions: {
      files: [ './test/**/*-tests.js' ]
    }
  });
}

EDIT:

Also run it with the same environment variables. Add this to your package.json:

"scripts": {
  "stryker": "cross-env NODE_ENV=test stryker run"
}

And run using npm run stryker

@prsn670
Copy link
Author

prsn670 commented Jul 30, 2018

I tried the configuration @nicojs suggested but I get a syntax error on the import. This happens when we don't include the babel transpiler. We do have a mocha.opts file in which our mocha-hook.js file is required. babel-register is required in the hook.js file, which should also disable our css imports. Could there be an issue where the mocha.opts file is not being used correctly?

@nicojs
Copy link
Member

nicojs commented Jul 30, 2018

Could there be an issue where the mocha.opts file is not being used correctly?

Yes, this is most definitely it. It is not used by default. Mocha has no clean way of providing the mocha.opts file, instead we had to recreate the functionality. See https://github.com/stryker-mutator/stryker/tree/master/packages/stryker-mocha-runner#configuring

I think our implementation doesn't support local requires. Could you try this:

module.exports = function(config){
  config.set({
    mutate:[
      "src/pages/poi/*.js"
    ],
    testRunner: "mocha",
    testFramework: "mocha",
    mutator: "javascript",
    reporter: ["html", "clear-text", "progress"],
    coverageAnalysis: "off",
    mochaOptions: {
      files: [ './test/**/*-tests.js' ],
      opts: 'mocha.opts',
      require: [require.resolve('./mocha-hook.js')]
    }
  });
}

That should do the trick.

@prsn670
Copy link
Author

prsn670 commented Jul 30, 2018

Alright, I think we're almost there. I am getting a different error:

11:17:04 (7868) ERROR TestRunnerChildProcessAdapter Child process exited with non-zero exit code 1. Last 10 message from the child process were:

11:17:04 (7868) ERROR InitialTestExecutor One or more tests resulted in an error:
TypeError: (0 , _mocha.describe) is not a function
TypeError: (0 , _mocha.describe) is not a function

I am using chai, enzyme, and sinon along with mocha if that makes a difference. I also need to add our dom.js file to the require tag under mochaOptions, but I'm unsure of the usage 'require.resolve'. Will I just need to add another require to the array and put in the corresponding path or does resolve take multiple strings as arguments?

@nicojs
Copy link
Member

nicojs commented Jul 30, 2018

I'm not sure who is responsible for adding _mocha.describe to the global object. Might be the dom.js you are talking about.

I also need to add our dom.js file to the require tag under mochaOptions, but I'm unsure of the usage 'require.resolve'

require.resolve takes one string argument and resolves it to an absolute path (relative to the current (stryker.conf.js) file). So it will return a string. If you want to also add dom,js you can do it by adding it to the require array:

    mochaOptions: {
      files: [ './test/**/*-tests.js' ],
      opts: 'mocha.opts',
      require: [require.resolve('./mocha-hook.js'), require.resolve('./dom.js')]
    }

If this setup works we need to fix it so that we use your mocha.opts file by default as well as make sure relative imports work as expected. But lets first get it working for you.

@prsn670 prsn670 closed this as completed Jul 30, 2018
@prsn670
Copy link
Author

prsn670 commented Jul 30, 2018

Unfortunately adding the dom.js didn't resolve any issues. I'm still getting the same error for mocha.describe.

I would think the "stryker-mocha-framework" and "stryker-mocha-runner" would take care of it.

@prsn670 prsn670 reopened this Jul 30, 2018
@nicojs
Copy link
Member

nicojs commented Jul 30, 2018

Yes, they should make sure describe is on the global scope.

Hmm ok, time to pull out the big guns. Could you open up /node_modules/stryker-mocha-runner/src/MochaTestRunner.js and replace the additionalRequires function? Should be on line 109 to 113. Replace it with this:

MochaTestRunner.prototype.additionalRequires = function () {
        var _this = this;
        if (this.mochaRunnerOptions.require) {
            var stuffToRequire = this.mochaRunnerOptions.require
                .map(function (theModule) { return theModule.startsWith('.') ? path.resolve(theModule) : theModule; });
            stuffToRequire.forEach(function (theModule) { return _this.log.info("Importing " + theModule); });
            stuffToRequire
                .forEach(LibWrapper_1.default.require);
        }
        else {
            this.log.info('Nothing to require');
        }
    };

That should make local requires possible (and add some logging).

If you now remove the entire require from your stryker.conf.js file (but leave the mochaOptions.opts key in there), that would make sure that you're requiring the local sandboxed files. That might do the trick.

If that doesn't work, would you mind creating a small reproduction repository? The smaller the better. If we could take a look, I'm sure we could figure it out.

@prsn670
Copy link
Author

prsn670 commented Aug 1, 2018

I'll see to creating a small repository that reproduces the problem (today). Unfortunately the replacement of the function did not help, I'm still getting the same error. I also added the ui tag and gave it 'bdd', but that didn't seem to help either.

I also wanted some clarification on location of the config file. The readme says that it is expected to be the current working directory, but then goes on to say:

"As you can see, the config file is not a simple JSON file. It should be a node module."

I have my config file in the root directory, just want to make sure this isn't an issue.

EDIT: I jut realized I never gave the whole error message last time. Here it is below:

08:12:35 (11112) ERROR TestRunnerChildProcessAdapter Child process exited with non-zero exit code 1. Last 10 message from the child process were:

08:12:35 (11112) ERROR InitialTestExecutor One or more tests resulted in an error:
TypeError: (0 , _mocha.describe) is not a function
TypeError: (0 , _mocha.describe) is not a function
at Object. (C:/code_base/poi/.stryker-tmp/sandbox5856437/test/unit/pages/poi-email/poi-email-tests.js:22:1)
at Module._compile (module.js:541:32)
at loader (C:\code_base\poi\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (C:\code_base\poi\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
at C:\code_base\poi\node_modules\mocha\lib\mocha.js:250:27
08:12:35 (11112) ERROR StrykerCli an error occurred Error: Something went wrong in the initial test run
at InitialTestExecutor.validateResult (C:\code_base\poi\node_modules\stryker\src\process\InitialTestExecutor.js:129:15)
at InitialTestExecutor. (C:\code_base\poi\node_modules\stryker\src\process\InitialTestExecutor.js:45:30)
at step (C:\code_base\poi\node_modules\stryker\node_modules\tslib\tslib.js:133:27)
at Object.next (C:\code_base\poi\node_modules\stryker\node_modules\tslib\tslib.js:114:57)
at fulfilled (C:\code_base\poi\node_modules\stryker\node_modules\tslib\tslib.js:104:62)
at process._tickCallback (internal/process/next_tick.js:103:7)

@nicojs
Copy link
Member

nicojs commented Aug 1, 2018

I have my config file in the root directory, just want to make sure this isn't an issue.

No, no issue there.

I'll see to creating a small repository that reproduces the problem (today

Awesome! I'm sure we'll be able to fix this issue once we can reproduce it 👍

@prsn670
Copy link
Author

prsn670 commented Aug 2, 2018

So I figured out what was causing describe not be recognized as a function. I got the idea here. I simply had to remove my import statement for mocha and it started working. The weird thing is when I tried to reproduce the error on my computer with the import statements it would still work and give me no error. It may be some anomaly with the environment, there's a lot of code and environment setup that was done by other people. Thanks so much to both of you for your help.

@prsn670 prsn670 closed this as completed Aug 4, 2018
@nicojs
Copy link
Member

nicojs commented Aug 4, 2018

@prsn670 Awesome you made it work! Have fun improving your test!

Thanks for closing, but i would like to keep this issue open. I want to fix 3 things:

  • 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.

@nicojs nicojs reopened this Aug 4, 2018
@simondel
Copy link
Member

@nicojs Shall we create an issue for the problems you mention?

@nicojs
Copy link
Member

nicojs commented Aug 12, 2018

I was thinking to use this issue for them. Doesn't matter really.

@nicojs nicojs added 🚀 Feature request New feature request 👶 Good first issue Good for newcomers hacktoberfest https://hacktoberfest.digitalocean.com/ labels Oct 15, 2018
@simondel simondel changed the title SyntaxError CSS file Improve Mocha support Oct 26, 2018
@simondel
Copy link
Member

I have renamed the issue. This comment by Nico contains a list of things to do for this issue.

@simondel simondel added the pesto label Oct 26, 2018
@bharaninb
Copy link
Contributor

Working on it

@ghost ghost added the 🔎 Needs review label Nov 12, 2018
simondel pushed a commit that referenced this issue Nov 13, 2018
…orts (#1237)

Check if `test/mocha.opts` exists if the no mocha.opts is specified in the stryker config.
Make sure relative imports when using `--require` (same as mocha does it).

Fixes #1046
@ghost ghost removed the 🔎 Needs review label Nov 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚀 Feature request New feature request 👶 Good first issue Good for newcomers hacktoberfest https://hacktoberfest.digitalocean.com/
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants