我在使用 Enzyme 和 Mocha 测试我的 React 项目时遇到了很多麻烦。我有一个这样的测试:
import React from 'react';
import { expect } from 'chai';
import { shallow } from 'enzyme';
import { ChipInput} from '../client/components/Chips';
describe('<ChipInput />', _ => {
it('rocks', done => {
done();
});
});
当ChipInput
被导入时,该文件会导入具有绝对路径的内容,例如/lib/collections/tags
,然后 Mocha 出错,因为它显然只执行相对路径。我如何让这个工作?
编辑:
实际错误:
Error: Cannot find module '/lib/collections/tags'
发生这种情况是因为从/tests/ChipInput-test.js
导入ChipInput
组件/client/components/Chips/index.js
,该组件具有以下几行:
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';