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

Support toMatchInlineSnapshot a-la Jest #19

Open
canonic-epicure opened this issue Jan 17, 2022 · 0 comments
Open

Support toMatchInlineSnapshot a-la Jest #19

canonic-epicure opened this issue Jan 17, 2022 · 0 comments

Comments

@canonic-epicure
Copy link
Collaborator

canonic-epicure commented Jan 17, 2022

From discord: https://discord.com/channels/712696926406967308/931174951229206618/931525305896689694

This is what works with Jest today:

const someFunc = () => ({
    a: 5,
    b: {
        c: "str",
        d: new Date(0)
    }
})
it('test', () => {
    expect(someFunc()).toMatchInlineSnapshot()
})

After running jest becomes:

const someFunc = () => ({
    a: 5,
    b: {
        c: 'str',
        d: new Date(0),
    },
})
it('test', () => {
    expect(someFunc()).toMatchInlineSnapshot(`
Object {
  "a": 5,
  "b": Object {
    "c": "str",
    "d": 1970-01-01T00:00:00.000Z,
  },
}
`)
})

As you can see jest captured it as a string. Furthermore, as far I know Jest is only a testing framework that allows you to capture snapshots right in the code.
I really do love testing via inline snapshots. Most of the time I need to add tests for existing code (and speaking more generally I didn't get used to do TDD). Anyway, writing .toEqual by hand is not an option for me.
Jest GUIs (such as majestic) and its alternatives (such as Ava) support only snapshots as separate files (Jest's toMatchSnapshot). I don't like this as I need to jump around .snap files.
[3:29 PM]
Again, I love inline snapshots as I can see the result right in the code. In case if they are bloated I can just collapse all toMatchInlineSnapshot calls in IDE.

And finally what I want from testing library after running test command:

const someFunc = () => ({
    a: 5,
    b: {
        c: "str",
        d: new Date(0)
    }
})
it('test', () => {
    expect(someFunc()).toMatchInlineSnapshot({
        a: 5,
        b: {
            c: "str",
            d: new Date(0)
        }
    })
})

I understand that this might be tricky to implement, however I didn't see any focus on snapshot testing from anybody, which is extremely frustrating for me.

Here is an example where I use a lot of .toMatchInlineSnapshot: https://github.com/zardoy/vscode-framework/blob/next/packages/vscode-framework/test/cli/manifestGenerators.test.ts.

Hope I gave you enough context for the idea, feel free to ping me if something isn't clear.
[3:30 PM]
Now I got the idea for codemod to transform .toMatchInlineSnapshot to .toEqual but I would lose the ability to update failing snapshots from UI in this case.
January 17, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant