question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
ajenkinscommented, Jan 31, 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...
};
1reaction
harshaliitrcommented, 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" ]

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property 'prefix' of undefined, test only · Issue ...
I am setting up jest in my project and get this error: TypeError: Cannot read property 'prefix' of undefined at Object.
Read more >
"Cannot read property 'prefix' of undefined" when making a ...
I've been working on it for ~ a week, when suddenly I get this error: 'Cannot read property 'prefix' of undefined'.
Read more >
cannot read properties of undefined (reading 'injectable')
This error occurs when you attempt to access a property on an object that is undefined. In this case, the object you are...
Read more >
ERROR in Cannot read property &#039;length&#38
Can you give more information about this: ERROR in Cannot read property 'length' of undefined ? Try to update "screenfull": "^3.3.0" to latest...
Read more >
Configuring Jest
Jest will run .mjs and .js files with nearest package.json 's type field set to module as ECMAScript Modules.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found