babel/generator-babel-boilerplate

Tests break after removing the "default" property

Opened this issue · 5 comments

atd commented

After following the last FAQ How can I export my library without the "default" property? (installing babel-plugin-add-module-exports and changing .babelrc), gulp test shows the following error:

$ gulp test               
[12:48:34] Using gulpfile ~/dev/cartodb.js/gulpfile.js
[12:48:34] Starting 'lint-src'...
[12:48:34] Starting 'lint-test'...
[12:48:34] Starting 'lint-gulpfile'...
[12:48:35] Finished 'lint-src' after 329 ms
[12:48:35] Finished 'lint-gulpfile' after 209 ms
[12:48:35] Finished 'lint-test' after 297 ms
[12:48:35] Starting 'lint'...
[12:48:35] Finished 'lint' after 9.63 μs
[12:48:35] Starting 'test'...
[12:48:35] 'test' errored after 299 ms
[12:48:35] TypeError in plugin 'gulp-mocha'
Message:
    /home/atd/dev/cartodb.js/test/setup/node.js: Property right of AssignmentExpression expected node to be of a type ["Expression"] but instead got null
Details:
    _babel: true
Stack:
TypeError: /home/atd/dev/cartodb.js/test/setup/node.js: Property right of AssignmentExpression expected node to be of a type ["Expression"] but instead got null
    at Object.validate (/home/atd/dev/cartodb.js/node_modules/babel-types/lib/definitions/index.js:109:13)
    at Object.validate (/home/atd/dev/cartodb.js/node_modules/babel-types/lib/index.js:505:9)
    at NodePath._replaceWith (/home/atd/dev/cartodb.js/node_modules/babel-traverse/lib/path/replacement.js:176:7)
    at NodePath._remove (/home/atd/dev/cartodb.js/node_modules/babel-traverse/lib/path/removal.js:58:10)
    at NodePath.remove (/home/atd/dev/cartodb.js/node_modules/babel-traverse/lib/path/removal.js:30:8)
    at PluginPass.CallExpression (/home/atd/dev/cartodb.js/node_modules/babel-plugin-transform-es2015-modules-amd/lib/index.js:42:12)
    at NodePath._call (/home/atd/dev/cartodb.js/node_modules/babel-traverse/lib/path/context.js:76:18)
    at NodePath.call (/home/atd/dev/cartodb.js/node_modules/babel-traverse/lib/path/context.js:48:17)
    at NodePath.visit (/home/atd/dev/cartodb.js/node_modules/babel-traverse/lib/path/context.js:105:12)
    at TraversalContext.visitQueue (/home/atd/dev/cartodb.js/node_modules/babel-traverse/lib/context.js:150:16)

If you build the lib, then look at the dist file, what does it appear to be exporting? You may need to adjust the tests to account for what’s exported.

atd commented

These are the last lines of the dist file

          var cartodb = {
            greet: function greet() {
              return 'hello';
            }
          };
        
          exports.default = cartodb;
          module.exports = exports['default'];

Without the babel-plugin-add-module-exports:

        var cartodb = {
          greet: function greet() {
            return 'hello';
          }
        };
        
        exports.default = cartodb;

In any case, the only generated unit test doesn't seem to be related with the dist folder, but the src folder. Maybe there is a babel transformation in between?

import cartodb from '../../src/cartodb.js';

describe('cartodb', () => {
  describe('Greet function', () => {
    beforeEach(() => {
      spy(cartodb, 'greet');
      cartodb.greet();
    });

    it('should have been run once', () => {
      expect(cartodb.greet).to.have.been.calledOnce;
    });

    it('should have always returned hello', () => {
      expect(cartodb.greet).to.have.always.returned('hello');
    });
  });
});

@atd did you manage to resolve this? I am facing the same issue and am lost

atd commented

Hi @Nick-Lucas I cannot remember the details but it seems I switched to module.exports

https://github.com/atd/cartodb.js/blob/master/src/cartodb.js#L114-L116

Ahhh gotcha, thanks!

I also managed to solve it. I had added transform-es2015-modules-umd as shown in the FAQ, but later realised this was just part of the example and removed it from my own code. This was the plugin which was breaking and the build works great without it.

User error!