Skip to content

Commit

Permalink
use replace and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tylersayshi committed Sep 19, 2024
1 parent 6932852 commit 2669fcb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
4 changes: 4 additions & 0 deletions ark/attest/__tests__/snapExpectedOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ attest(5, { cfg: { updateSnapshots: true } }).snap(5)

attest(5n).snap(5n)

attest(-5n).snap(-5n)

attest({ a: 4n }).snap({ a: 4n })

attest(undefined).snap(undefined)

attest("undefined").snap("undefined")
Expand Down
4 changes: 4 additions & 0 deletions ark/attest/__tests__/snapTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ attest(5, { cfg: { updateSnapshots: true } }).snap(6)

attest(5n).snap()

attest(-5n).snap()

attest({ a: 4n }).snap()

attest(undefined).snap()

attest("undefined").snap()
Expand Down
9 changes: 3 additions & 6 deletions ark/attest/cache/snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ const writeCachedInlineSnapshotUpdates = () => {
}
}

const BIG_INT_PREFIX = '"$ark.bigint-'

const snapshotArgsToQueuedUpdate = ({
position,
serializedValue,
Expand All @@ -161,10 +159,9 @@ const snapshotArgsToQueuedUpdate = ({
"`"
: JSON.stringify(serializedValue)

if (newArgText.startsWith(BIG_INT_PREFIX))
newArgText = newArgText.slice(BIG_INT_PREFIX.length, -1) + "n"
else if (newArgText.includes("$ark.undefined"))
newArgText = newArgText.replace('"$ark.undefined"', "undefined")
newArgText = newArgText
.replace(/"\$ark.bigint-(-?\d+)"/g, "$1n")
.replace(/"\$ark.undefined"/g, "undefined")

return {
position,
Expand Down
6 changes: 4 additions & 2 deletions ark/util/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ export type JsonPrimitive = string | boolean | number | null

export type JsonData = Json | JsonPrimitive

export const snapshot = <t>(data: t, opts: SerializationOptions): snapshot<t> =>
_serialize(data, opts, []) as never
export const snapshot = <t>(
data: t,
opts: SerializationOptions = {}
): snapshot<t> => _serialize(data, opts, []) as never

export type snapshot<t, depth extends 1[] = []> =
unknown extends t ? unknown
Expand Down

0 comments on commit 2669fcb

Please sign in to comment.