Skip to content

Commit

Permalink
feat: add automocking in vitest (#666)
Browse files Browse the repository at this point in the history
* feat: add automocking in vitest

* fix: integrate eslint and prettier correctly

When running the lint command, Eslint was displaying an error and not
working.

This commit fix this by:

- Updating dependencies by running `yarn upgrade`
- Setting up `eslint-plugin-prettier`

* fix: fix lint error due to dependencies in example
  • Loading branch information
dcdm3g committed Apr 30, 2024
1 parent ff26c4e commit 86d5e94
Show file tree
Hide file tree
Showing 7 changed files with 5,055 additions and 6,198 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ const buffer = storage.getBuffer('someToken')
console.log(buffer) // [1, 100, 255]
```

## Testing with Jest
## Testing with Jest or Vitest

A mocked MMKV instance is automatically used when testing with Jest, so you will be able to use `new MMKV()` as per normal in your tests. Refer to [example/test/MMKV.test.ts](example/test/MMKV.test.ts) for an example.
A mocked MMKV instance is automatically used when testing with Jest or Vitest, so you will be able to use `new MMKV()` as per normal in your tests. Refer to [example/test/MMKV.test.ts](example/test/MMKV.test.ts) for an example using Jest.

## Documentation

Expand Down
5,357 changes: 2,491 additions & 2,866 deletions example/yarn.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@
"@typescript-eslint/eslint-plugin": "^7.0.1",
"@typescript-eslint/parser": "^7.0.1",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-ft-flow": "^3.0.4",
"eslint-plugin-jest": "^27.8.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.7.0",
"metro-react-native-babel-preset": "^0.77.0",
"prettier": "^3.2.5",
Expand Down Expand Up @@ -138,7 +140,7 @@
"root": true,
"extends": [
"@react-native-community",
"prettier"
"plugin:prettier/recommended"
],
"plugins": [
"jest"
Expand Down
6 changes: 3 additions & 3 deletions src/MMKV.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createMMKV } from './createMMKV';
import { createMockMMKV } from './createMMKV.mock';
import { isJest } from './PlatformChecker';
import { isTest } from './PlatformChecker';

interface Listener {
remove: () => void;
Expand Down Expand Up @@ -131,7 +131,7 @@ export type NativeMMKV = Pick<
| 'recrypt'
>;

const onValueChangedListeners = new Map<string, ((key: string) => void)[]>();
const onValueChangedListeners = new Map<string, ((_key: string) => void)[]>();

/**
* A single MMKV instance.
Expand All @@ -147,7 +147,7 @@ export class MMKV implements MMKVInterface {
*/
constructor(configuration: MMKVConfiguration = { id: 'mmkv.default' }) {
this.id = configuration.id;
this.nativeInstance = isJest()
this.nativeInstance = isTest()
? createMockMMKV()
: createMMKV(configuration);
this.functionCache = {};
Expand Down
6 changes: 4 additions & 2 deletions src/PlatformChecker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export function isJest(): boolean {
export function isTest(): boolean {
if (global.process == null) {
// In a WebBrowser/Electron the `process` variable does not exist
return false;
}
return process.env.JEST_WORKER_ID != null;
return (
process.env.JEST_WORKER_ID != null || process.env.VITEST_WORKER_ID != null
);
}
9 changes: 7 additions & 2 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function useMMKV(configuration?: MMKVConfiguration): MMKV {
function createMMKVHook<
T extends (boolean | number | string | Uint8Array) | undefined,
TSet extends T | undefined,
TSetAction extends TSet | ((current: T) => TSet)
TSetAction extends TSet | ((current: T) => TSet),
>(getter: (instance: MMKV, key: string) => T) {
return (
key: string,
Expand Down Expand Up @@ -179,7 +179,12 @@ export const useMMKVBuffer = createMMKVHook((instance, key) =>
export function useMMKVObject<T>(
key: string,
instance?: MMKV
): [value: T | undefined, setValue: (value: T | undefined | ((prevValue: T | undefined) => T | undefined)) => void] {
): [
value: T | undefined,
setValue: (
value: T | undefined | ((prevValue: T | undefined) => T | undefined)
) => void,
] {
const [json, setJson] = useMMKVString(key, instance);

const value = useMemo(() => {
Expand Down

0 comments on commit 86d5e94

Please sign in to comment.