update to [email protected]
Successful test
got error from test with realm imported
TypeError: Cannot redefine property: Worker
at Function.defineProperty (native)
> 1 | import Realm from 'realm';
import Realm from 'realm';
describe("Testing realm", () => {
beforeEach(() => {
jest.resetModules();
});
test("realm", () => {
const realm2 = require('realm')
})
})
Adding configurable: true in realm/lib/index.js's Object.defineProperty(realmConstructor fixes the issue.
Not sure if this is a bug. Can you provide a workaround?
Update:
I noticed that this happens on tests that have mocks. Error occurs when the test has import, do a jest.resetModules() or jest.resetAllMocks() and then do a require('realm').
I can easily imagine that mocking can lead to troubles. A Realm object is not a plain JavaScript object as the object is managed by Realm and not the JS engine.
We have to investigate a bit further and I label it as a bug for now.
Thanks for the report and the suggested fix.
Edit: Same problem with Realm 2.2.1 and 2.2.6.
Edit: Deleting and re-cloning my project solved the problem
Edit: Deleting ".realm.lock", ".realm.management", ".realm.note" does not solve it.
Error occurs when the test has import, do a jest.resetModules() or jest.resetAllMocks() and then do a require('realm').
Sorry but I don't understand what the workaround is.
I import Realm in a service that I want to test, I cannot just require('realm') in each one of my tests.
I am not using Jest mocks, but I get the same error on all test suites involving Realm.
It happens randomly, too, while adding code and without changing any lib version. I reverted my work to a commit where the tests did pass, but now they do not anymore. Any cache to reset maybe (I tried jest --clearCache) ?
Still haven't found a workaround for this. I just stated how to replicate it in my last comment. Once i get the error, all of my test that has a module that imports realm also gets the error. I'm stuck using "realm": "~2.1.1".
Running into the same issue, some of my services import Realm directly so jest.resetModules() isn't fixing it 100% for me
I would like to know if anyone have tried to upgrade to 2.2.14?
@kneth
Still having the same error for test in description in 2.2.14.
``` FAIL __tests__TestRealm.js
● Testing realm › realm
TypeError: Cannot redefine property: Worker
at Function.defineProperty (native)
5 | });
6 | test("realm", () => {
> 7 | const realm2 = require('realm')
8 | })
9 | })
"dependencies": {
"react": "16.0.0",
"react-native": "0.51.0",
"realm": "^2.2.14"
},
```
I too get this Cannot redefine property: Worker error message. 😓
The tests run fine the first time, then when they're rerun (with --watch), I get that message. I've tried all the workarounds in this thread:
require('realm')Now I will downgrading to an older version of Realm to see if that works
Here is a quick gist I made that reproduces the problem. Tests pass the first time, but second time round:

I searched for Worker in the realm module, and changing this line in node_modules/realm/lib/index.js:125 fixes my problem 🎉
Object.defineProperty(realmConstructor, 'Worker', {value: nodeRequire('./worker')});
// to
if (!realmConstructor.Worker) {
Object.defineProperty(realmConstructor, 'Worker', {value: nodeRequire('./worker')});
}
I'm new to Realm and I don't use the "Sync" option at all, so I'm not sure if this causes a problem or not. This seems more like a symptom of a problem than the cause, so I'll hold off creating a PR 😛
I close the issue as #1695 has been merged and released as part of 2.2.15.
hi may i ask if any workaround for the above problem cause i keep on have
Test suite failed to run
TypeError: Cannot redefine property: Worker
at Function.defineProperty (<anonymous>)
1 | 'use strict'
2 |
> 3 | import Realm from 'realm';
4 | import { getModelList } from './DatabaseModel';
5 | import {log} from '../../Log'
6 | export const File = 'DatabaseService'
at Object.<anonymous> (node_modules/realm/lib/index.js:125:14)
at Object.<anonymous> (src/lib/managers/mgrDatabase/DatabaseService.js:3:12)
at Object.<anonymous> (src/lib/managers/mgrResource.js:135:71)
Hi. After update to 2.2.15:
TypeError: Cannot redefine property: Permissions
at Function.defineProperty (<anonymous>)
I have merged #1723 but we need to backport it to the 2.2.x series (branch 2.2.x).
Most helpful comment
I close the issue as #1695 has been merged and released as part of 2.2.15.