Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mock-async-storage

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-async-storage - npm Package Compare versions

Comparing version 2.2.0 to 2.9.0

.github/workflows/node.js.yml

99

package.json
{
"name": "mock-async-storage",
"title": "Jest Mock AsyncStorage for react-native",
"version": "2.2.0",
"version": "2.9.0",
"description": "Its a mock of react-native AsyncStorage for jest tests",
"main": "lib/index.js",
"main": "lib/index.cjs",
"type": "module",
"keywords": [

@@ -13,22 +14,10 @@ "jest",

],
"editions": [
{
"description": "Source + Typescript",
"entry": "src/index.ts",
"syntaxes": [
"typescript"
]
},
{
"description": "Babel Compiled + ESNext + Require",
"entry": "lib/index.js",
"syntaxes": [
"import",
"javascript",
"esnext",
"require",
"async"
]
}
],
"tsup": {
"entry": [
"src/index.ts"
],
"splitting": false,
"sourcemap": true,
"clean": true
},
"author": "Metál Ádám <devmetal91@gmail.com> (https://github.com/devmetal)",

@@ -39,46 +28,21 @@ "repository": {

},
"badges": {
"list": [
"travisci"
]
},
"license": "MIT",
"scripts": {
"tsc": "tsc",
"tdd": "jest src/** --watch",
"lint": "standard src/*.ts src/**/*.ts",
"test:src": "jest src/**",
"test": "yarn run tsc && yarn run lint && yarn run test:src && yarn run examples",
"build": "babel src --out-dir lib --ignore spec.ts --extensions .ts",
"projectz": "projectz compile",
"compile": "yarn run lint && yarn run build && yarn run projectz",
"example-jest": "jest ./examples/with-jest/**",
"example-sinon": "jest ./examples/with-sinon/**",
"examples": "yarn run example-jest && yarn run example-sinon",
"prepublish": "yarn run build"
},
"engines": {
"node": ">=8"
"node": ">=18"
},
"devDependencies": {
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.4",
"@babel/plugin-proposal-class-properties": "^7.4.4",
"@babel/plugin-proposal-object-rest-spread": "^7.4.4",
"@babel/preset-env": "^7.4.4",
"@babel/preset-typescript": "^7.3.3",
"@babel/runtime": "^7.4.4",
"@types/jest": "^24.0.12",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.7.1",
"jest": "^24.7.1",
"sinon": "^4.0.0",
"standard": "^12.0.1",
"typescript": "^3.4.5"
"@eslint/js": "^9.3.0",
"@types/jest": "^29.5.12",
"eslint": "^9.3.0",
"globals": "^15.3.0",
"jest": "^29.7.0",
"sinon": "^18.0.0",
"ts-jest": "^29.1.3",
"tsup": "^8.0.2",
"typescript": "^5.4.5",
"typescript-eslint": "^7.10.0",
"@types/sinon": "^17.0.3"
},
"standard": {
"parser": "babel-eslint"
},
"dependencies": {
"deepmerge": "^3.3.0"
"deepmerge": "^4.3.1"
},

@@ -94,3 +58,16 @@ "maintainers": [],

"url": "https://github.com/devmetal/mock-async-storage/issues"
},
"scripts": {
"tsc": "tsc",
"build": "tsup -d ./lib --dts",
"tdd": "jest src/** --watch",
"lint": "eslint ./src/*.ts ./src/**/*.ts",
"test:src": "jest src/**",
"test": "pnpm run compile && pnpm run test:src && pnpm run examples",
"compile": "pnpm run lint && pnpm run build",
"example-jest": "jest ./examples/with-jest/**",
"example-sinon": "jest ./examples/with-sinon/**",
"examples": "pnpm run example-jest && pnpm run example-sinon",
"prepublish": "pnpm run build"
}
}
}

@@ -1,26 +0,10 @@

<!-- TITLE/ -->
<h1>Mock AsyncStorage for react-native</h1>
<h1>Jest Mock AsyncStorage for react-native</h1>
<!-- /TITLE -->
<!-- BADGES/ -->
<span class="badge-travisci"><a href="http://travis-ci.org/devmetal/mock-async-storage" title="Check this project's build status on TravisCI"><img src="https://img.shields.io/travis/devmetal/mock-async-storage/master.svg" alt="Travis CI Build Status" /></a></span>
<!-- /BADGES -->
[![Standard - JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
<!-- DESCRIPTION/ -->
Its a mock of react-native AsyncStorage for jest tests
<!-- /DESCRIPTION -->
<h2>ES modules</h2>
From 2.9.x version, added esmodules support.
<!-- INSTALL/ -->
I suggest to use this library with **setupFilesAfterEnv** configuration. Its more stable and during my tests its working as expected.

@@ -30,8 +14,6 @@ <h2>Install</h2>

<a href="https://npmjs.com" title="npm is a package manager for javascript"><h3>NPM</h3></a><ul>
<li>Install: <code>npm install --save mock-async-storage</code></li>
<li>Module: <code>require('mock-async-storage')</code></li>
<li>Import: <code>import MockAsyncStorage from 'mock-async-storage'</code></li></ul>
<!-- /INSTALL -->
<h1>Quick Jest Example</h1>

@@ -56,3 +38,13 @@

<h2>@react-native-async-storage/async-storage</h2>
```JavaScript
import MockAsyncStorage from "mock-async-storage";
jest.mock(
"@react-native-async-storage/async-storage",
() => new MockAsyncStorage()
);
```
<h1>mock-async-storage@2.x.x</h1>

@@ -59,0 +51,0 @@

@@ -1,2 +0,2 @@

import merge from 'deepmerge'
import merge from "deepmerge";

@@ -7,14 +7,14 @@ export type Entry<K, V> = [K, V | null];

export type ErrBack<V> = (err: Error | null, val?: V | null) => {};
export type ErrBack<V> = (err: Error | null, val?: V | null) => void;
export type ArrErrBack<V> = (err: Array<Error> | null, val?: V) => {};
export type ArrErrBack<V> = (err: Array<Error> | null, val?: V) => void;
const isStringified = (str: string): boolean => {
try {
JSON.parse(str)
return true
JSON.parse(str);
return true;
} catch (e) {
return false
return false;
}
}
};

@@ -24,59 +24,65 @@ class AsyncDict<K, V> {

size (): number {
return this.store.size
size(): number {
return this.store.size;
}
getStore (): Map<K, V | null> {
return new Map(this.store)
getStore(): Map<K, V | null> {
return new Map(this.store);
}
constructor () {
this.store = new Map()
constructor() {
this.store = new Map();
}
async getItem (k: K, cb?: ErrBack<V>): Promise<V | null> {
const val = this.store.get(k) || null
if (cb) cb(null, val)
return val
async getItem(k: K, cb?: ErrBack<V>): Promise<V | null> {
const val = this.store.get(k) || null;
if (cb) cb(null, val);
return val;
}
async setItem (k: K, v: V, cb?: ErrBack<V>): Promise<void> {
this.store.set(k, v)
if (cb) cb(null)
async setItem(k: K, v: V, cb?: ErrBack<V>): Promise<void> {
this.store.set(k, v);
if (cb) cb(null);
}
async removeItem (k: K, cb?: ErrBack<V>): Promise<void> {
this.store.delete(k)
if (cb) cb(null)
async removeItem(k: K, cb?: ErrBack<V>): Promise<void> {
this.store.delete(k);
if (cb) cb(null);
}
async clear (cb?: ErrBack<V>): Promise<void> {
this.store.clear()
if (cb) cb(null)
async clear(cb?: ErrBack<V>): Promise<void> {
this.store.clear();
if (cb) cb(null);
}
async getAllKeys (cb?: ErrBack<Array<K>>): Promise<Array<K>> {
const keys: Array<K> = Array.from(this.store.keys())
if (cb) cb(null, keys)
return keys
async getAllKeys(cb?: ErrBack<Array<K>>): Promise<Array<K>> {
const keys: Array<K> = Array.from(this.store.keys());
if (cb) cb(null, keys);
return keys;
}
async multiGet (keys: Array<K>, cb?: ErrBack<Entries<K, V>>): Promise<Entries<K, V>> {
const requested: Entries<K, V> = keys.map(k => [k, this.store.get(k) || null])
if (cb) cb(null, requested)
return requested
async multiGet(
keys: Array<K>,
cb?: ErrBack<Entries<K, V>>
): Promise<Entries<K, V>> {
const requested: Entries<K, V> = keys.map((k) => [
k,
this.store.get(k) || null,
]);
if (cb) cb(null, requested);
return requested;
}
async multiSet (entries: Entries<K, V>, cb?: ErrBack<V>): Promise<void> {
async multiSet(entries: Entries<K, V>, cb?: ErrBack<V>): Promise<void> {
for (const [key, value] of entries) {
this.store.set(key, value)
this.store.set(key, value);
}
if (cb) cb(null)
if (cb) cb(null);
}
async multiRemove (keys: Array<K>, cb?: ErrBack<V>): Promise<void> {
async multiRemove(keys: Array<K>, cb?: ErrBack<V>): Promise<void> {
for (const key of keys) {
this.store.delete(key)
this.store.delete(key);
}
if (cb) cb(null)
if (cb) cb(null);
}

@@ -86,29 +92,36 @@ }

class MockAsyncStorage extends AsyncDict<string, string> {
async mergeItem (key: string, value: string, cb?: ErrBack<string>): Promise<void> {
const item: string | null = await this.getItem(key)
async mergeItem(
key: string,
value: string,
cb?: ErrBack<string>
): Promise<void> {
const item: string | null = await this.getItem(key);
if (!item) throw new Error(`No item with ${key} key`)
if (!isStringified(item)) throw new Error(`Invalid item with ${key} key`)
if (!isStringified(value)) throw new Error(`Invalid value to merge with ${key}`)
if (!item) throw new Error(`No item with ${key} key`);
if (!isStringified(item)) throw new Error(`Invalid item with ${key} key`);
if (!isStringified(value))
throw new Error(`Invalid value to merge with ${key}`);
const itemObj: Object = JSON.parse(item)
const valueObj: Object = JSON.parse(value)
const merged: Object = merge(itemObj, valueObj)
const itemObj: unknown = JSON.parse(item);
const valueObj: unknown = JSON.parse(value);
const merged: unknown = merge(itemObj, valueObj);
await this.setItem(key, JSON.stringify(merged))
await this.setItem(key, JSON.stringify(merged));
if (cb) cb(null)
if (cb) cb(null);
}
async multiMerge (entries: Entries<string, string>, cb?: ArrErrBack<string>): Promise<void> {
const errors: Array<Error> = []
/* eslint no-restricted-syntax: "off" */
/* eslint no-await-in-loop: "off" */
async multiMerge(
entries: Entries<string, string>,
cb?: ArrErrBack<string>
): Promise<void> {
const errors: Array<Error> = [];
for (const [key, value] of entries) {
try {
if (value) {
await this.mergeItem(key, value)
await this.mergeItem(key, value);
}
} catch (err) {
errors.push(err)
errors.push(err as Error);
}

@@ -118,13 +131,13 @@ }

if (errors.length) {
if (cb) cb(errors)
return Promise.reject(errors)
if (cb) cb(errors);
return Promise.reject(errors);
}
if (cb) cb(null)
return Promise.resolve()
if (cb) cb(null);
return Promise.resolve();
}
flushGetRequests () {}
flushGetRequests() {}
}
export default MockAsyncStorage
export default MockAsyncStorage;

@@ -5,3 +5,3 @@ {

"paths": { "*": ["types/*"] },
"target": "esnext",
"target": "ESNext",
"moduleResolution": "node",

@@ -8,0 +8,0 @@ "allowJs": true,

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc