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

__Rewire__ is not a function

Open stoplion opened this issue 4 years ago • 1 comments

A file has a few exported functions...

export function foo() {}
export function bar() {}
export function baz() {}

In a test file...

import * as pathFileHelpers from 'path/to/file';

pathFileHelpers.__Rewire__({
   readFileSync: () => {
      return 'test test test'
    },
})

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

Why?

SO Question

stoplion avatar Dec 10 '20 17:12 stoplion

I got the same, but I figure out that if the exported function are "heavy" enough, it works.

By example

function bar() {
	return 42;
}

export function foo() {
	return 60;
}

This doesn't work, but

function bar() {
	return 42;
}

export function foo() {
	return bar();
}

does work.

Even trickier:

function baz() { return bar(); }

function bar() { return 42; }

export function foo() {
	return 60;
}

work as well, even though the added code is unused and unreachable. But it looks like having inner calls is mandatory

BenjaminVanRyseghem avatar Aug 16 '21 09:08 BenjaminVanRyseghem