babel-plugin-rewire icon indicating copy to clipboard operation
babel-plugin-rewire copied to clipboard

TypeError: _get__(...).__REWIRE__ is not a function

Open codejet opened this issue 8 years ago • 53 comments

No matter what I try with exports and imports, I always get:

TypeError: _get__(...).__REWIRE__ is not a function

I added the plugin to our .babelrc for the tests and babel-plugin-rewire.js definitely gets executed.

I couldn't find any really similar issue. Am I missing something obvious?

codejet avatar Mar 15 '16 09:03 codejet

@codejet is it possible that you have a type in your code and use REWIRE instead of Rewire (https://github.com/speedskater/babel-plugin-rewire). I would even recommend to use the set method. Does this solution help you?

speedskater avatar Mar 15 '16 10:03 speedskater

thanks @speedskater, but it doesn't seem to matter whether REWIRE or Rewire...

TypeError: _get__(...).__Rewire__ is not a function

codejet avatar Mar 15 '16 11:03 codejet

I also have what appears to be the same problem:

undefined is not a constructor (evaluating '_get__('consoleLogger').__Rewire__('getWindow', function () {
          return windowMock;
        })')

undefined is not a constructor seems to happen when using either phantom or mocha, not sure which, but basically it's complaining because

moduleName.__Rewire__

is not a function. Note this was working fine until I reinstalled my npm modules, then it broke.

robin-barnes-triptease avatar Mar 15 '16 16:03 robin-barnes-triptease

@codejet , @robin-barnes-triptease thank you. Could one of you please create a PR with a minimum sample, similar to the ones existing in the repo?

speedskater avatar Mar 16 '16 07:03 speedskater

@codejet I am experiencing the same problem with PhantomJS but not in Chrome with karma running my tests. Did you get around fixing this? CC: @robin-barnes-triptease

mmahalwy avatar Mar 16 '16 18:03 mmahalwy

@mmahalwy no, unfortunately i didn't. we are using mocha here.

codejet avatar Mar 17 '16 13:03 codejet

+1

uriklar avatar Mar 23 '16 08:03 uriklar

a colleague of mine investigated, and according to him babel-register prevents rewiring of modules living inside node_modules in our case.

codejet avatar Mar 23 '16 08:03 codejet

Same issue here using babel 6, mocha, karma on chrome with rc1:

import * as ConfigService from "../src/services/configService";

const getEnvironmentConfigurationMock = () => Promise.resolve({});

ConfigService.__Rewire__("getEnvironmentConfiguration", getEnvironmentConfigurationMock);

where configService:

var environmentConfig = undefined;

export function getEnvironmentConfiguration () {
        ... return some form of promise
    });
}

jonashartwig avatar Mar 23 '16 09:03 jonashartwig

I had the same problem, but manage to fix it.

In your case @jonashartwig, it will be something like, switching: import * as ConfigService from (...)
To: import { getEnvironmentConfiguration, __RewireAPI__ as configServiceRewireApi } from (...)

And finally in the code configServiceRewireApi.__Rewire__("getEnvironmentConfiguration", (...)

Hope it helps.

ModuloM avatar Mar 24 '16 14:03 ModuloM

Ran into this today:

import * as things from './myThings.js';
...
things.__Rewire__(...)

Throws: TypeError: _get__('things').__REWIRE__ is not a function

I wanted to keep the import * as syntax, so I found a workaround by importing the things and the API separately:

import * as things from 'myThings.js';
import { __RewireAPI__ as ThingsRewireAPI } from 'myThings.js';
...
ThingsRewireAPI.__Rewire__(...)

spncrlkt avatar Mar 24 '16 21:03 spncrlkt

It seems you can't rewire a dependency that is not used. This caused the issue for me.

It's a little confusing when doing TDD, cause you might want to rewire a service before anything actually uses it.

// notice Foo is never used, so babel doesnt bother loading it
import Foo from "./foo"
export default class Bar {
}
import Bar from "./bar"

Bar.__Rewire__('Foo', 1); // TypeError: _get__(...).__Rewire__ is not a function

This can be fixed by adding a reference to the import anywhere in your code

import Foo from "./foo"

Foo; // now the module will be included

export default class Bar {
}

joshnuss avatar Mar 28 '16 18:03 joshnuss

@spncrlkt, @ModuloM, @robin-barnes-triptease , @jonashartwig The behaviour regarding wildcard imports is intentional. The reason is that if we would add the rewire api to the wildcard import it could break the contract of modules expecting certain exports.

@joshnuss You are right, rewiring a dependency which is not used or does not exist causes an error at the moment. I think we should be able to ignore such cases, as rewiring not existing dependencies won't change anything. @TheSavior what do you think? Anyway @joshnuss could you please create a PR for a failing usage test?

speedskater avatar Mar 28 '16 19:03 speedskater

I am still getting this error with as simple as:

import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { asyncConnect } from 'redux-async-connect';

import Grid from 'react-bootstrap/lib/Grid';
import Row from 'react-bootstrap/lib/Row';
import Col from 'react-bootstrap/lib/Col';

import { load } from 'redux/modules/image_attributions';

@asyncConnect([{
  promise({ store: { dispatch } }) {
    return dispatch(load());
  }
}])
@connect(
  state => ({
    imageAttributions: state.imageAttributions.entities
  })
)
export default class ImageAttribution extends Component {
  static propTypes = {
    imageAttributions: PropTypes.array
  };

  render() {
    return (
      <Grid style={{marginTop: '140px'}}>
      </Grid>
    );
  }
}

and test:

import ImageAttributions from './index';

console.log(ImageAttributions); // function Connect(props, context) { ... }
console.log(ImageAttributions.__GetDependency__); // undefined
console.log(ImageAttributions.__Rewire__); // undefined

This only happens with phantomjs and only when I have export default and using babel 6:

{
  "presets": ["react", "es2015", "stage-0"],
  "plugins": [
    "transform-runtime",
    "add-module-exports",
    "transform-decorators-legacy",
    "transform-react-display-name"
  ],
  "env": {
    "test": {
      "plugins": [
        "rewire"
      ]
    },

My work around has been:

import ImageAttributions, { __RewireAPI__ as ImageAttributionsApi } from './index';

which is weird since export default should already have the apis.

mmahalwy avatar Apr 13 '16 23:04 mmahalwy

@mmahalwy maybe this could be related to the decorators in your sample. What happens if your separate the declaration of the component and the export?

speedskater avatar Apr 26 '16 07:04 speedskater

i can give it a try and report back

mmahalwy avatar Apr 28 '16 22:04 mmahalwy

@speedskater getting same problem with non decoratored file

mmahalwy avatar May 19 '16 00:05 mmahalwy

I found declaring an anonymous function as default export directly caused this error, whereas assigning to a const and exporting that worked.

e.g. doesn't work:

default export ({ path, func }) => {
    function request(params) {
    //do stuff
    }
    return { request };
};

does work

const method = ({ path, func }) => {
  function request(params) {
    //do stuff
  }
  return { request };
};
export default method;

asgarddesigns avatar Jun 23 '16 00:06 asgarddesigns

@asgarddesigns thanks for figuring this out. Could you please create a PR with a failing sample with your example?

speedskater avatar Jul 05 '16 06:07 speedskater

I found the same thing as @asgarddesigns. However, it is still related to the import not being used because adding a reference to the unused import fixed the problem.

BenoitAverty avatar Jul 07 '16 14:07 BenoitAverty

@speedskater sorry, been on holidays. Will try and push something up when I get a chance (an remember what I was actually doing!).

asgarddesigns avatar Jul 10 '16 23:07 asgarddesigns

I encountered an issue that may be related (version 1.0.0-rc-3, using it with webpack).

I have two modules, both of which consist of a number of exported functions. However, when I imported them, __Rewire_API__ worked fine for one, but not the other.

When trying to track down the difference between my modules, I discovered that while __Rewire_API__ is defined when importing this module:

const foo = 'TEST';
export const bar = () => {
  return foo;
};

It is undefined (both as a default import and as a named import) while importing this one:

export const bar = () => {
  return 'TEST (not Foo!)';
};

This also does not work (notice that foo is defined, but unused):

const foo = 'bar';
export const bar = () => {
  return 'TEST (not Foo!)';
};

Here is the module that I imported the three of these with:

import { bar, __RewireAPI__ as RewireBar } from './bar';
console.log('bar:', bar());
console.log('rewire:', RewireBar);

So it seems that you need at least one top level variable that is used within the module for __RewireAPI__ to work when that module is imported.

I suppose that makes sense, as there would be nothing for rewire to, well, rewire, but it occurred during development and I couldn't figure out for the life of me what was happening, so if it would be possible to have a descriptive error, or simply have __Rewire_API__ work anyways, that would be great.

Let me know if this is the right place for this or if I can provide any more detail.

liamfd avatar Jul 13 '16 16:07 liamfd

@liamfd Do you still have this problem with the latest version (1.0.0-rc-4)

TheSavior avatar Jul 13 '16 21:07 TheSavior

@TheSavior unfortunately yes, the issue still seems to be present with 1.0.0-rc-4.

liamfd avatar Jul 13 '16 21:07 liamfd

The problem is still present in 1.0.0-rc-5. I included rewire as a plugin in .babelrc. Then tried mocha tests without modifications and it fails: TypeError: _get__(...) is not a function . Tests were working fine before, without rewire plugin. I am using mocha 2.5.3 with babel.

selection_004

panzerdp avatar Aug 07 '16 17:08 panzerdp

I still got this error in 1.0 doing something like this:

PushQueue.js

export default class PushQueue {
  // ...
}

PushQueue.spec.js

const MockQueueItem = function MockQueueItem(action, payload) {
  console.log('should be called', action, payload);
  return { action, payload };
};

import PushQueue from './PushQueue';

describe('PushQueue', function () {
  beforeEach(function () {
    PushQueue.__Rewire__('PushQueueItem', MockQueueItem);
    this.queue = new PushQueue();
  });

  afterEach(function () {   
    PushQueue.__ResetDependency__('PushQueueItem');
  });

  // tests are here ...
});

As @mmahalwy said: __Rewire__ and __ResetDependency__ are undefined.

When I also import the __RewireAPI__ from the PushQueue.js file I can call these functions there but it doesn't change the import when calling a function that uses the PushQueueItem.

Is there some update about this issue?

tchock avatar Oct 03 '16 19:10 tchock

@tchock thanks for providing the sample. Could you please provide the complete code of PushQueue so that i can try to tackle the issue asap?

speedskater avatar Oct 10 '16 13:10 speedskater

@speedskater of course Code: https://github.com/Hypheme/harmonized.js/blob/feature/base_transporter/src/BaseTransporter/PushQueue.js Spec: https://github.com/Hypheme/harmonized.js/blob/feature/base_transporter/src/BaseTransporter/PushQueue.spec.js

This is the webpack config for this: https://github.com/Hypheme/harmonized.js/blob/feature/base_transporter/test/unit/webpack.config.js

Commented the rewire code in the specs out because it didn't work.

tchock avatar Oct 16 '16 15:10 tchock

@tchock Thanks for providing the complete code and sorry for my delay but I have been on holidays over the last 3 weeks. I will have a look at the code tomorrow in the evening and let you know whether I can figure something out.

speedskater avatar Nov 09 '16 08:11 speedskater

@tchock I have not found your problem till now (trying to create a sample project but i think i will have to do it over the weekend). But maybe your issue is related to issue #93 and the proposed fix in the sample project https://github.com/danawoodman/webpack-babel-rewire-bug-example/pull/1

speedskater avatar Nov 10 '16 23:11 speedskater