thlorenz / proxyquireify

browserify >= v2 version of proxyquire. Mocks out browserify's require to allow stubbing out dependencies while testing.
MIT License
152 stars 24 forks source link

ES7 Spread not working with proxyquireify #59

Closed nehaleem closed 8 years ago

nehaleem commented 8 years ago

Issue: When using spread operator in tests, proxyquireify cannot parse these files.

Error:

{ [Error: Line 59: Unexpected token {]
  index: 1260,
  lineNumber: 59,
  column: 38,
  description: 'Unexpected token {' }
Error: Line 59: Unexpected token {
    at throwError (/home/user/test-project/node_modules/proxyquireify/node_modules/detective/node_modules/esprima-fb/esprima.js:2154:21)

Config: --karma.conf.js:

const karmaConfig = {
    basePath: '.',
    urlRoot: '/',
    frameworks: [ 'browserify', 'jasmine' ],
    port: 3002,
    reporters: [ 'dots' ],
    browsers: [ 'PhantomJS' ],
    files: [ './base/**/*.spec.js' ],
    preprocessors: {
        './base/**/*.spec.js': [ 'browserify' ],
    },
    autoWatch: true,
    singleRun: false,
    colors: true,
    browserify: {
        debug: false,
        transform: [ 'babelify' ],
        plugin: [ proxyquire.plugin ],
        extensions: [ '.js' ],
    },
};

--.babelrc:

{
  "presets": [ "es2015", "react", "stage-0" ],
  "plugins": [
    "transform-decorators-legacy"
  ]
}

Versions:

"babel-core": "^6.7.0",
"babel-plugin-external-helpers-2": "^6.3.13",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-polyfill": "^6.3.14",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"babelify": "^7.2.0",
"babelify-external-helpers": "^1.1.0",
"browserify": "^13.0.0",
"jasmine-co": "^1.1.0",
"karma": "^0.13.19",
"karma-browserify": "^5.0.1",
"karma-jasmine": "^0.3.7",
"karma-phantomjs-launcher": "^1.0.0",
"phantomjs-prebuilt": "^2.1.3",
"proxyquireify": "^3.1.1",
"watchify": "^3.7.0"

Test file:

import React from 'react';

const proxyquire = require('proxyquireify')(require);

describe('Some test', () => {
    let MyComponent = null;

    beforeEach(() => {
        MyComponent = proxyquire('components/MyComponent', {
            'OtherComponent': {
                default () {
                    return <input type="text" />;
                },
            },
        }).default;
    });

    it('should not throw error', () => {
        const params = {
            var1: 1,
            var2: 2
        };

        console.log({...params}); //<= this will produce error
        // Rest of code is not needed, because here esprima throws an error
    });
});
bendrucker commented 8 years ago

You need to make sure babelify is running before proxyquireify. Otherwise invalid JS gets passed along to something that isn't expecting it. This isn't a proxyquireify issue. You should manually configure karma-browserify instead of passing tranform and plugin arrays to guarantee order.