Permalink
Browse files

Correctly handle proxyquire calls without arguments (fixes #29)

  • Loading branch information...
1 parent caa06b6 commit 1d34f66b7419e66a5d63236ddfad08ec26d1caf0 @skovhus committed Dec 23, 2016
Showing with 21 additions and 0 deletions.
  1. +7 −0 src/utils/proxyquire.js
  2. +14 −0 src/utils/proxyquire.test.js
@@ -34,6 +34,13 @@ export default function proxyquireTransformer(fileInfo, j, ast) {
const argumentPath = node.type === 'CallExpression' ? p.parentPath : p.parent.parent.parent;
const args = argumentPath.node.arguments;
+
+ if (!args) {
+ // proxyquire is called with no arguments
+ j(argumentPath).remove();
+ return;
+ }
+
const requireFile = args[0].value;
const mocksNode = args[1];
@@ -95,6 +95,20 @@ it('supports variable reference object', () => {
expect(mockedLogger).not.toBeCalled();
});
+it('supports empty noCallThru', () => {
+ const ast = j(`
+ import proxyquire from 'proxyquire';
+ proxyquire.noCallThru();
+ const a = proxyquire('a', {'b': 'c'});
+ `);
+ proxyquireTransformer(fileInfo, j, ast);
+ expect(ast.toSource({ quote: 'single' })).toEqual(`
+ jest.mock('b', () => 'c');
+ const a = require('a');
+ `);
+ expect(mockedLogger).not.toBeCalled();
+});
+
it('logs error when proxyquire mocks are not defined in the file', () => {
// TODO: this is kind of a bad state, but also a funny usage of proxyquire
const ast = j(`

0 comments on commit 1d34f66

Please sign in to comment.