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

Cannot use both mocked realm and actual realm in a test #6122

Open
Yupeng-li opened this issue Sep 8, 2023 · 5 comments
Open

Cannot use both mocked realm and actual realm in a test #6122

Yupeng-li opened this issue Sep 8, 2023 · 5 comments

Comments

@Yupeng-li
Copy link

Yupeng-li commented Sep 8, 2023

How frequently does the bug occur?

Always

Description

If I mock realm in a test file, and then import the real realm in a particular test, it will give this error:

{{ TypeError: Cannot redefine property: Realm}}

The error is from this line. Object.defineProperty was called twice.

To reproduce the issue, see example below.

jest.mock('realm')

it('a test', () => {
    const Realm = jest.requireActual('realm')
    // ...
})

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

12.1.0

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Mac OS 13.5.1 (22G90)

Build environment

Which debugger for React Native: ..

Cocoapods version

1.12.1

@kneth
Copy link
Contributor

kneth commented Sep 8, 2023

@Yupeng-li Mocking Realm in Jest isn't trivial. You might want to seek inspiration in #370 (comment)

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Sep 8, 2023
@Yupeng-li
Copy link
Author

Hi @kneth , thank you! My issue isn't about how to mock realm, but realm module throws when it's imported multiple times. The sample code above shows the scenario when it could be imported twice. Our use case is slightly different. We use a jest custom matcher which imports Realm. Our tests also import realm, so the module is imported twice.

I temporarily fixed the issue I reported but then I got errors like:
Error: Expected value to be an instance of List, got an instance of List
Error: Illegal constructor: Results objects are read from managed objects only.

It seems that assertion is checking instance type against two copies of the module. We didn't have the issue with Realm v11, so I am wondering if this is something you could fix?

Best regards

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Sep 8, 2023
@Yupeng-li
Copy link
Author

There is a more common scenario. If I mock a file that imports Realm, the TypeError will be thrown. Please let me know if you need a min repo.

// functionB.ts

import Realm from 'realm' 

export const functionB = () => {
    console.log('function B', typeof Realm)
}
// some.test.ts
import Realm from 'realm' // this is required to reproduce the issue 
import { functionB } from './functionB'

jest.mock('./functionB') // This crashes the test because of the error: TypeError: Cannot redefine property: Realm

it('a test', () => {
    console.log('ss', typeof Realm) // This could be the code pre-populating data to realm, e.g. realm.create(...)
    functionB()
})

@a396901990
Copy link

I have the same issue, the test stopped working since upgrading to realm v12.3.0

@kraenhansen kraenhansen self-assigned this Feb 12, 2024
@sync-by-unito sync-by-unito bot removed the Needs-Attention Reporter has responded. Review comment. label Feb 12, 2024
@liamjones
Copy link

Hi @kraenhansen I'm going through a bunch of upgrades on our apps atm (RN, 3rd-party deps, etc) and I was just wondering if there was any news on this issue yet? It's our remaining blocker to upgrade from Realm 11 to 12.

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

5 participants