Skip to content

Commit

Permalink
Add full-loop test
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Dec 17, 2024
1 parent ad8c52c commit 4c9b79a
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 54 deletions.
48 changes: 48 additions & 0 deletions src/__snapshots__/index.test.ts.snap
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,
},
]
`;
70 changes: 16 additions & 54 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,10 @@ import { describe, expect, it } from "vitest";
import * as ts from "typescript";
import { getEnv } from "../tests/env";
import { setupTypeAcquisition } from ".";
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.
*/
async function fixture(path: string, mode: "read" | "write") {
return await (mode === "read" ? reader(path) : recorder(path));
}
import { fixture } from "../tests/fixture";

describe("ata", () => {
it("base", async () => {
it("non-prefixed lodash acquisition", async () => {
const env = getEnv();

let finish: (value?: unknown) => unknown;
Expand All @@ -73,7 +23,6 @@ describe("ata", () => {
delegate: {
receivedFile: (code: string, path: string) => {
env.createFile(path, code);
// Add code to your runtime at the path...
},
started: () => {
console.log("ATA start");
Expand All @@ -88,9 +37,22 @@ describe("ata", () => {
},
});

ata('import { x } from "lodash"');
const code = 'import { x } from "lodash"';
const path = "index.ts";
env.createFile(path, code);
ata(code);

await expect(finishedPromise).resolves.toBeFalsy();
await rec.save();

const sourcePos = code.indexOf("l") + 3;

expect(
env.languageService.getQuickInfoAtPosition(path, sourcePos),
).toMatchSnapshot();

expect(
env.languageService.getTypeDefinitionAtPosition(path, sourcePos),
).toMatchSnapshot();
});
});
51 changes: 51 additions & 0 deletions tests/fixture.ts
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));
}

0 comments on commit 4c9b79a

Please sign in to comment.