facebook/idx

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

Closed this issue · 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 => {

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

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

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

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() {}
	};
};

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

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.

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.

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.

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?

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

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.

You can also use the following workaround jestjs/jest#3549 (comment)

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

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