mockery icon indicating copy to clipboard operation
mockery copied to clipboard

Mockery with Babel transpiled ES6 modules gives lots of warnings

Open sazzer opened this issue 8 years ago • 7 comments

When using Mockery to mock out dependencies of modules that are written in ES6 using Babel, there are a number of core Babel modules that get required automatically that need to be marked as allowed to avoid getting warnings.

The example I've found so far is using ES6 classes. When I use Mockery to mock out the dependencies of this module I still get:

WARNING: loading non-allowed module: babel-runtime/helpers/create-class
WARNING: loading non-allowed module: babel-runtime/core-js/object/define-property
WARNING: loading non-allowed module: core-js/library/fn/object/define-property
WARNING: loading non-allowed module: ../../modules/$
WARNING: loading non-allowed module: babel-runtime/helpers/class-call-check
WARNING: loading non-allowed module: babel-runtime/helpers/interop-require-default

I suspect there will be other core imports as well should other parts of ES6/Babel be used.

sazzer avatar Aug 21 '15 11:08 sazzer

:+1:

fustic avatar Nov 03 '15 16:11 fustic

@sazzer have you figured out any solution ?

fustic avatar Nov 03 '15 17:11 fustic

No, but then I haven't tried it with more recent versions of Babel or Mockery since then. All I did in the end was write a helper file to just call mockery.registerAllowable for a whole bunch of core modules.

sazzer avatar Nov 04 '15 09:11 sazzer

@sazzer @fustic Have you been able to get babel working with Mockery? I am using an older 5.x version of babel - still no dice.

I am running my mocha tests via Gulp's gulp-mocha plugin.

An example: I am testing a file ~//src/FileUploadManager.js. It has a dependency on ~/src/utils/FileStorageClient.js. I am mocking the require ("../utils/FileStorageClient") with mockery.

I immediately get a test failure that looks like this:

~/src/utils/FileStorageClient.js: false == true

AssertionError: src/utils/FileStorageClient.js: false == true at new Scope (node_modules/@brady/core-gulp/node_modules/babel-core/node_modules/regenerator/node_modules/recast/node_modules/ast-types/lib/scope.js:13:12)

The same tests pass if I use Proxyquire instead of Mockery, but Mockery just makes it so much easier for deep mocking.

Thanks for the input

danbucholtz avatar Nov 12 '15 20:11 danbucholtz

hi @danbucholtz. I dropped usage of Mockery and I switched to https://www.npmjs.com/package/a since we had a in the project anyway.

var fakeDep = {};

var expectRequire = require('a').expectRequire;
expectRequire('./realDep').return(fakeDep);

require('./realDep'); //returns fakeDep

fustic avatar Nov 12 '15 22:11 fustic

@danbucholtz @fustic I've managed to get proxyquire to work with es6 modules, bit messy though:

const subject = proxyquire("../../../../../src/server/data/util/cacheMoney", {
  "../../infrastructure/redis": {
    "default": {
      __esModule: true,
      get: () => console.log("get!!!!")
    }
  },
  "ramda": {
    isNil: () => console.log("nil"),
  }
}).default;

Dakuan avatar Nov 23 '15 10:11 Dakuan

By default all warnings should be disabled. Otherwise it's too noisy and potentially doesn't play well with other modules such as babel.

stevemao avatar Jan 10 '17 06:01 stevemao