Fails when proxyquire identifier used but not as a function #29

Closed
spudly opened this Issue Dec 22, 2016 · 4 comments

Projects

None yet

2 participants

@spudly
spudly commented Dec 22, 2016

In my old tests, there are a lot of palces where proxyquire.noCallThru() is called (once per test usually), but this causes an exception in jest-codemods (switching from ava to jest).

ERR test/utils/zipPackageTest.js Transformation error
TypeError: Cannot read property '0' of undefined
  at NodePath.ast.find.forEach.p (/Users/ssorense/.nvm/versions/node/v7.0.0/lib/node_modules/jest-codemods/dist/utils/proxyquire.js:46:37)
  ...

I poked around the code a little bit and it seems that this happens because the proxyquire util expects the proxyquire variable to be used only as a function. In this case, it's not used as a function and so the util fails when i tries to get the function args that don't exist.

It's easy enough to workaround - just remove the function call from tests before running the codemod, but I though you guys would want to know it's breaking in this instance (and other people might be interested in the workaround).

@skovhus
Owner
skovhus commented Dec 23, 2016

Thanks for reporting this. I'll have a look at fixing this.

Can you give me a snall code example and maybe what you expect the transformed output to look like?

@spudly
spudly commented Dec 23, 2016

In the transformed code, the call to noCallThru should simply be removed. It has no equivalent in jest and is not needed. I would expect some (very basic) code with proxyquire to be transformed as follows:

Before:

import proxyquire from 'proxyquire';
proxyquire.noCallThru();
const a = proxyquire('a', {b: 'c'});

After:

jest.mock('b', () => 'c');
const a = require('a');
@skovhus
Owner
skovhus commented Dec 23, 2016

Thanks! 👍

@skovhus skovhus closed this in 1d34f66 Dec 23, 2016
@skovhus
Owner
skovhus commented Dec 24, 2016

Bug fix released as 0.7.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment