基本的に、公式に認められた標準的なセットアップに従って、React アプリをテストしていcreate-react-app
ます。
私は次のようにテストを書きました:
import React from 'react';
import {shallow} from 'enzyme';
import MyComponent from './MyComponent';
describe('MyComponent', () => {
it('should have the expected properties', () => {
const wrapper = shallow(<MyComponent/>);
expect(wrapper.props()).toMatchObject({a: 1});
});
});
私のpackage.jsonには以下が含まれています:
{
...
"devDependencies": {
"enzyme": "^2.6.0",
"react-scripts": "0.8.3",
...
},
"dependencies": {
"react": "^15.4.1",
...
},
"scripts": {
"test": "react-scripts test --env=jsdom",
...
},
"jest": {
"automock": false
}
}
テストを実行すると、次のエラー メッセージが表示されますTypeError: expect(...).toMatchObject is not a function
。
ただし、react-scripts のドキュメントには、Jest が使用される組み込みツールの 1 つであると記載されています。たとえば、Jest を使用する必要はありませんnpm install
。Jestのドキュメントは、それtoMatchObject
が関数であることを明確に示しています。
では、なぜこのエラー メッセージが表示されるのでしょうか。