Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Not recognizing default parameters #216

Open
bashlyss opened this issue Apr 20, 2018 · 4 comments
Open

Not recognizing default parameters #216

bashlyss opened this issue Apr 20, 2018 · 4 comments
Labels

Comments

@bashlyss
Copy link

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
Copy link
Member

jdalton commented Apr 20, 2018

Hi @jamesloewen!

Would you be up for creating a PR?

@jdalton jdalton added the bug label Apr 20, 2018
@bashlyss
Copy link
Author

I can take a stab at it this weekend

@bashlyss
Copy link
Author

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
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants