generator-babel-boilerplate icon indicating copy to clipboard operation
generator-babel-boilerplate copied to clipboard

Tests break after removing the "default" property

Open atd opened this issue 6 years ago • 5 comments

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)

atd avatar Sep 25 '17 10:09 atd

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.

jamesplease avatar Sep 25 '17 15:09 jamesplease

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 avatar Sep 26 '17 07:09 atd

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

Nick-Lucas avatar Jul 28 '18 21:07 Nick-Lucas

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

atd avatar Jul 29 '18 16:07 atd

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!

Nick-Lucas avatar Jul 30 '18 08:07 Nick-Lucas