Skip to content

Commit

Permalink
test(typescript): Add tests for Volar caching (withastro#836)
Browse files Browse the repository at this point in the history
* test(typescript): Add tests for Volar caching

* test: add more tests and update Volar
  • Loading branch information
Princesseuh authored Mar 21, 2024
1 parent d57daad commit ad3c572
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 84 deletions.
12 changes: 6 additions & 6 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
"dependencies": {
"@astrojs/compiler": "^2.7.0",
"@jridgewell/sourcemap-codec": "^1.4.15",
"@volar/kit": "~2.1.2",
"@volar/language-core": "~2.1.2",
"@volar/language-server": "~2.1.2",
"@volar/language-service": "~2.1.2",
"@volar/typescript": "~2.1.2",
"@volar/kit": "~2.1.3",
"@volar/language-core": "~2.1.3",
"@volar/language-server": "~2.1.3",
"@volar/language-service": "~2.1.3",
"@volar/typescript": "~2.1.3",
"fast-glob": "^3.2.12",
"volar-service-css": "0.0.34",
"volar-service-emmet": "0.0.34",
Expand All @@ -45,7 +45,7 @@
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.1",
"@types/node": "^18.17.8",
"@volar/test-utils": "~2.1.2",
"@volar/test-utils": "~2.1.3",
"astro": "^4.3.5",
"chai": "^4.3.7",
"mocha": "^10.2.0",
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions packages/language-server/test/fixture/cachingTest.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
import Component from './caching/';
---

<AutoImpo
3 changes: 3 additions & 0 deletions packages/language-server/test/fixture/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'im-a-super-module' {
export const hello: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
import { hello } from 'im-a-super-module';
hello;
---
3 changes: 3 additions & 0 deletions packages/language-server/test/fixture/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "astro/tsconfigs/base"
}
140 changes: 140 additions & 0 deletions packages/language-server/test/typescript/caching.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { readdir } from 'node:fs/promises';
import path from 'node:path';
import type { FullDocumentDiagnosticReport } from '@volar/language-server';
import { expect } from 'chai';
import { mkdir, rm, writeFile } from 'fs/promises';
import { type LanguageServer, getLanguageServer } from '../server.js';

const fixtureDir = path.join(__dirname, '../fixture');

const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));

