lodash / babel-plugin-lodash

Modular Lodash builds without the hassle.
Other
1.96k stars 91 forks source link

Not recognizing default parameters #216

Open bashlyss opened 6 years ago

bashlyss commented 6 years ago
import _ from 'lodash';

const someObj = {
  fn: (onClick=_.noop) {
    /* do something */
    onClick();
  }
}

When running this in certain environments (specifically jest with --coverage flag), this code sample result in TypeError: Cannot read property 'noop' of undefined

This started happening when I added this plugin

jdalton commented 6 years ago

Hi @jamesloewen!

Would you be up for creating a PR?

bashlyss commented 6 years ago

I can take a stab at it this weekend

bashlyss commented 6 years ago

Well, I tried reproducing this in the test environment here and couldn't immediately find out how to reproduce. I'll take another chance when I have he chance at the office when I have access to the repo I first encountered it in. For reference if others want to try to reproduce, this is the babelrc in use at the time.

Issue only comes up when running jest with coverage turned on. (Something somewhere is getting it to a bad state that this repo can't handle, but not sure where and which repo is actually responsible for the bug).

{
    "plugins": ["babel-plugin-webpack-alias", "lodash"],
    "presets": [
      "react",
      ["env", {
        "targets": { "browsers": ["> 1%"] },
        "modules": false,
        "useBuiltins": true,
      }],
    ],
    "env": {
      "test": {
        "presets": [
          [
            "env", { "modules": "commonjs" }
          ]
        ]
      },
      "development": {
        "plugins": ["react-hot-loader/babel"]
      },
      "production": {
        "plugins": ["transform-react-remove-prop-types"],
      }
    }
}
ilya-pyatin commented 4 years ago

Hi, I'm facing the same issue. I was able to create a minimum playground that reproduces this issue:

package.json

{
  "name": "",
  "version": "",
  "scripts": {
    "test": "jest",
    "test:coverage": "./node_modules/.bin/jest --coverage"
  },
  "devDependencies": {
    "babel": "^6.23.0",
    "babel-core": "^6.24.1",
    "babel-jest": "^22.4.0",
    "babel-plugin-lodash": "^3.2.11",
    "babel-preset-env": "^1.6.1",
    "jest": "22.4.0",
    "regenerator-runtime": "^0.13.3"
  },
  "jest": {
    "testURL": "http://localhost/"
  }
}

.babelrc

{
  "presets": [
    [
      "env",
    ],
  ],
  "plugins": [
    "lodash"
  ]
}

src/filter.js

import _ from 'lodash';

export function filter(a, b = _.filter) {
  return b(a, function(o) { return o === 1; });
}

src/filter.test.js

import {filter} from './filter';

test('', () => {
  expect(filter([1, 2])).toEqual([1]);
});

When I run npm run test:coverage I get the "TypeError: Cannot read property 'filter' of undefined". (Everything is fine with npm run test).

And if I remove the usage of the plugin the issue disappears.

Hope this helps.