Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Jest] _reactNativeMmkvStorage.MMKVLoader is not a constructor #285

Open
anhtuank7c opened this issue Sep 4, 2022 · 2 comments
Open

[Jest] _reactNativeMmkvStorage.MMKVLoader is not a constructor #285

anhtuank7c opened this issue Sep 4, 2022 · 2 comments

Comments

@anhtuank7c
Copy link

anhtuank7c commented Sep 4, 2022

Describe the bug
Jest test failed when initializing an instance. Application working fine except jest scenario.

To Reproduce
Steps to reproduce the behavior:

  1. Integrate with Mobx state tree
  2. initializing an instance in the model class
  3. define a test scenario
  4. See error
// Model class
import {Instance, SnapshotIn, SnapshotOut, types} from 'mobx-state-tree';
import {MMKVLoader} from 'react-native-mmkv-storage';

const devSettingStore = new MMKVLoader()
  .withEncryption()
  .withInstanceID('devSettings')
  .initialize();

/**
 * Model description here for TypeScript hints.
 */
export const FeatureFlagModel = types
  .model('FeatureFlag')
  .props({
    key: types.identifier,
    label: types.string,
    desc: types.optional(types.string, ''),
    selected: types.optional(
      types.union(types.union(types.string, types.undefined), types.null),
      undefined,
    ),
    defaultValue: types.string,
  })
  .views(self => ({
    get enabled() {
      return (self.selected || self.defaultValue) === 'enabled';
    },
    get defaultText() {
      return `Default (${self.defaultValue})`;
    },
  }))
  .views(self => ({
    get valueText() {
      return self.selected || self.defaultText;
    },
  }))
  .actions(self => ({
    loadCachedValue() {
      self.selected = devSettingStore.getString(self.key);
    },
    setValue(value: 'enabled' | 'disabled' | 'default' = 'default') {
      if (value === 'default') {
        self.selected = null;
        devSettingStore.removeItem(self.key);
        return;
      }
      devSettingStore.setString(self.key, value);
      self.selected = value;
    },
    reset() {
      self.selected = null;
      devSettingStore.removeItem(self.key);
    },
  }));
 
 // Jest test class
 import {FeatureFlagModel} from './feature-flag';

test('can be created', () => {
  const instance = FeatureFlagModel.create({
    key: 'test',
    label: 'Test',
    defaultValue: 'disabled',
  });

  expect(instance).toBeTruthy();
  expect(instance).toMatchSnapshot();
});

Expected behavior
Jest test working good.

Screenshots
image

Platform Information:

System:
    OS: macOS 12.5.1
    CPU: (8) x64 Apple M1
    Memory: 26.59 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.7.0 - /usr/local/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.7.0/bin/yarn
    npm: 8.15.0 - ~/.nvm/versions/node/v18.7.0/bin/npm
    Watchman: 2022.07.04.00 - /opt/homebrew/bin/watchman
  Managers:
    CocoaPods: 1.11.3 - /Users/tuannguyen/.rvm/gems/ruby-2.7.5/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.4, iOS 15.5, macOS 12.3, tvOS 15.4, watchOS 8.5
    Android SDK:
      API Levels: 28, 29, 30, 31, 32, 33
      Build Tools: 28.0.3, 29.0.2, 30.0.2, 30.0.3, 31.0.0, 32.0.0, 32.1.0
      System Images: android-30 | Wear OS 3 - Preview ARM 64 v8a, android-32 | Google APIs ARM 64 v8a, android-33 | Google Play ARM 64 v8a
      Android NDK: Not Found
  IDEs:
    Android Studio: 2021.2 AI-212.5712.43.2112.8512546
    Xcode: 13.4.1/13F100 - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.11 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 18.0.0 => 18.0.0 
    react-native: 0.69.4 => 0.69.4 
   react-native-mmkv-storage: ^0.8.0
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

I added an extra configuration to jest config.

https://rnmmkv.vercel.app/#/mockjest

@williamheelis
Copy link

I want to confirm: this is a major issue for us. Not sure what the cause is.

@williamheelis
Copy link

no jest unit test errors

import MMKVStorage from 'react-native-mmkv-storage';

const SecureStorage = new MMKVStorage.Loader()
    .withEncryption()
    .withInstanceId('someThing')
   .initialize();

hundreds of unit test errors

import { MMKVLoader } from 'react-native-mmkv-storage';

const SecureStorage = new MMKVLoader()
    .withEncryption()
    .withInstanceId('someThing')
   .initialize();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants