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

Refactor test with custom snapshot serializer #57

Merged
merged 5 commits into from Aug 19, 2022
Merged

Conversation

mizdra
Copy link
Owner

@mizdra mizdra commented Aug 19, 2022

Before

expect(result).toMatchInlineSnapshot(`
  Object {
    "dependencies": Array [],
    "tokens": Array [
      Object {
        "name": "a",
        "originalLocations": Array [
          Object {
            "end": Object {
              "column": 2,
              "line": 1,
            },
            "filePath": "/test/1.css",
            "start": Object {
              "column": 1,
              "line": 1,
            },
          },
        ],
      },
      Object {
        "name": "b",
        "originalLocations": Array [
          Object {
            "end": Object {
              "column": 2,
              "line": 2,
            },
            "filePath": "/test/1.css",
            "start": Object {
              "column": 1,
              "line": 2,
            },
          },
        ],
      },
    ],
  }
`);

After

expect(result).toMatchInlineSnapshot(`
  {
    "dependencies": [],
    "tokens": [
      {
        "name": "a",
        "originalLocations": [
          { "filePath": "/test/1.css", "start": { "line": 1, "column": 1 }, "end": { "line": 1, "column": 2 } }
        ]
      },
      {
        "name": "b",
        "originalLocations": [
          { "filePath": "/test/1.css", "start": { "line": 2, "column": 1 }, "end": { "line": 2, "column": 2 } }
        ]
      }
    ]
  }
`);

@mizdra mizdra added the Type: Refactoring A code change that neither fixes a bug nor adds a feature label Aug 19, 2022
},
};

expect.addSnapshotSerializer(jsonSerializer);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -27,3 +28,24 @@ expect.extend({
return mockfs.bypass(() => toThrowErrorMatchingSnapshot.call(this, ...args));
},
});

const jsonSerializer: jest.SnapshotSerializerPlugin = {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const jsonSerializer: jest.SnapshotSerializerPlugin = {
serialize(val) {
return format(JSON.stringify(val), { parser: 'json', printWidth: 120 }).trimEnd();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mizdra mizdra merged commit d559f9f into main Aug 19, 2022
@mizdra mizdra deleted the add-custom-serializer branch August 19, 2022 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Refactoring A code change that neither fixes a bug nor adds a feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant