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

Realm.List inherited functions seem missing in jest test #1096

Closed
peteroid opened this issue Jun 26, 2017 · 8 comments
Closed

Realm.List inherited functions seem missing in jest test #1096

peteroid opened this issue Jun 26, 2017 · 8 comments

Comments

@peteroid
Copy link

peteroid commented Jun 26, 2017

Summary

When I was testing my model under Realm using Jest, it seems some functions were missing. For instance, map() and keys() would be missing and the test will throw the error saying TypeError: xxx.map is not a function. However, this error only happens in the jest but not in the normal execution.

After some trials, I found that replacing the map function from xxx.map(fn) to Realm.List.prototype.map.call(xxx, fn) could be working in both the jest test and normal execution.

Expected results

map function should be defined in the List object

Actual results

Jest threw errors like TypeError: xxx.map is not a function

Version

  • realm: 1.3.1
  • node: 7.10.0
  • jest: 20.0.4
@John251314
Copy link

I met the same problem. what is the solution, in addition to the above method. Thanks.

@benweidig
Copy link

benweidig commented Nov 14, 2017

We ran into this exact issue today, but we needed some(...) which isn't
callable like that.

@kneth
Copy link
Contributor

kneth commented Oct 16, 2019

@visemet
Copy link

visemet commented Oct 16, 2019

@kneth, I don't think this issue is specific to jest per-se though it is very much related to how jest uses vm.runInContext(). It appears that Realm.List instances do not have their prototype installed correctly when Realm is loaded from a separate context and are missing the properties from Realm.Collection.prototype.

const assert = require('assert');
const vm = require('vm');

function main() {
  const code = `
const Realm = require('realm');

const realm = new Realm({
  schema: [
    {
      name: 'PersonList',
      properties: {
        list: 'PersonObject[]',
      },
    },
    {
      name: 'PersonObject',
      properties: {
        name: 'string',
        age: 'double',
      },
    },
  ],
  deleteRealmIfMigrationNeeded: true,
});

realm.write(() => realm.deleteAll());

let object;
realm.write(() => {
  object = realm.create('PersonList', {list: [
    {name: 'Ari', age: 10},
    {name: 'Tim', age: 11},
    {name: 'Bjarne', age: 12},
  ]});
});

const methods = [];
let current = object.list;
do {
  methods.push(
    ...Object.getOwnPropertyNames(current).filter(prop => typeof object.list[prop] === 'function'));
} while ((current = Object.getPrototypeOf(current)));

realm.close();

new Set(methods);
`;

  const methodsInNewContext = vm.runInNewContext(code, {console, require});

  global.require = require;
  const methodsInThisContext = vm.runInThisContext(code);

  const difference = Array.from(
      new Set(
          [...methodsInThisContext].filter(x => !methodsInNewContext.has(x)))).sort();

  assert.deepStrictEqual(difference, []);
}

main();
AssertionError [ERR_ASSERTION]: Input A expected to strictly deep-equal input B:
+ expected - actual

- [
-   'concat',
-   'entries',
-   'every',
-   'filter',
-   'find',
-   'findIndex',
-   'forEach',
-   'join',
-   'keys',
-   'lastIndexOf',
-   'map',
-   'reduce',
-   'reduceRight',
-   'slice',
-   'some',
-   'values'
- ]
+ []

@kneth
Copy link
Contributor

kneth commented Oct 17, 2019

@kraenhansen You have a hypothesis why it isn't working, right?

@kraenhansen
Copy link
Member

@kraenhansen You have a hypothesis why it isn't working, right?

I believed we have a similar issue with for(let ... of ...) loop on React Native. Originating from the way Realm JS is looping over the methods to export here: https://github.com/realm/realm-js/blob/master/lib/collection-methods.js#L49. I have a suspicion that some environments call the callback of forEach asynchronously.

But ... reading over the Jest issues, that you @kneth linked to, this does indeed seem like a known Jest bug. Perhaps a workaround could be found, but it seems more valuable to get the root cause fixed.

@takameyer
Copy link
Contributor

@peteroid We took some time to try and reproduce this issue and failed to reproduce it. Can you try updating Jest and Realm, to see if the issue is resolved?

@sync-by-unito sync-by-unito bot closed this as completed Feb 16, 2022
@takameyer
Copy link
Contributor

The issues has been closed, as we are not able to reproduce it and no one in the community has reported that it is still an issue. If someone is seeing this issue, feel free to yell and we will reopen the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants