
Research
Security News
Lazarus Strikes npm Again with New Wave of Malicious Packages
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
mock-async-storage
Advanced tools
Its a mock of react-native AsyncStorage for jest tests
npm install --save mock-async-storage
require('mock-async-storage')
This package is published with the following editions:
mock-async-storage/src/index.js
is Source + ESNext + Import + Flowmock-async-storage
aliases mock-async-storage/lib/index.js
mock-async-storage/lib/index.js
is Babel Compiled + ES2015 + RequireOlder environments may need Babel's Polyfill or something similar.
I removed the jest specific part from the mock lib. In the next version the mocking method is not predefined. You can use any kind of library (sinon) for use this mock.
I suggest to use jest manual mocks Jest Manual Mocks For demonstrate this solution you can find an example in examples folder.
const MockAsyncStorage = require('mock-async-storage');
// or import { mock, release } from 'mock-async-storage';
// mock();
// release();
const mock = () => {
const mockImpl = new MockAsyncStorage()
jest.mock('AsyncStorage', () => mockImpl)
}
const release = () => jest.unmock('AsyncStorage')
mock();
// For unmock
mockStorage.release();
Working example:
import 'react-native';
import MockAsyncStorage from 'mock-async-storage'
import React from 'react';
import Index from '../index.android.js';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
const mock = () => {
const mockImpl = new MockAsyncStorage()
jest.mock('AsyncStorage', () => mockImpl)
}
mock();
import { AsyncStorage as storage } from 'react-native'
it('renders correctly', () => {
const tree = renderer.create(
<Index />
);
});
it('Mock Async Storage working', async () => {
await storage.setItem('myKey', 'myValue')
const value = await storage.getItem('myKey')
expect(value).toBe('myValue')
})
This is for the current verison
In your test codes:
const mockStorage = require('mock-async-storage');
// or import { mock, release } from 'mock-async-storage';
// mock();
// release();
// For mock AsyncStorage
mockStorage.mock();
// For unmock
mockStorage.release();
Working example:
import 'react-native';
import { mock, release } from 'mock-async-storage'
import React from 'react';
import Index from '../index.android.js';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
mock()
import { AsyncStorage as storage } from 'react-native'
it('renders correctly', () => {
const tree = renderer.create(
<Index />
);
});
it('Mock Async Storage working', async () => {
await storage.setItem('myKey', 'myValue')
const value = await storage.getItem('myKey')
expect(value).toBe('myValue')
})
FAQs
Its a mock of react-native AsyncStorage for jest tests
The npm package mock-async-storage receives a total of 11,019 weekly downloads. As such, mock-async-storage popularity was classified as popular.
We found that mock-async-storage demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.
Security News
Opengrep continues building momentum with the alpha release of its Playground tool, demonstrating the project's rapid evolution just two months after its initial launch.