speedskater / babel-plugin-rewire

A babel plugin adding the ability to rewire module dependencies. This enables to mock modules for testing purposes.
843 stars 90 forks source link

Block-scoped declarations not yet supported outside strict mode #168

Closed 01binary closed 7 years ago

01binary commented 7 years ago

I am getting the following error when running mocha unit tests:

Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

This is being caused by babel-register attempting to compile:

let __RewireAPI__ = {};

My interpretation of this error is that because we have 'use strict' at the top of all modules, the code injected by babel-plugin-rewire (which contains let __RewireAPI__ = {}; somewhere) causes an error when compiled by babel-register because let is not supported in strict mode.

The closest issue that describes something similar is this one: https://github.com/speedskater/babel-plugin-rewire/issues/71#issuecomment-185254037

Some kind of related commit is here: https://github.com/speedskater/babel-plugin-rewire/commit/ddfea829fa39a53dc725956e4655db1b70c52bc7

As you can tell, I don't have a good eagle-eye view of the whole babel pipeline, so it's hard to reason about what's going wrong at which stage. In my package.json I have:

"babel-plugin-rewire": "^1.0.0"

Perhaps I got an old version from NPM because the package was renamed, and the fix for my issue is in a new version somewhere?

Thank you!

01binary commented 7 years ago

Adding transform-es2015-block-scoping plug-in before babel-plugin-rewire in .babelrc appears to have fixed this issue. I also added the require for babel-plugin-rewire in our Gruntfile.js to have mocha load the plug-in when running unit tests (otherwise it wasn't able to find the definition for Rewire, etc).

For posterity...

Before

.babelrc

{
    "presets": ["react"]
    "plugins": ["babel-plugin-rewire"]
}

Gruntfile.js

mochaTest: {
            unit: {
                src: src.testjs,
                options: {
                    clearRequireCache: true,
                    reporter:'mocha-jenkins-reporter',
                    reporterOptions: {
                        'junit_report_path': require('path').join('results/', 'mocha-unit.xml')
                    },
                    require: [
                        'babel-register',
                        'setup.js'
                    ]
                }
            }
        },

After

.babelrc

{
    "presets": ["react"],
    "plugins": ["transform-es2015-block-scoping", "babel-plugin-rewire"]
}

Gruntfile.js

mochaTest: {
            unit: {
                src: src.testjs,
                options: {
                    clearRequireCache: true,
                    reporter:'mocha-jenkins-reporter',
                    reporterOptions: {
                        'junit_report_path': require('path').join('results/', 'mocha-unit.xml')
                    },
                    require: [
                        'babel-register',
                        'babel-plugin-rewire',
                        'setup.js'
                    ]
                }
            }
        },