Navigation Menu

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

Getting TypeError: Cannot read property 'prefix' of undefined, test only #321

Closed
harshaliitr opened this issue Jan 12, 2021 · 6 comments
Closed

Comments

@harshaliitr
Copy link

I am setting up jest in my project and get this error:
TypeError: Cannot read property 'prefix' of undefined at Object.<anonymous> (node_modules/react-native-agora/lib/src/RtcChannel.native.js:10:38) at Object.<anonymous> (node_modules/react-native-agora/lib/src/RtcEngine.native.js:3:1)

Do you have a mock available?

@harshaliitr
Copy link
Author

harshaliitr commented Jan 12, 2021

Solved it like this -

// create a file called react-native-agora-mock.js with the folllowing content -
`class RtcEngine {
constructor() {}
}

class RtcChannel {
constructor() {}
}

const View = require('react-native/Libraries/Components/View/View');

const RtcLocalView = View
const RtcRemoteView = View

export default RtcEngine;
export { RtcChannel, RtcLocalView, RtcRemoteView };`

// in jest.setup.js
jest.mock('react-native-agora', () => require('./testHelpers/react-native-agora-mock'));

// in package json jest block -
"setupFiles": [ "<rootDir>/jest.setup.js", "./node_modules/react-native-gesture-handler/jestSetup.js" ]

@harshaliitr
Copy link
Author

But you can still make this module testable.
Are there any plans of doing that?

@LichKing-2234
Copy link
Collaborator

the test is just for mocking, it will be a low priority.

@ajenkins
Copy link

ajenkins commented Jan 30, 2021

@harshaliitr I'm getting the warning Possible Unhandled Promise Rejection: TypeError: _reactNativeAgora.default.create is not a function. That's triggered when I call RtcEngine.create(appId) from my code. Have you figured out a way to mock the create() function?

UPDATE: I think I figured it out. I ended up going with a different solution. Instead of creating a new file that mocks agora-react-native, I mocked it directly in jest.setup.js. Here's what I did:

// jest.setup.js

jest.mock('react-native-agora', () => {
  return {
    __esModule: true,
    default: {
      create: jest.fn(() => ({
        addListener: jest.fn(),
        destroy: jest.fn(),
        enableVideo: jest.fn(),
        removeListener: jest.fn(),
        // Add any additional methods on RtcEngine that you use
      })),
    },
  };
});

And if you don't already have a jest.setup.js file, you can create a new file with that name and use it within your jest.config.js file like so (or in your package.json, however you configure jest should work):

module.exports = {
  preset: 'react-native',
  setupFiles: ['<rootDir>/jest.setup.js'],
  // ...Whatever else you have in your config...
};

@sonal-maniya
Copy link

Facing the same issue, the above solution is not working for me.
any update?

@stale
Copy link

stale bot commented Mar 14, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Mar 14, 2023
@stale stale bot closed this as completed Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants