[SOLVED] Mocha is unhappy about absolute paths and meteor imports


#1

I’m using Enzyme and Mocha to test my React components, and I’ve run into two major problems with it:

  1. Absolute paths when using import are not supported.
  2. As soon as an import { xyz } from 'meteor/some:package is encountered, it’s all over.

How do I address these issues? @SkinnyGeek1010, @sam?


#2

PS: I’m using strictly npm-based testing frameworks (enzyme, sinon, chai, mocha), not practicalmeteor:mocha.


#3

How do these tools deal with the non-standard import syntax in Webpack? There must be some ways to plug in various ways to interpret module paths.


#4

One would think so. You’re asking the wrong guy, though… still zero experience with webpack (no time, currently). :tired_face: :slight_smile:


#5

Re problem 2), is this the issue or am I misunderstanding: Meteor 1.3 testing - with “meteor/meteor:x” package imports? Not sure if you’re using the Meteor test runner, but if you are then that thread is relevant I think.


#6

Thanks, Tom! That looks like it’ll solve the issue of Mocha choking on Meteor package imports. It doesn’t address the fact that my imports are mostly absolute paths, which Mocha doesn’t like. I suppose I could go through my whole project and change everything to relative, but I’ve got a pretty deep folder hierarchy, and would wind up with imports like:

import Thing from '../../../../client/components/Thing';

There must be a way to get Mocha to recognize absolute paths!

EDIT: To be clear, I’m referring to the import statements in my actual code, not the Mocha tests.


#7

Hey, did you try https://github.com/mantrajs/babel-root-slash-import?


#8

Holy crap, THANK YOU. This appears to work, as the test no longer throws an error on the importing from /lib/collections anymore. So now it’s throwing an error here:

Error: Cannot find module 'meteor/mongo'
    at Function.Module._resolveFilename (module.js:438:15)
    at Function.Module._load (module.js:386:25)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (tags.js:1:1)
    at Module._compile (module.js:541:32)
...

Which is understandable. The proxyquire solution that @tomRedox posted above should address the importation of Meteor packages.

Thanks again!!


#9

Yeah, arunoda developed this for testing in mantra via external mocha back with Meteor 1.2 :slight_smile:


#10

Hi, @ffxsam How did you manage to solve the issue? I’m having the exact same problem. Can’t get the test to work.

Error: Cannot find module 'meteor/mongo’
at Function.Module._resolveFilename (module.js:440:15)
at Function.Module._load (module.js:388:25)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object. (posts.js:1:1)
at Module._compile (module.js:541:32)

I tried the solutions mentioned above but still can’t get it to work.


#11

Oops… nevermind, I got it to work. :slight_smile:


#12

What did you do? I wound up just using the meteor test instead of npm test.


#13

In my case, What happened was I had a schema defined together with the collection on one file. So when I imported the schema it also includes import {Mongo} from 'meteor/mongo';. So what I did was just separate the collection and the schema into two files and that fixed my problem.

I’m using Mantra, btw.

Hope it could help solve your problem. :slight_smile: