1

I'm upgrading Babel from 6.26.0 to 7.8.3 and some of my tests are now breaking. It appears that the new version is preventing an exported function from being stubbed by Sinon after the upgrade.

Is there a configuration setting or plugin that I need to use as part of the upgrade to get the older behavior?

Here is an example of a test that is breaking:

it('a calls b', function () {
  bStub = sinon.stub(B, 'default');
  a.do();
  expect(bStub.calledOnce).to.be.true; // this used to be true but no is now false
});

The functions are mostly simple but I added some logging code and it appears that B() used to stub as expected when using the older version of Babel but it no longer gets stubbed successfully and the actual B() function is called.

The actual function under test: A.js

A.do = function () {
  return B();
};

export default A;

And the function call that is being stubbed out: B.js

export default function B () {
  ...
};

Some config changes I made as part of the upgrade:

project.json

  "devDependencies": {
+    "@babel/cli": "^7.8.3",
+    "@babel/core": "^7.8.3",
+    "@babel/preset-env": "^7.8.3",
-    "babel-cli": "^6.5.1",
-    "babel-core": "^6.26.0",
-    "babel-loader": "^7.1.2",
-    "babel-preset-es2015": "^6.5.0",
+    "babel-loader": "^8.0.6",
  ...
}

.babelrc

 {
-  "presets": ["es2015"]
+  "presets": ["@babel/preset-env"]
 }

webpack.config.json

 module: {
   rules: [
     {
       test: /\.js$/,
       exclude: /node_modules|scripts|dist|build\//,
       use: {
         loader: 'babel-loader',
         options: {
-            presets: ['es2015']
+            presets: ['@babel/preset-env']
         }
       }
     }
   ]
 }

Thanks!

Steven Lyons
  • 8,138
  • 1
  • 28
  • 27

2 Answers2

1

I was able to make this work again by adding an additional modules configuration setting to the babel-loader. My tests now pass as expected.

I was able to get the tests to pass using the umd and commonjs values but I ended up going with commonjs because that is the most compatible with the default from the previous version (as far as I could tell).

webpack.config.js

{
...
        use: {
          loader: 'babel-loader',
          options: {
            presets: [["@babel/preset-env", { "modules": "commonjs" }]]
          }
        }
...
};
Steven Lyons
  • 8,138
  • 1
  • 28
  • 27
0

Any updates? Exactly the same situation here. Could anyone throw light on this?

Except that I had to do the following in addition - install regenerator & core-js@3. As in Link

For the issue - regeneratorRuntime is not defined Link

Have tried both these methods - Link1

you can directly import "core-js" or use @babel/preset-env's useBuiltIns option.

Link2

in favor of directly including core-js/stable (to polyfill ECMAScript features) and regenerator-runtime/runtime

Have referred to Ref1, Ref2, Ref3, Ref4.

Finally, it seems that sinon stub behavior are not recognized -

TypeError: _common__WEBPACK_IMPORTED_MODULE_1__.functionName.withArgs is not a function

When debugging the stub behavior is not captured in the tests.

user3499836
  • 87
  • 1
  • 9
  • The answer has the solution I found, I hope it works for you as well. – Steven Lyons Feb 01 '20 at 06:35
  • the answer above works perfect! In addition the following webpack config was used in normal build (other than test/coverage) `{ loader: "babel-loader", options: { presets: [ [ "@babel/preset-env", { useBuiltIns: "usage", corejs: { version: 3, proposals: false }, modules: "umd" } ] ] } }` – user3499836 Feb 05 '20 at 23:21