stryker-mutator / stryker-js

Mutation testing for JavaScript and friends
https://stryker-mutator.io
Apache License 2.0
2.6k stars 251 forks source link

Using Stryker with a MeteorJS project #466

Closed aviadbd closed 5 years ago

aviadbd commented 6 years ago

I'm working with Meteor, testing with Mocha, and would really love to use Stryker.

Problem is, since Stryker is its own runner, it won't find any Meteor packages (or so it seems):

(function (exports, require, module, __filename, __dirname) { import {HTTP} from 'meteor/http';
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at /Project/node_modules/mocha/lib/mocha.js:231:27
[2017-11-21 07:22:12.750] [ERROR] StrykerCli - an error occurred Error: Something went wrong in the initial test run

Any ideas on how to make it work?

nicojs commented 6 years ago

Hi @aviadbd , thanks reporting this issue.

Could you provide is with some more information? I've never used Meteor myself, could you explain how your build process works? I'm assuming that this line of code import {HTTP} from 'meteor/http'; comes from your code somewhere. NodeJS does not support import statements yet, so what kind of transpiler are you using? Babel? Webpack?

aviadbd commented 6 years ago

Hi @nicojs

Meteor has its own package manager, uses import and in fact wraps npm packages in a way that allows you to import them too.

Meteor is its own transpiler, for that matter, as it builds from your source a node.js project and runs/deploys it.

nicojs commented 6 years ago

Ok, but the tests are clearly running on plain node, seeing the nodejs function wrapper siganture here: (function (exports, require, module, __filename, __dirname). It seems to me that meteor doesn't use the mocha test runner, but something like karma instead. Can you confirm this? Could you maybe provide a small test project for us that looks like your application (meteor + es6 + a way to run the tests)?

aviadbd commented 6 years ago

Maybe I didn't explain well?

When I run tests using Meteor, it complies (transpiles?) the sources correctly and then uses a test driver to run the tests. It can use many test drivers packaged for the task, specifically I use Mocha.

Please see a great example as the Todos App, at https://github.com/meteor/todos/ .

An example test, using Mocha and Chai (the recommended setup at Meteor's own site), could be found here: https://github.com/meteor/todos/blob/master/imports/api/lists/server/lists.tests.js

nicojs commented 6 years ago

Ok, thanks for your additional explanation. I want to take a closer look when i get some time (which is hopefully by the end of this week). For the time being, you could try to run stryker on the intermediary files that meteor transpiles for you.

Basically what needs to happen is that every mutation to the source file gets transpiled by stryker using meteor (or whatever bundler meteor internally uses), before running it through mocha.

aviadbd commented 6 years ago

I'm not sure that's actually possible, as I'm not sure the transpiled project keeps the same structure of the original project; therefore, choosing the files to mutate might prove hard.

I can wait a week or two. Please tell me if you want me to check something.

simondel commented 6 years ago

@aviadbd Could you see what happens if you use https://www.npmjs.com/package/stryker-javascript-mutator in the project? (This probably won't mean we support your project. I'm just curious what happens)

aviadbd commented 6 years ago

@simondel I'm trying to understand why it would be any different; its still running on code that has dependencies that can't be found (the Meteor dependencies). Am I wrong?

simondel commented 6 years ago

We have decided that we won't focus on building Meteor support. Would you be willing to build this yourself or shall we close this issue?

gugacavalieri commented 6 years ago

Hey Guys,

Found a solution by mocking the Meteor Framework :)

I created a repo for this.

You can check it out at https://github.com/gugacavalieri/stryker-meteor-integration

Many thanks to https://github.com/pcorey/unit-testing-with-testdouble

nicojs commented 6 years ago

@gugacavalieri awesome you were able to pull it off! I see you're trying to setup the mutation score badge. If you're having trouble with it, please don't hesitate to drop us a line at gitter: https://gitter.im/stryker-mutator/stryker

gugacavalieri commented 6 years ago

@nicojs Thanks ! Will check again why it is not rendering and let you know :smile:

simondel commented 5 years ago

As a workaround is available, I'm closing this issue for now.