Stub exported functions [Resolved]

While trying to write a unit test, I got stuck while trying to stub an exported function. An example of what I’m trying to achieve can be found in this git repo.

The example follows the solution offered here:

I already tried to replace sinon with spyOn (jasmine) (following the sanjo:jasmine migration approach) but this doesn’t solve the problem.

Thx in advance for all efforts to help out!

1 Like

I’ve made some try with babel-plugin-rewire

.babelrc

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

then fix dev dependencies in package.json

“devDependencies”: {
“babel-core”: “^6.0.0”,
“babel-template”: “^6.2.0”,
“babel-traverse”: “^6.2.0”,
“babel-types”: “^6.2.0”,
“babel-plugin-rewire”: “^1.0.0-rc-6”,
“babel-plugin-transform-export-extensions”: “^6.8.0”,
“babel-preset-meteor”: “^6.12.0”
}

meteor npm install
meteor test --driver-package practicalmeteor:mocha

foo.js

 function message() {
    return 'Hello world';
 }

 export function foo() {
    return message();
 }

module_to_test.test.js

import FooModule from './foo.js';
import { foo, __RewireAPI__ as FooModuleRewireAPI } from './foo.js';

describe('module default export test', function() {
   it('should demonstrate the default exported rewire api', function() {
      expect( foo() ).to.equal('Hello world');
      FooModule.__Rewire__('message', function() {
         return 'my message';
      });
      expect( foo() ).to.equal('my message');
      FooModule.__ResetDependency__('message');
   });

   it('should demonstrate the rewire apis named export', function() {
      expect( foo() ).to.equal('Hello world');
      FooModuleRewireAPI.__Rewire__('message', function() {
         return 'my message';
      });
      expect( foo() ).to.equal('my message');
      FooModuleRewireAPI.__ResetDependency__('message');
   });
});

This validates the use of babel-plugin-rewire in meteor but does not still solve your problem yet …

1 Like

Got it :

exported_function.js

 function message() {
   return 'real result';
 }
 export function foo() {
   return message();
 }

module_to_test.test.js

import {chai} from 'meteor/practicalmeteor:chai'
import FooModule from './exported_function.js';
import { foo, __RewireAPI__ as FooModuleRewireAPI } from './exported_function.js';
import { bar } from './module_to_test.js';

const stubbedResult = 'stubbed result';

describe('module default exported rewire api', function() {
 before(function() {
    FooModule.__Rewire__('message', function() {
       return stubbedResult;
   });
 });
 after(function() {
      FooModule.__ResetDependency__('message');
});
 it('should demonstrate the default exported rewire api', function() {
    expect( foo() ).to.equal(stubbedResult);
 });
 it('should also work for bar', function() {
    const result = bar();
    expect(result).to.equal(stubbedResult);
 });
});

describe('module rewire apis named export', function() {
  before(function() {
      FooModuleRewireAPI.__Rewire__('message', function() {
         return stubbedResult;
      });
  });
  after(function() {
     FooModuleRewireAPI.__ResetDependency__('message');
  });
  it('should demonstrate the rewire apis named export', function() {
    expect( foo() ).to.equal(stubbedResult);
  });
  it('should also work for bar', function() {
    const result = bar();
    expect(result).to.equal(stubbedResult);
  });
 });

:persevere:

PR sent : rewired by lc3t35 · Pull Request #1 · MadMaxedy/meteor-test-stub-imported-function · GitHub

2 Likes

W00t @lc3t35 thx for your effort, much appreciated! I’ll look into it later this week!

I checked it out, and it works pretty well, thx again @lc3t35! The only minor thing remaining is this approach requires an additional function, but I think that’s an acceptable trade off.

2 Likes

this is still a relevant topic today. crazy but somehow I’ve spent all day today trying to get this technique in place for one of my apps.

tried proxyquire, rewire, rewiremock and several others which all fail for Meteor. and worse all lead to threads that are unresolved and dead 4 yours ago.

you just saved my day @lc3t35