question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow primitives, objects, arrays and functions as arguments to `toMatchInlineSnapshot`

See original GitHub issue

🚀 Feature Proposal

Make toMatchInlineSnapshot able to handle primitive values (strings, booleans, null, undefined, numbers), objects (deep), arrays (deep), functions (check if it is a function).

Motivation

This is heavily influenced by @aaz’s comment in https://github.com/facebook/jest/pull/7792#issuecomment-460094000 and the motivation is to have more powerful inline snapshots by having the actual values in the code instead of strings representations of them.

Example

expect({
    a: 1,
    b: "2",
    c: true,
    d: null,
    e: undefined,
    f: [1, "2", [false]],
    g: () => {}
  }).toMatchInlineSnapshot({
    a: 1,
    b: "2",
    c: true,
    d: null,
    e: undefined,
    f: [1, "2", [false]],
    g: expect.any(Function)
  });

Pitch

The example is the pitch, toMatchInlineSnapshot is a very powerful tool, and it can do a lot better than just printing strings. Get the data you are trying to snapshot in front of you by just running the tests and change it accordingly writing JS instead of JS in a string.

`
    Object {
      "a": 1,
      "b": "2",
      "c": true,
      "d": null,
      "e": undefined,
      "f": Array [
        1,
        "2",
        Array [
          false,
        ],
      ],
      "g": [Function],
    }
  `

vs

{
    a: 1,
    b: "2",
    c: true,
    d: null,
    e: undefined,
    f: [1, "2", [false]],
    g: expect.any(Function)
  }

Implementation

Probably will have to be done on top of #7792 , it is good that babel is already being used in that PR as it is the most powerful AST editor out there.

I’d be glad to implement this myself and ideally have it ready for Jest 26.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jeysalcommented, Apr 26, 2020

If you want to help debug the OOM problem on #7792 so that we can land it feel free 😂 (half serious 😅 )

0reactions
github-actions[bot]commented, May 4, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expect - Jest
The expect function is used every time you want to test a value. ... so it's not possible to accept custom arguments in...
Read more >
How to pass primitive/object types through functions in ...
Both object and array fall under non-primitive data type but here we ... the two string values or parameters through the function print....
Read more >
Jest Expect - w3resource
toHaveBeenNthCalledWith to test what arguments it was nth called with. For instance, let us say you have a drinkEach(drink, Array<flavor>) ...
Read more >
API Reference | Vitest
It receives the test name, an async function with the tests to collect, ... properties with $ prefix, if you are using objects...
Read more >
Methods of primitives - The Modern JavaScript Tutorial
Let's look at the key distinctions between primitives and objects. ... There are other kinds of objects in JavaScript: functions, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found