Skip to content

Commit

Permalink
fix: jest setup was voiding custom turbo mocks (#636)
Browse files Browse the repository at this point in the history
* fix: jest setup was voiding custom turbo mocks
* fix: properly scope test-specific mocks
* docs: document jest mocking on a dedicated page
  • Loading branch information
DoctorJohn authored Oct 7, 2024
1 parent b359292 commit 8866d8a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
14 changes: 14 additions & 0 deletions __tests__/banner.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import * as ReactNative from 'react-native';
import React from 'react';
import { render } from '@testing-library/react-native';
import { BannerAd, BannerAdSize } from '../src';

const MOCK_ID = 'MOCK_ID';

jest.doMock('react-native', () => {
return Object.setPrototypeOf(
{
...ReactNative,
Platform: {
OS: 'android',
select: () => {},
},
},
ReactNative,
);
});

describe('Google Mobile Ads Banner', function () {
it('throws if no unit ID was provided.', function () {
let errorMsg;
Expand Down
3 changes: 2 additions & 1 deletion docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
["European User Consent", "/european-user-consent"],
["Ad Inspector", "/ad-inspector"],
["Impression-level ad revenue", "/impression-level-ad-revenue"],
["Video ad volume control", "/video-ad_volume-control"]
["Video ad volume control", "/video-ad_volume-control"],
["Testing", "/testing"]
]
],
[
Expand Down
17 changes: 17 additions & 0 deletions docs/testing.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Testing

The React Native Google Mobile Ads library depends on a lot of platform-specific native code which you likely want to mock in your tests.

## Testing with Jest

This library ships with a Jest setup file that mocks the native code for you.
To use it, add the following to your Jest configuration:

```json
// jest.config.js|ts|mjs|cjs|json
{
"setupFiles": [
"./node_modules/react-native-google-mobile-ads/jest.setup.ts"
]
}
```
5 changes: 1 addition & 4 deletions jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import * as ReactNative from 'react-native';
jest.doMock('react-native', () => {
return Object.setPrototypeOf(
{
Platform: {
OS: 'android',
select: () => {},
},
NativeModules: {
...ReactNative.NativeModules,
RNAppModule: {
Expand All @@ -28,6 +24,7 @@ jest.doMock('react-native', () => {
RNGoogleMobileAdsConsentModule: {},
},
TurboModuleRegistry: {
...ReactNative.TurboModuleRegistry,
getEnforcing: () => {
return {
initialize: jest.fn(),
Expand Down

0 comments on commit 8866d8a

Please sign in to comment.