Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha keeps bombing due to absolute paths

Tags:

I'm having a great deal of trouble using Enzyme and Mocha to test my React project. I have a test like this:

import React from 'react';
import { expect } from 'chai';
import { shallow } from 'enzyme';

import { ChipInput} from '../client/components/Chips';

describe('<ChipInput />', _ => {
  it('rocks', done => {
    done();
  });
});

And when ChipInput gets imported, that file imports something with an absolute path, e.g. /lib/collections/tags, and then Mocha errors out because it apparently only does relative paths. How do I get this working?

EDIT:

The actual error:

Error: Cannot find module '/lib/collections/tags'

This happens because /tests/ChipInput-test.js imports the ChipInput component from /client/components/Chips/index.js, which has the following lines:

import React from 'react';
import {
  MapsLocalOffer as TagIcon,
  ImageRemoveRedEye as InsightIcon,
  EditorInsertChart as TestIcon,
  SocialPerson as UserIcon,
} from 'material-ui/svg-icons';

import { Tag } from '/lib/collections/tags'; // error thrown here
import { Insight } from '/lib/collections/insights';
// import { Test } from '/lib/collections/tests';
import Chip from './Chip';
import ChipDisplay from './ChipDisplay';
import ChipInput from './ChipInput';
import * as chipTypes from './chip-types';
like image 605
ffxsam Avatar asked May 14 '16 00:05

ffxsam


2 Answers

To anyone hitting here from google, while ffxsam's answer will work, there are many ways to accomplish this. Node's require allows for a base to be set either via environment variable or programmatically, allowing for simple absolute paths that don't require the leading slash (require("my/module"); vs require("/my/module");).

I use gulp as a taskrunner, so my preferred technique is to use app-module-path to do the following at the top of my gulpfile (this will work anywhere, so long as you haven't encountered any absolute requires yet):

require('babel-core/register'); //for mocha to use es6

require('app-module-path').addPath(__dirname + '/src'); //set root path

//I also use webpack to pull in other extensions, so I
//want mocha to noop out on them
require.extensions['.css'] = _.noop;
require.extensions['.scss'] = _.noop;
require.extensions['.png'] = _.noop;
require.extensions['.jpg'] = _.noop;
require.extensions['.jpeg'] = _.noop;
require.extensions['.gif'] = _.noop;

For a more complete rundown, check out this gist by github user branneman: https://gist.github.com/branneman/8048520

like image 74
MaxPRafferty Avatar answered Oct 11 '22 17:10

MaxPRafferty


This has been awhile, but I want to share my solution here, just in case, all solutions above don't work in your specific situation. I was looking for a solution to fix our unit test, which failed "Error: Cannot find module 'components/shared/xyz', our 'components' folder is under 'client/src' folder, so I came up with the following solution which works for us.

npm install babel-plugin-module-resolver --save-dev

{
  'plugins': [
      'babel-plugin-module-resolver',
      { 'root': ['client/src'] }
   ]
}
like image 33
Keith Socheath Chea Avatar answered Oct 11 '22 17:10

Keith Socheath Chea