Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.62k stars 600 forks source link

TypeError: Super expression must either be null or a function #1734

Open pankajnegi1893 opened 10 months ago

pankajnegi1893 commented 10 months ago

I am getting below error when running my test case

Test suite failed to run

    TypeError: Super expression must either be null or a function

      13 |   @field('modified') modified!: string;
      14 |
    > 15 |   @field('whatsapp_setting_id') whatsappSettingId!: string;
         |                                ^
      16 | }
      17 |
      18 | export default ChatSettings;

      at _inherits (node_modules/@babel/runtime/helpers/inherits.js:4:11)
      at src/db/modals/ChatSettings.ts:15:32
      at Object.<anonymous> (src/db/modals/ChatSettings.ts:15:32)
      at Object.require (src/db/index.ts:3:1)
      at Object.require (src/db/controller/MessageDBController.ts:2:1)
      at Object.require (src/screens/WhatsappChat/Message.tsx:13:1)
      at Object.require (navigation/StackNavigators/AccountStack.tsx:30:1)
      at Object.require (navigation/StackNavigators/__tests__/AccountStack.test.tsx:6:1)

I have checked regarding this error but still getting same issue. below are the babel.config.js file I am having.

babel.config.js

module.exports = {
  presets: [
    'module:metro-react-native-babel-preset',
    '@babel/preset-typescript',
  ],
  plugins: [
    '@babel/plugin-proposal-export-namespace-from',
    'react-native-reanimated/plugin',
    ['@babel/plugin-proposal-decorators', {legacy: true}],
    ['@babel/plugin-proposal-class-properties', {loose: true}],
    [
      '@babel/plugin-transform-runtime',
      {
        helpers: true,
        regenerator: true,
      },
    ],
  ],
  env: {
    production: {
      plugins: ['transform-remove-console'],
    },
  },
};

tsconfig.json

{ "extends": "@tsconfig/react-native/tsconfig.json", "compilerOptions": { "experimentalDecorators": true, "emitDecoratorMetadata": true, "paths": { // this allows sub-package imports from src; fx. '@nozbe/watermelondb/decorators' "@nozbe/watermelondb/": ["./src/db/"] } }, "include": [ "./*.ts", "./src/*/.ts" // this is just used to validate everything, not just imported declarations ] }

package.json

"@nozbe/watermelondb": "^0.27.1", "@nozbe/with-observables": "^1.6.0",

pankajnegi1893 commented 10 months ago

After putting below code in jest setup file. it's working fine.

export const mockDatabase = {
  collections: {
    get: jest.fn(),
  },
};

jest.mock('@nozbe/watermelondb', () => ({
  ...jest.requireActual('@nozbe/watermelondb'),
  Modal: jest.fn(),
  Database: jest.fn(() => mockDatabase),
  Q: {
    sqlite: {
      openDatabase: jest.fn(),
    },
  },
}));
naumanshafique commented 9 months ago

After putting below code in jest setup file. it's working fine.

export const mockDatabase = {
  collections: {
    get: jest.fn(),
  },
};

jest.mock('@nozbe/watermelondb', () => ({
  ...jest.requireActual('@nozbe/watermelondb'),
  Modal: jest.fn(),
  Database: jest.fn(() => mockDatabase),
  Q: {
    sqlite: {
      openDatabase: jest.fn(),
    },
  },
}));

@pankajnegi1893 I have tried your solution but I'm getting this error:

TypeError: Cannot read properties of undefined (reading 'getRandomIds')

Screenshot 2024-02-02 at 5 26 08 PM

Do you have any idea how to fix this?

estebanfm commented 6 months ago

@MuhammadNaumanShafique I had the same error and updating to 0.27.1 fixed it for me