-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
115 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`ata > non-prefixed lodash acquisition 1`] = ` | ||
{ | ||
"displayParts": [ | ||
{ | ||
"kind": "keyword", | ||
"text": "module", | ||
}, | ||
{ | ||
"kind": "space", | ||
"text": " ", | ||
}, | ||
{ | ||
"kind": "stringLiteral", | ||
"text": ""/node_modules/@types/lodash/index"", | ||
}, | ||
], | ||
"documentation": [], | ||
"kind": "module", | ||
"kindModifiers": "declare", | ||
"tags": undefined, | ||
"textSpan": { | ||
"length": 8, | ||
"start": 18, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`ata > non-prefixed lodash acquisition 2`] = ` | ||
[ | ||
{ | ||
"containerKind": undefined, | ||
"containerName": "", | ||
"failedAliasResolution": false, | ||
"fileName": "/node_modules/@types/lodash/index.d.ts", | ||
"isAmbient": true, | ||
"isLocal": false, | ||
"kind": "module", | ||
"name": ""/node_modules/@types/lodash/index"", | ||
"textSpan": { | ||
"length": 221, | ||
"start": 547, | ||
}, | ||
"unverified": false, | ||
}, | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as Fs from "node:fs/promises"; | ||
|
||
async function reader(path: string) { | ||
const m = new Map<string, string>( | ||
JSON.parse(await Fs.readFile(path, "utf8")), | ||
); | ||
return { | ||
fetch: (...args: any[]) => { | ||
return Promise.resolve(new Response(new Blob([m.get(args[0])!]))); | ||
}, | ||
async save() {}, | ||
}; | ||
} | ||
|
||
function recorder(path: string) { | ||
const recordings: Promise<[string, string]>[] = []; | ||
return { | ||
fetch: (...args: Parameters<typeof fetch>) => { | ||
const url = args[0]; | ||
return fetch(...args).then((r) => { | ||
if (typeof url === "string") { | ||
recordings.push( | ||
r | ||
.clone() | ||
.text() | ||
.then((text) => { | ||
return [url, text]; | ||
}), | ||
); | ||
} else { | ||
console.error("Non-string URL provided unexpectedly"); | ||
} | ||
return r; | ||
}); | ||
}, | ||
recordings, | ||
async save() { | ||
await Fs.writeFile(path, JSON.stringify(await Promise.all(recordings))); | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Using msw is probably the right thing to do, but there isn't an easy | ||
* mode for recording & replaying Node-native fetch calls. This is that. | ||
* It will probably need to be extended with the ability to communicate | ||
* headers and such in the future. | ||
*/ | ||
export async function fixture(path: string, mode: "read" | "write") { | ||
return await (mode === "read" ? reader(path) : recorder(path)); | ||
} |