Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Rewire with TypeScript #166

Open
oargaruna opened this issue Oct 11, 2016 · 12 comments
Open

Using Rewire with TypeScript #166

oargaruna opened this issue Oct 11, 2016 · 12 comments

Comments

@oargaruna
Copy link

I am working on a React-Native project using TypeScript. To write my unit tests I would like to use the babel-plugin-rewire to mock my module imports. However, TypeScript adds a _1 suffix at the end of imports while converting from ES6 to ES5, and this breaks my test code.

Consider the following:

import Test from 'test-file';

this might be converted by TypeScript to:

var test_file_1 = require('test-file');

To mock the Test class using the Rewire plugin I would have to write:

ComponentToTest.__Rewire__('Test', TestMock);

but since the import has been renamed this will break.

Though this is by design, I would love to know if there are any workarounds.

Thanks.

@speedskater
Copy link
Owner

speedskater commented Oct 13, 2016

@oargaruna thanks for reporting this issue. As i am not very familiar with typescript my question might be stupid, but is it possible to transpile typescript to es6 and use babel to transpile to es5 (including babel-plugin-rewire)

Maybe this helps: http://stackoverflow.com/questions/30439869/can-typescript-compile-to-es6-code

@speedskater
Copy link
Owner

@oargaruna Did you try the intermediate step to compile to es6 ?

@oargaruna
Copy link
Author

Not yet. I was on vacation for the last two weeks :). I will try it out this weekend.

@speedskater
Copy link
Owner

@oargaruna any news on this subject?

@petrgazarov
Copy link

Hey @speedskater, a bit late to the conversation. Similar problem here -

is it possible to transpile typescript to es6 and use babel to transpile to es5 (including babel-plugin-rewire)

Yes, and I think this is the way to go here

However, one step further, I'm having an issue with the plugin exports -
none of the usual exports seem to be defined, e.g. __Rewire__, __ResetDependency__

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

All works well if I just rename a .ts file to .js file, confirming @blakeembrey's suggestion that babel-plugin-rewire does not get applied to .ts files.

From babel-register docs:

All subsequent files required by node with the extensions .es6, .es, .jsx and .js will be transformed by Babel.

Does that sound right to you? Have you had similar problems with extensions other than the above? Seems like it's not possible to configure babel extensions in .babelrc like I would normally do in webpack.

@knee-cola
Copy link

knee-cola commented Nov 17, 2017

I'm having similar problems ... as I learned by tracing the execution via debugger.

I did manage force it work by modifying my test. First here's the original line form the spec file:

__Rewire__(‘someModule’, { moduleMethod: aSpy );`

This was changed into the following:

__Rewire__(‘someModule’, { default: { moduleMethod: aSpy } });

Although test started to work fine after this hack, doing something like this isn't really usable ...

The interesting thing is that all works fine as long as both spec and the tested module are written in vanilla JS (they are not processed via a TS loader). However if I rewrite both files to TypeScript, things stop working.

@filipsuk
Copy link

filipsuk commented Mar 6, 2018

@oargaruna @knee-cola have you guys managed to get it working? I'm trying to use typescript + ts-jest + this plugin. I had problems with source maps and coverage.

@boazhoch
Copy link

boazhoch commented Jun 7, 2018

@filipsuk same here

@rivneglee
Copy link

Same here

@yinzara
Copy link

yinzara commented Jun 10, 2019

Still needs "TSInterfaceDeclaration" added as well

@yinzara
Copy link

yinzara commented Jun 11, 2019

Actually even that didn't fix it. If you have a typescript file such as the following:

export default interface Foo {
    bar: string
}

This plugin causes an error on any app start and throws an exception.

@yinzara
Copy link

yinzara commented Jun 11, 2019

@oargaruna @knee-cola have you guys managed to get it working? I'm trying to use typescript + ts-jest + this plugin. I had problems with source maps and coverage.

I'm not sure you can use ts-jest with this plugin as you're basically using two different transformations. You should use babel-jest instead with the @babel/preset-typescript preset instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants