Security News
New Python Packaging Proposal Aims to Solve Phantom Dependency Problem with SBOMs
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
@formidable-webview/ersatz-testing
Advanced tools
🚀 Test React Native WebViews with @testing-library/react-native, jest and @formidable-webview/ersatz
:rocket: Test React Native WebViews
with
@testing-library/react-native
,
jest
and
@formidable-webview/ersatz
(written in Typescript :blue_heart:)
Assuming you already have jest
and
react-test-renderer
installed:
npm install -D @testing-library/react-native \
@formidable-webview/ersatz \
@formidable-webview/ersatz-testing
Ersatz
is the component mimicking WebView
behaviors. In the snippet bellow,
MyComponent
explicitly depends on Ersatz
. Of course, this does not reflect
real use cases which will be laid out later, but it is relevant for the purpose
of learning.
// ../../acceptance/ersatz-testing/src/basic.test.tsx
import * as React from 'react';
import Ersatz from '@formidable-webview/ersatz';
import { waitForWindow } from '@formidable-webview/ersatz-testing';
import { render } from '@testing-library/react-native';
import { WebViewProps } from 'react-native-webview';
const MyComponent = ({ source }: Pick<WebViewProps, 'source'>) => (
<Ersatz source={source} injectedJavaScript={'window.awesomeGlobal = 1;'} />
);
describe('MyComponent', () => {
it('should make awesomeGlobal available to window with value “1”', async () => {
const window = await waitForWindow(
render(<MyComponent source={{ html: '<div></div>' }} />)
);
expect(window.awesomeGlobal).toEqual(1);
});
});
Now, lets dive into a more realistic situation where MyComponent
depends on
WebView
directly. The two steps to get it work:
config/__mocks__/react-native-webview.js
file.jest.mock('react-native-webview');
at the top of my test file.More information on jest manual mocks here. The resulting files:
// ../../acceptance/ersatz-testing/config/__mocks__/react-native-webview.js
import Ersatz from '@formidable-webview/ersatz';
export default Ersatz;
// ../../acceptance/ersatz-testing/src/mock.test.tsx
jest.mock('react-native-webview');
import * as React from 'react';
import { waitForWindow } from '@formidable-webview/ersatz-testing';
import { render } from '@testing-library/react-native';
import { default as WebView, WebViewProps } from 'react-native-webview';
const MyComponent = ({ source }: Pick<WebViewProps, 'source'>) => (
<WebView source={source} injectedJavaScript={'window.awesomeGlobal = 1;'} />
);
describe('MyComponent', () => {
it('should make awesomeGlobal available to window with value “1”', async () => {
const window = await waitForWindow(
render(<MyComponent source={{ html: '<div></div>' }} />)
);
expect(window.awesomeGlobal).toEqual(1);
});
});
And voila!
FAQs
🚀 Test React Native WebViews with @testing-library/react-native, jest and @formidable-webview/ersatz
The npm package @formidable-webview/ersatz-testing receives a total of 6 weekly downloads. As such, @formidable-webview/ersatz-testing popularity was classified as not popular.
We found that @formidable-webview/ersatz-testing demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
Security News
Socket CEO Feross Aboukhadijeh discusses open source security challenges, including zero-day attacks and supply chain risks, on the Cyber Security Council podcast.
Security News
Research
Socket researchers uncover how threat actors weaponize Out-of-Band Application Security Testing (OAST) techniques across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.