mfncooper / mockery

Simplifying the use of mocks with Node.js
Other
1.1k stars 60 forks source link

Mockery with Babel transpiled ES6 modules gives lots of warnings #35

Open sazzer opened 9 years ago

sazzer commented 9 years ago

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.

fustic commented 8 years ago

:+1:

fustic commented 8 years ago

@sazzer have you figured out any solution ?

sazzer commented 8 years ago

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.

danbucholtz commented 8 years ago

@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

fustic commented 8 years ago

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
Dakuan commented 8 years ago

@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;
stevemao commented 7 years ago

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