speedskater / babel-plugin-rewire

A babel plugin adding the ability to rewire module dependencies. This enables to mock modules for testing purposes.
842 stars 90 forks source link

Error when exported function contains other functions. #155

Closed jseminck closed 8 years ago

jseminck commented 8 years ago

Hello,

babel-rewire-plugin mocking fails when the exported function contains another function definition. I will create a PR with a failing test case to investigate, but basically, when trying to mock out the variable test using the syntax fileName.__set__({test: 2000}); for the following file, this error is returned:

Error TypeError: _get__(...).set is not a function

File:

const test = 0

export default function getTestValue() {
  return addOne(test);

  function addOne(value) {
    return value + 1;
  }
}

However, the following approaches work:

const test = 0

export default function getTestValue() {
  const addOne = function(value) {
    return value + 1;
  }

  return addOne(test);
}

And

const test = 0

export default function getTestValue() {
    return addOne(test);
}

function addOne(value) {
  return value + 1;
}

Cheers,

speedskater commented 8 years ago

@jseminck thanks for opening this issue together with the PR describing the problem. It should be fixed in rc6. Could you please let me know if it works for you.

jseminck commented 8 years ago

Hi. Thanks for the fast reply. Indeed this seems to work now, but we cannot upgrade due to compatibility issue with `babel-plugin-rewire', see issue https://github.com/speedskater/babel-plugin-rewire/issues/148