javascript - Why would babel-register results differ from babel (pre-transpiled)? -


with same source code, different runtime results pre-transpiling babel vs. transpiling babel-register on fly. when pre-transpiled code runs, operates correctly. when code transpiles babel-register fails.

same results in node 4.2 , 5.10

working:

babel ./ --ignore='node_modules/*,output/*' -d output mocha output/tests/unit --recursive --reporter spec 

not working:

mocha tests/unit --recursive --reporter spec --compilers js:babel-register 

347 tests @ moment.

when fails, 2 of tests fail, , fail exact same way - trying require file (different files). afaict, failing test suites , files requireing don’t have special or differentiating them.

more detail

  • the test suite uses mockery hook loader substitute mocks @ require time.
  • the test suite uses mockery in same fashion 21 times total, fails 2 uses.
  • the test suites still in es5.
  • at line of error, module 'require`d not mocked , es6. mocks subsequently required es6 file in es5. es5 requires es6 requires es5 mocks. how 21 tests run.
  • i tried prefixing failing command env babel_disable_cache=1

the symptomatic babel error "cannot read property 'delete' of undefined"

looking @ specific error in babel, appears weakmap reference cache.path undefined when delete method called. don’t yet understand how object reference possibly removed.

error stack:

/test-project/services/data/fetch.js: cannot read property 'delete' of undefined typeerror: cannot read property 'delete' of undefined   @ traverse.clearnode (node_modules/babel-traverse/lib/index.js:151:13)   @ function.traverse.cheap (node_modules/babel-traverse/lib/index.js:86:3)   @ function.traverse.removeproperties (node_modules/babel-traverse/lib/index.js:161:12)   @ getast (node_modules/babel-template/lib/index.js:63:40)   @ node_modules/babel-template/lib/index.js:85:24   @ pluginpass.exit (node_modules/babel-plugin-transform-es2015-modules-commonjs/lib/index.js:308:33)   @ newfn (node_modules/babel-traverse/lib/visitors.js:262:19)   @ nodepath._call (node_modules/babel-traverse/lib/path/context.js:63:18)   @ nodepath.call (node_modules/babel-traverse/lib/path/context.js:47:17)   @ nodepath.visit (node_modules/babel-traverse/lib/path/context.js:105:8)   @ traversalcontext.visitqueue (node_modules/babel-traverse/lib/context.js:152:16)   @ traversalcontext.visitsingle (node_modules/babel-traverse/lib/context.js:115:19)   @ traversalcontext.visit (node_modules/babel-traverse/lib/context.js:184:19)   @ function.traverse.node (node_modules/babel-traverse/lib/index.js:135:17)   @ object.traverse [as default] (node_modules/babel-traverse/lib/index.js:69:12)   @ node_modules/babel-core/lib/transformation/file/index.js:513:33   @ array.foreach (native)   @ file.transform (node_modules/babel-core/lib/transformation/file/index.js:510:23)   @ node_modules/babel-core/lib/transformation/pipeline.js:49:19   @ file.wrap (node_modules/babel-core/lib/transformation/file/index.js:528:16)   @ pipeline.transform (node_modules/babel-core/lib/transformation/pipeline.js:46:17)   @ object.transformfilesync (node_modules/babel-core/lib/api/node.js:124:10)   @ compile (node_modules/babel-register/lib/node.js:98:20)   @ loader (node_modules/babel-register/lib/node.js:126:14)   @ object.require.extensions.(anonymous function) [as .js] (node_modules/babel-register/lib/node.js:136:7)   @ function.hookedloader [as _load] (node_modules/mockery/mockery.js:111:12)   @ require (internal/module.js:16:19)   @ context.<anonymous> (tests/unit/services/data/fetch.js:21:13)   @ object.exports.default (node_modules/babel-template/lib/index.js:46:11)   @ object.<anonymous> (node_modules/babel-plugin-transform-es2015-modules-commonjs/lib/index.js:37:56)   @ object.require.extensions.(anonymous function) [as .js] (node_modules/babel-register/lib/node.js:134:7)   @ function.hookedloader [as _load] (node_modules/mockery/mockery.js:111:12)   @ require (internal/module.js:16:19)   @ object.<anonymous> (node_modules/babel-preset-es2015/index.js:22:5)   @ object.require.extensions.(anonymous function) [as .js] (node_modules/babel-register/lib/node.js:134:7)   @ function.hookedloader [as _load] (node_modules/mockery/mockery.js:111:12)   @ require (internal/module.js:16:19)   @ node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:22   @ array.map (native)   @ optionmanager.resolvepresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:364:20)   @ optionmanager.mergepresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:348:10)   @ optionmanager.mergeoptions (node_modules/babel-core/lib/transformation/file/options/option-manager.js:307:14)   @ optionmanager.addconfig (node_modules/babel-core/lib/transformation/file/options/option-manager.js:207:10)   @ optionmanager.findconfigs (node_modules/babel-core/lib/transformation/file/options/option-manager.js:413:16)   @ optionmanager.init (node_modules/babel-core/lib/transformation/file/options/option-manager.js:461:12)   @ compile (node_modules/babel-register/lib/node.js:81:45)   @ loader (node_modules/babel-register/lib/node.js:126:14)   @ object.require.extensions.(anonymous function) [as .js] (node_modules/babel-register/lib/node.js:136:7)   @ function.hookedloader [as _load] (node_modules/mockery/mockery.js:111:12)   @ require (internal/module.js:16:19)   @ context.<anonymous> (tests/unit/services/data/fetch.js:21:13) 

any advice appreciated.

i solved own issue.

here's happening:

  1. my app requires module via path './cache' (i have module named cache.js)
  2. babel-traverse/index.js requires own cache module (a weakmap) via path './cache'.
  3. my failing tests mock cache require using './cache' module identifier.
  4. the './cache' mocks being returned babel when require('./cache').

once mock setup (loader hooked), mock './cache' being returned babel-traverse when required './cache'. cache implementation not export weakmap, no 'delete' method there. thing, or have had more obscure problem.

matching on module identifier or path how mockery works.

this explains why worked precompiling babel , not running babel on fly. on fly, while tests run, mockery loader hook in effect.

using mockery babel tricky business. if must this:

  1. if have use babel-register (transpiling on fly) make sure module identifiers (the paths require might mock) unique app, or might supplying mocks babel well.
  2. if can, pre-transpile using babel cli.

anytime playing globals playing fire.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -