Repository: neherlab/covid19_scenarios
File: src/algorithms/utils/TimeSeries.test.ts (TypeScript)

Code before:
import { uniformDatesBetween, makeTimeSeries, updateTimeSeries } from './TimeSeries'
import { interpolateTimeSeries } from '../run'

import * as fs from 'fs'
const { utils } = require('jest-snapshot')

expect.extend({
  toBeCloseToArraySnapshot(received: number[]) {
    const { testPath, currentTestName, snapshotState } = this
    snapshotState._counters.set(currentTestName, (snapshotState._counters.get(currentTestName) || 0) + 1)
    const count = Number(snapshotState._counters.get(currentTestName))
    const key = utils.testNameToKey(currentTestName, count)
    const expected = snapshotState._snapshotData[key]

    /* If this isn't done, Jest reports the test as 'obsolete' and prompts for deletion. */
    snapshotState._uncheckedKeys.delete(key)

    /* If the snapshot isn't JSON, then return an empty array. Otherwise
     * the test will fails in an inscrutable way.
     */
    const tryParse = (str: string) => {
      try {
        return JSON.parse(str)
      } catch (e) {
        return []
      }
    }

    /* If the expected field is blank, then this is probably a new snapshot. */
    const expectedDeserialized = expected ? tryParse(expected) : []

    const pass =
      expectedDeserialized.length > 0 /* must check this because every() returns true if empty array */
        ? received.every((_, idx) => Math.abs(expectedDeserialized[idx] - received[idx]) < 10 ** -2 / 2)
        : false
    const hasSnapshot = expected !== undefined

Review comment (suggestion):
This code was lifted from Jest's `toMatchSnapshot()` implementation. Snapshots are labeled with a monotonic 'count' number, which is needed to find snapshot contents.

Code after:
import { uniformDatesBetween, makeTimeSeries, updateTimeSeries } from './TimeSeries'
import { interpolateTimeSeries } from '../run'

import * as fs from 'fs'
const { utils } = require('jest-snapshot')

expect.extend({
  toBeCloseToArraySnapshot(received: number[]) {
    const { currentTestName, snapshotState } = this
    snapshotState._counters.set(currentTestName, (snapshotState._counters.get(currentTestName) || 0) + 1)
    const count = Number(snapshotState._counters.get(currentTestName))
    const key = utils.testNameToKey(currentTestName, count)
    const expected = snapshotState._snapshotData[key]

    /* If this isn't done, Jest reports the test as 'obsolete' and prompts for deletion. */
    snapshotState._uncheckedKeys.delete(key)

    /* If the snapshot isn't JSON, then return an empty array. Otherwise
     * the test will fails in an inscrutable way.
     */
    const tryParse = (str: string) => {
      try {
        return JSON.parse(str)
      } catch (e) {
        return []
      }
    }

    /* If the expected field is blank, then this is probably a new snapshot. */
    const expectedDeserialized = expected ? tryParse(expected) : []

    const pass =
      expectedDeserialized.length > 0 /* must check this because every() returns true if empty array */
        ? received.every((_, idx) => Math.abs(expectedDeserialized[idx] - received[idx]) < 10 ** -2 / 2)
        : false
    const hasSnapshot = expected !== undefined