jestjsvitest

Combine toMatchSnapshot and toBeCloseTo behavior?


In my snapshot, I have objects of the following format:

{
  "meanElements": {
    "Om": 2.2835713400168607,
    "am": 1.3043985097733939,
    "em": 0.166565,
    "im": 0.5743651675510579,
    "mm": -4.361270622785478,
    "nm": 0.049918822378349076,
    "om": 4.074394344293675,
  },
  "position": {
    "x": -5869.9169277202145,
    "y": 6791.004588475254,
    "z": 0.0037576740924038414,
  },
  "velocity": {
    "x": -4.630159774803886,
    "y": -2.7654049420795106,
    "z": 3.4389423149777203,
  },
}

I want to match object structure and numbers in it, but up to N digits after coma instead of precisely. How do I accomplish that with Vitest? I have looked into propertyMatchers but I don't think I can use toBeCloseTo there.


Solution

  • This does not combine toMatchSnapshot and toBeCloseTo but solves the underlying problem: loosely compare to a bunch of known results.

    Instead of using snapshots at all, I have made a JSON file with the expected results. I import it in the test file and compare with it using toBeCloseTo .