describe('TypeScript - Cache invalidation', async () => {
let languageServer: LanguageServer;

async function createFile(name: string, contents: string) {
await writeFile(path.join(fixtureDir, 'caching', name), contents);
}

async function removeFile(name: string) {
await rm(path.join(fixtureDir, 'caching', name));
}

before(async () => {
languageServer = await getLanguageServer();

try {
await mkdir(path.join(fixtureDir, 'caching'));
} catch (e) {}

await createFile('toBeDeleted.astro', '');
});

it('Can get paths completions for new files', async () => {
const fileNames = ['PathCompletion.astro', 'PathCompletion2.astro'];

const document = await languageServer.handle.openTextDocument(
path.join(fixtureDir, 'cachingTest.astro'),
'astro'
);

// Try two different files, to make sure the cache capture everything
for (const fileName of fileNames) {
await createFile(fileName, '');

const completions = await languageServer.handle.sendCompletionRequest(document.uri, {
line: 1,
character: 33,
});

const labels = completions?.items.map((i) => i.label);
expect(labels).to.include(fileName, `Expected ${fileName} to be in the completions`);
}
});

it('Does not get path completions for removed files', async () => {
const document = await languageServer.handle.openTextDocument(
path.join(fixtureDir, 'cachingTest.astro'),
'astro'
);

await removeFile('toBeDeleted.astro');

const directoryContent = await readdir(path.join(fixtureDir, '/caching'));
expect(directoryContent).to.not.include('toBeDeleted.astro');

const completions = await languageServer.handle.sendCompletionRequest(document.uri, {
line: 1,
character: 33,
});

const labels = completions?.items.map((i) => i.label);
expect(labels).to.not.include(
'toBeDeleted.astro',
`Expected toBeDeleted.astro to not be in the completions, since the file was deleted`
);
});

it('Can get auto-imports for new files', async () => {
const fileNames = ['AutoImport.astro', 'AutoImport2.astro'];

const document = await languageServer.handle.openTextDocument(
path.join(fixtureDir, 'cachingTest.astro'),
'astro'
);

// Try two different files, to make sure the cache capture everything
for (const fileName of fileNames) {
await createFile(fileName, '');

const imports = await languageServer.handle.sendCompletionRequest(document.uri, {
line: 4,
character: 9,
});

const labels = imports?.items.map((i) => i.label);
const className = fileName.slice(0, -'.astro'.length);
expect(labels).to.include(className, `Expected ${className} to be in the auto-imports`);
}
});

it('New files have access to context of the project', async () => {
const existingDocument = await languageServer.handle.openTextDocument(
path.join(fixtureDir, 'importFromSuperModule.astro'),
'astro'
);

const existingDiagnostics = (await languageServer.handle.sendDocumentDiagnosticRequest(
existingDocument.uri
)) as FullDocumentDiagnosticReport;

expect(existingDiagnostics.items).to.have.length(
0,
'Expected no diagnostics, as the file is part of the project'
);

await createFile(
'WillImportFromSuperModule.astro',
'---\n\nimport { hello } from "im-a-super-module";\n\nhello;\n\n---\n'
);

const document = await languageServer.handle.openTextDocument(
path.join(fixtureDir, 'caching', 'WillImportFromSuperModule.astro'),
'astro'
);

const diagnostics = (await languageServer.handle.sendDocumentDiagnosticRequest(
document.uri
)) as FullDocumentDiagnosticReport;

expect(diagnostics.items).to.have.length(
0,
'Expected no diagnostics, as new files should have access to the module declaration in the project like already existing files.'
);
});

after(async () => {
// Delete all the temp files
await rm(path.join(fixtureDir, 'caching'), { recursive: true });
});
});
11 changes: 10 additions & 1 deletion packages/language-server/test/typescript/diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('TypeScript - Diagnostics', async () => {
const diagnostics = (await languageServer.handle.sendDocumentDiagnosticRequest(
document.uri
)) as FullDocumentDiagnosticReport;
expect(diagnostics.items).length(1);
expect(diagnostics.items).length(2);

diagnostics.items = diagnostics.items.map((diag) => ({ ...diag, data: {} }));
expect(diagnostics.items).to.deep.equal([
Expand All @@ -75,6 +75,15 @@ describe('TypeScript - Diagnostics', async () => {
severity: DiagnosticSeverity.Error,
source: 'ts',
},
{
code: 2307,
data: {},
message:
"Cannot find module 'astro:content' or its corresponding type declarations.\n\nIf you're using content collections, make sure to run `astro dev`, `astro build` or `astro sync` to first generate the types so you can import from them. If you already ran one of those commands, restarting the language server might be necessary in order for the change to take effect.",
range: Range.create(1, 31, 1, 46),
severity: 1,
source: 'ts',
},
]);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/ts-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"author": "withastro",
"license": "MIT",
"dependencies": {
"@volar/language-core": "~2.1.2",
"@volar/typescript": "~2.1.2",
"@volar/language-core": "~2.1.3",
"@volar/typescript": "~2.1.3",
"@astrojs/compiler": "^2.7.0",
"@jridgewell/sourcemap-codec": "^1.4.15",
"semver": "^7.3.8",
Expand Down
6 changes: 3 additions & 3 deletions packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
},
"scripts": {
"prebuild": "cd ../ts-plugin && pnpm build",
"build": "pnpm prebuild && node scripts/build.mjs -- --minify",
"build": "tsc && pnpm prebuild && node scripts/build.mjs -- --minify",
"dev": "node scripts/build.mjs -- --watch",
"build:grammar": "npx js-yaml syntaxes/astro.tmLanguage.src.yaml > syntaxes/astro.tmLanguage.json",
"test": "pnpm test:vscode && pnpm test:grammar",
Expand All @@ -201,8 +201,8 @@
"@types/mocha": "^10.0.1",
"@types/node": "^18.17.8",
"@types/vscode": "^1.82.0",
"@volar/language-server": "~2.1.2",
"@volar/vscode": "~2.1.2",
"@volar/language-server": "~2.1.3",
"@volar/vscode": "~2.1.3",
"@vscode/test-electron": "^2.3.2",
"@vscode/vsce": "latest",
"esbuild": "^0.17.19",
Expand Down
3 changes: 2 additions & 1 deletion packages/vscode/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
"rootDir": "src",
"emitDeclarationOnly": true
},
"include": ["src"]
}
Loading

0 comments on commit ad3c572

Please sign in to comment.