我正在使用玩笑和酶来测试我的反应成分。我还使用蓝图图标作为我的反应组件中的依赖项之一。作为我的 webpack 配置的一部分,添加了以下内容:
config.resolve.alias = {
blueprintIcons: path.resolve('./node_modules/@blueprintjs/icons'),
blueprint: path.resolve('./node_modules/@blueprintjs/core')
};
以下是作为 jest 配置的一部分添加的:
rootDir: '.',
roots: [
'<rootDir>/__test__/'
],
transformIgnorePatterns: [
'<rootDir>/node_modules/'
],
transform: {
'^.+\\.jsx?$': 'babel-jest'
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.jsx?$',
moduleDirectories: ['node_modules'],
moduleFileExtensions: [
'js',
'jsx',
'json',
'node'
],
moduleNameMapper: {
'\\.(css|scss)$': 'identity-obj-proxy',
blueprintIcons: '<rootDir>/node_modules/@blueprintjs/core'
blueprint: '<rootDir>/node_modules/@blueprintjs/core'
},
snapshotSerializers: ['enzyme-to-json/serializer']
};
这是我的组件:
import React, { Component } from 'react';
import Icon from 'blueprint';
import IconNames from 'blueprintIcons';
class Foo extends Component {
render() {
return (
<div>
<p>Hello Foo</p>
<Icon icon={IconNames.HOME} iconSize={Icon.SIZE_LARGE}/>
</div>
);
}
}
export default Foo;
这是我的 foo.test.js
import React from 'react';
import Foo from '../../src/Components/Foo';
import Adapter from 'enzyme-adapter-react-16';
import Enzyme, { mount, shallow } from 'enzyme';
describe('Reviews component', () => {
it('render component when loading in progress', () => {
const mountedComponent = mount(<Foo />);
});
});
当我尝试测试该组件时,测试失败
TypeError:无法读取 IconNames.HOME 未定义的属性“HOME”
这是我的 package.json 中指定的一些包
"babel-cli": "6.26.0",
"babel-core": "^6.26.3",
"babel-eslint": "^10.0.1",
"babel-jest": "^23.0.1",
"babel-loader": "7.1.4",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.1.1",
"enzyme-to-json": "^3.3.4",
"jest": "^23.1.0",
"jest-html-reporter": "^2.3.0",
"@blueprintjs/core": "^2.3.1",
"react": "16.2.0"
我正在使用反应 16.2.0
我尝试模拟它但不起作用(也许我没有正确执行它)但这是我正在使用的代码:
jest.mock('@blueprintjs/icons', () => (
{ IconNames: {HOME: 'home' }}));