Stryker mutation tesing with ES6 files - javascript

I have encountered an issue where I am trying to perform mutation testing on my util classes in my react project using this http://stryker-mutator.github.io/ library. However I get the following errors,
[2017-05-17 16:29:04.321] [ERROR] CoverageInstrumenterStream - Error while instrumenting file "path/to/something.js", error was: Error: Line 29: Unexpected token
[2017-05-17 16:29:05.586] [ERROR] Stryker - One or more tests errored in the initial test run:
SyntaxError: Unexpected token import
seems the library cant identify the import and the export statements in the file.
I tried to search a fix for this but came up short. It will be grately appreciated if you experts can help me on a workaround, or a solution via grunt where I could change the import and exports to require and module.exports without harming the code format of the logic.
Thanks alot

I have worked with stryker framework. In our project, we were using browserify for importing files. So if you are using the same, you have to provide 'browserify' in framework array in stryker.conf.js.

The problem is that this version of Stryker work with ES5 by default.
There are a new version of Stryker which support ES6 (https://www.npmjs.com/package/stryker-javascript-mutator).

Related

Get "SyntaxError: Cannot use import statement outside a module" when attempting to import a class in Jasmine spec file

As an exercise I decided to create a little vanilla JavaScript game using ES6 syntax that runs in the browser. The program works fine.
I'd like to test it using Jasmine. However, whenever I try to perform an import e.g.
import Deck from "../Deck.js";
Deck.js starts:
export default class Deck {
I get error SyntaxError: Cannot use import statement outside a module.
Things I've done:
Installed node v13.0.1 - I thought this version of node allowed es6 modules.
Installed jasmine and initialized node ./node_module/jasmine/bin/jasmine init
Run node ./node_module/jasmine/bin/jasmine - works fine without imports
Run node --experimental-modules ./node_module/jasmine/bin/jasmine - doesn't work with imports
Tried require instead of import: const Deck = require('../Deck.js'); - SyntaxError: Unexpected token 'export'
How do I get jasmine to work with imports? At the moment I cannot include any files to test !
I'm sure I've gone about this the wrong way, but i just want some cmd line tests.
Follow the offical guide from Babel at https://babeljs.io/setup#installation
Then choose one of the options from this solution: https://stackoverflow.com/a/59399717/673351
Personally, I have renamed my spec files to have the mjs extension becaue I would like ot use the LTS (currently 12) version of node.

How can I resolve the following compilation error when running Jest for testing?

Although I am able to start the npm project using npm start without any issues with webpack or babel, once I run npm test, I find the following error related to testing App.js using App.test.js (where App.js imports ApolloClient):
TypeError: Cannot assign to read only property '__esModule' of object '[object Object]'
| import ApolloClient from 'apollo-boost';
| ^
at node_modules/apollo-boost/lib/bundle.cjs.js:127:74
at Array.forEach (<anonymous>)
at Object.<anonymous> (node_modules/apollo-boost/lib/bundle.cjs.js:127:36)
Essentially, I'm confused as to why I get an error when running the test but not when starting the project.
I've tried adding in a number of babel plugins to both .babelrc and in my webpack config file:
#babel/plugin-transform-object-assign
#babel/plugin-transform-modules-commonjs
babel-plugin-transform-es2015-modules-commonjs
However, I haven't been able to resolve the issue. My thinking was that this is related to the fact that the file that fails to compile was originally CommonJS.
I was only able to find something relatively similar here, https://github.com/ReactTraining/react-router/pull/6758, but I didn't find a solution.
Is there something that I'm missing specifically related to running tests? I should also mention I've tried frameworks other than Jest and ran into the same issue.
EDIT:
I removed everything from App.test.js except the imports to isolate the issue so it just contains the following:
import React from 'react';
import { shallow } from 'enzyme/build';
import App from './App';
UPDATE:
I was able to resolve the initial error by upgrading apollo-boost from version 0.3.1 to 0.4.2. However, I now have a different error that is similarly frustrating. I am using Babel 7 and have added the plugin #babel/plugin-syntax-dynamic-import to both my .babelrc and to my webpack.config.js files. Despite this, I get the following error related to the use of a dynamic import in App.js when running the Jest to test App.test.js:
SyntaxError: Support for the experimental syntax 'dynamicImport' isn't currently enabled
Add #babel/plugin-syntax-dynamic-import (https://git.io/vb4Sv) to the 'plugins' section of your Babel config to enable parsing.
I'm not sure if there is a parsing error or something else, but I've tried numerous things that have not worked. The closest discussion I could find related to this problem is, https://github.com/facebook/jest/issues/5920, however, the proposed solutions don't work for me.
UPDATE:
One thing that I'm trying is to avoid duplication of the babel options as right now they're both in .babelrc and in the babel-loader options within webpack.config.js. From what I found online (Whats the difference when configuring webpack babel-loader vs configuring it within package.json?), the way to make webpack use the settings in .babelrc is to not specify options. However, doing so results in the same error described above showing up only this time when running npm start. I will add that the project that was originally created using create-react-app, however, in order to support multiple pages, I needed to customize webpack's configuration and so ejected from it. I'm not sure why this is so convoluted.
its probably a babel configuration issue, I'm pretty sure jest needs to be compiled to work with create-react-app...
did you specify a setup file in package.json:
"jest": {
"setupFiles": [
"/setupTests.js"
]
}
and in setupTests.js:
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
It turns out that one of the components in the project's src directory had its own local package.json file even though it wasn't being used and was not installed as a local dependency in the top level package.json (instead imports were done using relative urls). For some reason, the existence of this file changed the behavior of webpack and other tools when starting and testing the project such that none of the top level configurations were used for files within directories with separate package.json files. Once I removed these local package.json files from the components sub-directory, all the prior issues were resolved. One hallmark of this problem is that compilation errors were not showing up for JavaScript files that weren't nested under an alternate package.json file.
Hopefully this is useful for anyone that encounters similar errors as I don't think the cause can be directly determined from the compiler messages alone.

Make Webpack 1.13 work with ES6 vendor

I am trying to add some external dependency cryptowatch-embed js to my webpack project (of version 1.13.3) as vendor. The concern is that this dependency is written in ES6 style and js file ends with something like export default Embed;. When I run my webpack-dev-server and open the browser the console shows me Uncaught SyntaxError: Unexpected token export on this line. I suppose that webpack does not bundle ES6 vendor properly, but I dot'n have any ideas what should be done to fix this error. So the main question is how can I make webpack work with /node_modules/cryptowatch-embed/src/main.js that is written in ES6?

Export Import Command Hazards in Nodejs

I am trying to use a machine learning library from Github. Using decision trees example code, I got the error message "Unexpected token declaration" on the following row
import { DecisionTreeRegression as DTRegression } from 'ml-cart';
Given that import command is not fully supported by nodejs, I changed the above line to
var DTRegression = require ('ml-cart');
Now nodejs is flagging error on the subsequent statement var reg = new DTRegression(); with message "DTRegression is not a constructor".
Looking through index.js file at ./node_modules/src/index.js, I can see the following code:
export { DecisionTreeClassifier } from './DecisionTreeClassifier';
export { DecisionTreeRegression } from './DecisionTreeRegression';
Then I tried directly referencing the ./node_modules/src/DecisionTreeRegression.js file using require command. That gave errors on 'export class' command inside DecisionTreeRegression.js.
Then I tried using the following command to make the import work.
node --experimental-modules myapp.mjs
I got the error "SyntaxError: The requested module 'ml-cart' does not provide an export named 'DecisionTreeRegression'".
Nodejs version is 10.3 on Ubuntu 16.04 LTS.
Any help would be highly appreciated.
Thanks
Best Regards,
Adeel
Try the mjs extension here
DecisionTreeRegression.mjs when importing with
node --experimental-modules myapp.mjs

Running JavaScript module returns SyntaxError: Unexpected token export

I'm trying to run a javascript package simple-statistics from
command-line via node
jupyter notebook via javascript kernel
In either case, I'm trying to import the module via:
var ss = require('simple-statistics')
but receiving the error:
C:\Users\...\simple-statistics\index.js:8
export { default as linearRegression } from './src/linear_regression';
^^^^^^
SyntaxError: Unexpected token export
I have tried multiple solutions such as
updating .babelrc to include multiple configurations of
{
"presets": ["latest"]
}
updating package.json to include code very similar to the above
pretty much everything I've found on SO
I'm new to Javascript, and to be honest it's a bit overwhelming navigating the sheer quantity of modules that may be related to the problem (babel, webpack, etc.).
I'm hoping someone can point me in the right direction, or help me what I assume is a simple issue. I'm drawing a blank. Thanks!
Solved (for this case).
As pointed out by loganfsmyth and Ruslan, I failed to create the distribution build.
Everything worked after running npm run build.

Categories