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

Testing store with jest snapshotting (mobx 5) #1613

Closed
PDarkTemplar opened this issue Jun 28, 2018 · 14 comments
Closed

Testing store with jest snapshotting (mobx 5) #1613

PDarkTemplar opened this issue Jun 28, 2018 · 14 comments

Comments

@PDarkTemplar
Copy link

PDarkTemplar commented Jun 28, 2018

Right now if i want to test my store with snapshot there is a lot of internal information like this:

...
    +     Symbol(mobx did run lazy initializers): true,
    +     Symbol(mobx administration): ObservableObjectAdministration$$1 {
    +       "defaultEnhancer": [Function],
    +       "keysAtom": Atom$$1 {
    +         "diffValue": 0,
    +         "isBeingObserved": false,
    +         "isPendingUnobservation": false,
    +         "lastAccessedBy": 0,
    +         "lowestObserverState": 2,
    +         "name": "[email protected]",
    +         "observers": Set {},
    +       },
    +       "name": "TreePage@149",
    +       "target": [Circular],
    +       "values": Map {
    +         "selectedFolder" => SelectedFolder {
    +           "id": 1,
    +           "name": "test1",
    +           "parentFolderId": undefined,
    +           "parentOrgUnitId": undefined,
    +           "type": undefined,
    +         },
    +         "breadCrumbs" => Array [],
    +         "items" => undefined,
    +         "loading" => true,
    +         "showSearchList" => false,
    +         "openFolderType" => "folder",
...

Which i don't need. So i created small transformation for jest which automaticly transform observables to JS. But then i lost all information about class names, and now they are all objects.

 "treePage": Object {
    "breadCrumbs": Array [],
    "canCreateFolder": false,
    "inputSearch": Object {
      "items": null,
      "loading": false,
      "requestsId": Array [],
      "showItems": null,
      "term": "",
    },

So my question is, is there is some tools to deal with this?
If someone interested in my transformation i could create repo for it.

@mweststrate
Copy link
Member

mweststrate commented Jun 28, 2018 via email

@PDarkTemplar
Copy link
Author

I update jest to 23.2 and it doesn't help, unfortinatly.

@PDarkTemplar
Copy link
Author

PDarkTemplar commented Jun 29, 2018

I created small repro, and find cause, if i create class throught constructor i would see all the internals of mobx. But if i create class in class field, it would be empty. I need middle result :) just plain value with className if it is class. Or it is jest bug, and i should go there?

@mweststrate
Copy link
Member

mweststrate commented Jun 29, 2018 via email

@PDarkTemplar
Copy link
Author

PDarkTemplar commented Jun 29, 2018

Ok, so if in first test i read value, i would get exactly the same result as on second test. And i want to cut out Symbol(mobx administration) and Symbol(mobx did run lazy initializers). And i think i could do this only with custom transformation.

@mweststrate
Copy link
Member

mweststrate commented Jun 29, 2018 via email

@manu-st
Copy link

manu-st commented Oct 1, 2018

Any update on this? Is there a workaround?

@thejuan
Copy link

thejuan commented Nov 20, 2018

You can add a custom snapshot serializer for observables, but you loose the object type (it will print Object { rather than MyType { )

import {isObservable, toJS} from "mobx";

expect.addSnapshotSerializer({
  test(value) {
    if (!value) {
      return false;
    }
    if (isObservable(value)) {
      return true;
    }

    return false;
  },
  print(value, serialize, indent) {
    return serialize(toJS(value));
  }
});

@thejuan
Copy link

thejuan commented Nov 20, 2018

A little better

expect.addSnapshotSerializer({
  test(value) {
    return isObservable(value);
  },
  serialize(value, config, indentation, depth, refs, printer) {
    if (isObservableArray(value)) {
      return printer(value.slice(), config, indentation, depth, refs);
    }
    const keys = Object.getOwnPropertyNames(value);
    keys.sort();
    const inner = keys
      .map(key => {
        return (
          indentation +
          config.indent +
          printer(key, config, indentation, depth, refs) +
          ": " +
          printer(value[key], config, indentation, depth, refs)
        );
      })
      .join(",\n");
    if (inner) {
      return value.constructor.name + " {\n" + inner + ",\n" + indentation + "}";
    }
    return value.constructor.name + " {}";
  }
});

@austinalameda
Copy link

@mweststrate any update on this? we are upgrading packages, jest 23.6.0, mobx 5.6.0, and our snapshots grow out of control having all this extra information

@mweststrate
Copy link
Member

Sorry, no, didn't get around opening an issue or PR on jest (the problem is that it should ignore non-enumerable (symbolic) members. It was fixed a while ago for toEqual, but not yet for snapshots. If anybody beats me to it that would be great ;)

@austinalameda
Copy link

if you have additional input: jestjs/jest#7443

@mweststrate
Copy link
Member

Fix has been merged: jestjs/jest#7448, will probably be released anywhere soon. Closing this one in the mean time.

@lock
Copy link

lock bot commented Jul 21, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.

@lock lock bot locked as resolved and limited conversation to collaborators Jul 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants