3.13.2-ts.3 • Published 8 months ago
@rbxts-js/jest-snapshot v3.13.2-ts.3
jest-snapshot
Upstream: https://github.com/facebook/jest/tree/v27.4.7/packages/jest-snapshot
This package implements snapshot testing capabilities of Jest. You can find its documentation in the Jest documentation.
:pencil2: Notes
- To update snapshots, first set
--load.asRobloxScript --fs.readwrite="$(pwd)"to grantroblox-clielevated privileges to be able to write to filesystem and also grant it read/write access to the current directory and additionally set either:--lua.globals=UPDATESNAPSHOT="all"to create new snapshots and update all existing snapshots--lua.globals=UPDATESNAPSHOT="new"to only create new snapshots, existing failing snapshots will continue to fail
- We intentionally exclude inline snapshot testing as it introduces a lot of code complexity for not much added utility.
- Major deviations on snapshot read/write implementation details because we handle reading snapshots through various Roblox-specific mechanisms like
ModuleScript,FileSystemService, andCoreScriptSyncService. - New snapshot tests pass by default in Jest because snapshots get automatically created for tests that do not already have a snapshot written to file, Jest Roblox cannot do this because elevated permissions are required to write snapshots to file, therefore new snapshot tests fail by default (Jest Roblox tries to match against an empty snapshot) until a snapshot update is run.
- Detailed implementation notes and deviations on snapshot handling for future reference:
- Snapshots are written to file as a
.snap.luathat can then berequire'd as a validModuleScript. It exports a table with{ [key] = "snapshot" }. - The TestEZ runner writes and updates three pieces of information into a
JEST_TEST_CONTEXTglobal state for Jest snapshots to access:instance: theInstancereference to the.spec.luathat is currently running, this is needed to resolve the snapshot filesystem path.blocks: the test context (i.e. thedescribe/ithierarchy) of the currently running test, this is needed for the matchers to locate the correct snapshot to match against.snapshotState: theSnapshotStateobject, this is also needed for the matchers to locate the correct snapshot to match against.
- After a
.spec.luafile is run, the TestEZ runner attempts to save a snapshot if a snapshot is dirty and the condition matches the snapshot update strategy. toMatchSnapshotfirst initializes theSnapshotStatefor a.spec.luafile and stores it in theJEST_TEST_CONTEXTglobal state if it doesn't already exist.- If a
.snap.luafile for that.spec.luafile exists, it initializes aSnapshotStateof that snapshot. - If it doesn't exist, it initializes an empty
SnapshotStatewith its_snapshotPathset tonil(explained below).
- If a
- The
_snapshotPathproperty of aSnapshotStateis actually anInstancereferring to the snapshot file instead of a path on the filesystem, this is needed because we load snapshots byrequire'ing them as aModuleScriptand also because we load things within the Roblox DOM instead of from filesystem.- The exception is in
utils/saveSnapshotFile, which actually takes a filesystem path for it'ssnapshotPathargument. The filesystem path of the snapshot is resolved inSnapshotState:savebased off the path of the.spec.luafile that is currently running. - This means that for a snapshot that doesn't yet exist, the
_snapshotPathis set tonilsince there is noInstanceto refer to.
- The exception is in
- Because
FileSystemServiceandCoreScriptSyncServiceare heavily sandboxed and require elevated privileges, we opted to require absolutely no additional permissions to match against existing snapshots, but elevated permissions are needed to update snapshots.- New snapshot tests fail by default (mentioned above).
- Obsolete snapshots (snapshots without a corresponding test) are not pruned unless done in an update snapshot run.
- Obsolete snapshot files are not pruned. If a
.spec.luano longer contains any snapshot tests, the.snap.luawill not be removed even through an update snapshot run. This is because of separation between the TestEZ runner and Jest Roblox, resulting in Jest Roblox initializing theSnapshotStateand not the runner. This can be addressed when Jest Roblox gets its own runner.
- Obsolete snapshot files are not pruned. If a
- Snapshots are written to file as a
:x: Excluded
src/snapshot_resolver.ts:package: Dependencies
| Package | Version | Status | Notes |
|---|---|---|---|
@jest/types | 27.4.2 | :heavy_check_mark: Ported | |
@chalk | 4.0.0 | :heavy_check_mark: Ported | Imported from the chalk-lua library |
@expect | 27.4.6 | :heavy_check_mark: Ported | |
@graceful-fs | 4.2.4 | :x: Will not port | Don't need file system interaction since we use the Roblox datamodel for file access |
@jest-diff | 27.4.6 | :heavy_check_mark: Ported | |
@jest-get-type | 27.4.0 | :heavy_check_mark: Ported | |
@jest-matcher-utils | 27.4.6 | :heavy_check_mark: Ported | |
@pretty-format | 27.4.6 | :heavy_check_mark: Ported |