mardi 26 mars 2019

How to enable babel-plugin-rewire in Meteor

I'm trying to use babel-plugin-rewire to mock a function inside another file. This function is not exported, but is called by the default export from that file.

Meteor 1.6.1 "babel-plugin-rewire": "^1.2.0" meteortesting:mocha@1.1.2

in my app's package.json:

  "babel": {
    "presets": ["latest", "meteor"],
    "env": {
      "test": {
        "plugins": [
          "babel-plugin-rewire"
        ]
      }
    }
  }

In my parentFunction.js:

import { some function } from 'anotherFile';

function childFunction() {
  ...
  return someValue;
}

export default function parentFunction() {
  return childFunction()
}

In my test file:

import { childFunction, __RewireAPI__ as MyRewireAPI } from './parentFunction'; // eslint-disable-line import/named

if (Meteor.isServer) {
  ...

  describe('parentFunction', () => {
    it('uses the mocked child function', () => {
      MyRewireAPI.__Rewire__('childFunction', function () {
        return Promise.resolve({ 'name': 'bob' });
            });
    });
  });
}

When I run the tests with this command:

TEST_WATCH=1 meteor test --driver-package meteortesting:mocha

All my other tests pass but this one fails with the error:

TypeError: Cannot read property '__Rewire__' of undefined

I thought the point of rewire is that it gets a non-exported module out of a file, so does this mean rewire isn't running? Is there something else I need to do to connect the rewire plugin with Meteor's built-in babel?

I've read the documentation and looked for other similar issues, and can't see what I'm doing wrong. I'd be very grateful for suggestions as to what simple thing I am missing here.

Aucun commentaire:

Enregistrer un commentaire