idx icon indicating copy to clipboard operation
idx copied to clipboard

Running Jest tests I got "The body of the arrow function supplied to `idx` must be a single expression"

Open kvarc7 opened this issue 9 years ago • 13 comments

The body of the arrow function supplied to `idx` must be a single expression (without curly braces).
        36 |            })
        37 |                    .then(res => {
      > 38 |                            const similarItems = idx(res, _ => _.data.similarDeals) || [];
           |                                                               ^
        39 |                            if (similarItems.length) {
        40 | 
        41 |                                    const converted = similarItems.map(item => {

kvarc7 avatar May 09 '17 09:05 kvarc7

@davscro What version of babel-plugin-idx are you using? This should have been fixed by 2b4c33ccd540d3766fafd32ce5ead506defc4b7f.

yungsters avatar May 10 '17 04:05 yungsters

hi @yungsters ,

"name": "babel-plugin-idx",
 "version": "1.5.1",

"name": "idx", 
"version": "1.5.0"

When I run tests directly from Webstorm everything is OK screen shot 2017-05-11 at 10 16 41 screen shot 2017-05-11 at 10 17 06

But from console with yarn test i got an error: Here is the test step: "test": "rimraf results && rimraf coverage && jest --config jest.config.json --coverage --bail" screen shot 2017-05-11 at 10 18 54

kvarc7 avatar May 11 '17 08:05 kvarc7

@davscro Can you share the contents of your jest.config.json?

yungsters avatar May 24 '17 03:05 yungsters

Sure @yungsters jest.config.json

	"setupTestFrameworkScriptFile": "<rootDir>/setup-jasmine-env.js",
	"coverageReporters": [
		"text-summary",
		"html",
		"cobertura"
	],
	"setupFiles": [
		"<rootDir>/test-setup.js"
	],
	"snapshotSerializers": [
		"<rootDir>/node_modules/enzyme-to-json/serializer"
	],
	"globals": {
		"__DEV__": true
	},
	"coverageThreshold": {
		"global": {
			"branches": 35,
			"functions": 50,
			"lines": 55,
			"statements": 50
		}
	}
}

setup-jasmine-env.js

/* eslint-disable no-undef */
const jasmineReporters = require('jasmine-reporters');

jasmine.VERBOSE = true;
jasmine.getEnv().addReporter(
	new jasmineReporters.JUnitXmlReporter({
		consolidateAll: false,
		savePath: './results',
		filePrefix: 'test-results'
	})
);

test-setup.js

window.matchMedia = window.matchMedia || function() {
	return {
		matches : false,
		addListener : function() {},
		removeListener: function() {}
	};
};

kvarc7 avatar May 25 '17 08:05 kvarc7

I have the same problem. When I remove --coverage from command, everything works just fine.

michalkvasnicak avatar May 26 '17 11:05 michalkvasnicak

Interesting! I wonder if a coverage plugin is injecting extra AST nodes into the arrow function.

Thanks for that insight. I'll need to brainstorm with @cpojer about how to best proceed.

yungsters avatar May 26 '17 15:05 yungsters

Is there anyway to work this around? I had the issue above a long time running, I thought to maybe implement a function identical to idx locally rather using the babel plugin.

jsdario avatar Jun 11 '17 18:06 jsdario

Hmm... this is an unfortunate collision between the two plugins. I don't have time to dig into this, but if we could identify the output of the coverage plugin, we can add a special case in babel-plugin-idx to ignore the extra nodes introduced by the coverage plugin.

Open to other ideas, too.

yungsters avatar Aug 29 '17 22:08 yungsters

Isn't idx just a "hopefully" temporary wrapper around the new optional chaining proposal? If there is currently a babel implementation of the optional chaining proposal, it would be interesting if jest coverage would work with them.

Correct me if I'm wrong, but at least to me it seems that the babel implementation shouldn't be that different?

JanPeter avatar Sep 07 '17 08:09 JanPeter

const f = () => 'a';

turns during jest run with coverage into

() => {/* istanbul ignore next */++cov_14mnsv87h8.f[0];++cov_14mnsv87h8.s[1];return 'a';}

So, when this transformation applied to idx's fake function argument, it violates idx's assertions (not a single stmt, curly braces, referencing values other than single argument's properties, etc).

dolphin278 avatar Oct 17 '17 18:10 dolphin278

Looks like babel 7 could solve this issue - it puts each plugin on separate compilation pass, which may prevent conflict between idx babel plugin and instanbul plugin, that included by babel-jest in babel config when we run jest with --coverage.

dolphin278 avatar Oct 18 '17 16:10 dolphin278

You can also use the following workaround https://github.com/facebook/jest/issues/3549#issuecomment-347915603

TL;DR use the idx as a package, but don't take the compilation step for the test phase

jsdario avatar Nov 29 '17 16:11 jsdario

This issue is almost a year old now. I'm going to close it out, but please do file a new one if this is still a problem (with Babel 7).

yungsters avatar Nov 16 '18 17:11 yungsters