React Nativeプロジェクトの単体テストを書くときに、異なるプラットフォームに基づいて異なるスナップショットをテストできるようにしたいと考えています。React Native:Jest Mocking Platform
私はまずjest.mock
を模擬してPlatform
にしようとしましたが、非同期であるようです。このアプローチは2つの別々のファイルがある場合には機能しますが、可能であればすべてを1つのファイルに保存したいと考えています。
Iは理由ドキュメントからこのスニペットのjest.doMock
を試みた:
をバベル、冗談を使用する場合、自動的にコード・ブロックの先頭に掲揚するモックの呼び出し。この動作を明示的に回避するには、このメソッドを使用します。 https://facebook.github.io/jest/docs/en/jest-object.html#jestdomockmodulename-factory-options
しかし、私はまだ望ましくない結果を見ています。アンドロイドテストでconsole.log
が表示されたら、Platform.OS
は最初に設定したものです。doMock
です。私はそれが http://facebook.github.io/jest/docs/en/setup-teardown.html#scoping
describe('ios test',() => {
it('renders ui correctly',() => {
jest.doMock('Platform',() => {
const Platform = require.requireActual('Platform');
Platform.OS = 'ios';
return Platform;
});
const wrapper = shallow(<SomeComponent />);
const tree = renderer.create(wrapper).toJSON();
expect(tree).toMatchSnapshot();
});
});
describe('android test',() => {
it('renders ui correctly',() => {
jest.doMock('Platform',() => {
const Platform = require.requireActual('Platform');
Platform.OS = 'android';
return Platform;
});
const wrapper = shallow(<SomeComponent />);
const tree = renderer.create(wrapper).toJSON();
expect(tree).toMatchSnapshot();
});
});
私は同じファイルにテストのための模擬プラットフォームを変更する方法上の任意のアイデアをスコープに役立つかもしれないと思ったbecasue
は、私はまた、describe
に
beforeEach
でモックをラップしようとしましたか?