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

Feature request: allow hook for overriding serialize function in jest-snapshot #1741

Closed
anilanar opened this issue Sep 19, 2016 · 5 comments
Closed

Comments

@anilanar
Copy link
Contributor

I have a custom component implementation based on incremental-dom and I wish to do snapshot testing for it.

I've checked the source code of jest and from what I've figured, it is as easy as replacing serialize function in jest-snapshot/src/SnapshotFile.js, which converts a given data to a string against which comparison/matching is done. In React's case, serialize takes a jsonized react component and it returns it after piping it through pretty-format.

I will fork jest for now; but it would save me maintaining the fork if this feature was implemented.

@anilanar anilanar changed the title Feature request: allow a hook for overriding serialize function in jest-snapshot Feature request: allow hook for overriding serialize function in jest-snapshot Sep 19, 2016
@anilanar
Copy link
Contributor Author

Just tested serialize(data: any): string { return data; } for comparing plain strings just to see if it works. It works great.

@cpojer
Copy link
Member

cpojer commented Sep 20, 2016

You can overwrite toJSON on your top level object to provide a custom return value.

Try:

const object = …;
object.toJSON = () => 'my string';
expect(object).toMatchSnapshot();

@cpojer cpojer closed this as completed Sep 20, 2016
@anilanar
Copy link
Contributor Author

@cpojer Mind if I make a PR for specifying packages that would export objects that conform to pretty-format plugin interface?

// package.json
"jest": {
    // ReactElementPlugin, ReactTestComponentPlugin are natural members of this array
    "snapshot-serializers": ["my-serializer-package"]
}
// my-serializer-package
var MyCustomClass = require('my-package');
module.exports = {
    test: function(val) {
        return val instanceof MyCustomClass;
    }
    print: function(val) {
        // custom implementation
    }
};

Usage

test(() => {
    var foo = new MyCustomClass();
    expect(foo).toMatchSnapshot();
});

@cpojer
Copy link
Member

cpojer commented Oct 1, 2016

yeah I'd be happy to accept that!

anilanar added a commit to anilanar/jest that referenced this issue Oct 2, 2016
anilanar added a commit to anilanar/jest that referenced this issue Oct 2, 2016
anilanar added a commit to anilanar/jest that referenced this issue Oct 2, 2016
anilanar added a commit to anilanar/jest that referenced this issue Oct 2, 2016
anilanar added a commit to anilanar/jest that referenced this issue Oct 2, 2016
anilanar added a commit to anilanar/jest that referenced this issue Oct 2, 2016
okonet pushed a commit to okonet/jest that referenced this issue Nov 11, 2016
nickpresta pushed a commit to nickpresta/jest that referenced this issue Nov 15, 2016
tushardhole pushed a commit to tushardhole/jest that referenced this issue Aug 21, 2017
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
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

2 participants