Skip to content

Commit c2e54b9

Browse files
committed
Rewrite find definitions tests
1 parent 705cd4b commit c2e54b9

File tree

11 files changed

+150
-359
lines changed

11 files changed

+150
-359
lines changed

packages/language-server/tests/__snapshots__/completions.spec.ts.snap

-124
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`Definitions > #2600 1`] = `"tsconfigProject/foo.vue"`;
4+
5+
exports[`Definitions > #2600 2`] = `
6+
{
7+
"end": {
8+
"character": 0,
9+
"line": 0,
10+
},
11+
"start": {
12+
"character": 0,
13+
"line": 0,
14+
},
15+
}
16+
`;
17+
18+
exports[`Definitions > Alias path 1`] = `"tsconfigProject/foo.ts"`;
19+
20+
exports[`Definitions > Alias path 2`] = `
21+
{
22+
"end": {
23+
"character": 25,
24+
"line": 0,
25+
},
26+
"start": {
27+
"character": 0,
28+
"line": 0,
29+
},
30+
}
31+
`;
32+
33+
exports[`Definitions > TS to vue 1`] = `"tsconfigProject/empty.vue"`;
34+
35+
exports[`Definitions > TS to vue 2`] = `
36+
{
37+
"end": {
38+
"character": 0,
39+
"line": 0,
40+
},
41+
"start": {
42+
"character": 0,
43+
"line": 0,
44+
},
45+
}
46+
`;
47+
48+
exports[`Definitions > TS to vue 3`] = `"tsconfigProject/empty.vue"`;
49+
50+
exports[`Definitions > TS to vue 4`] = `
51+
{
52+
"end": {
53+
"character": 0,
54+
"line": 0,
55+
},
56+
"start": {
57+
"character": 0,
58+
"line": 0,
59+
},
60+
}
61+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { Location, TextDocument } from '@volar/language-server';
2+
import * as path from 'path';
3+
import { afterEach, describe, expect, it } from 'vitest';
4+
import { URI } from 'vscode-uri';
5+
import { getLanguageServer, testWorkspacePath } from './server.js';
6+
7+
describe('Definitions', async () => {
8+
9+
it('TS to vue', async () => {
10+
await ensureGlobalTypesHolder('tsconfigProject');
11+
await assertDefinition('tsconfigProject/fixture1.ts', 'typescript', `import C|omponent from './empty.vue';`);
12+
await assertDefinition('tsconfigProject/fixture2.ts', 'typescript', `import Component from '|./empty.vue';`);
13+
});
14+
15+
it('Alias path', async () => {
16+
await ensureGlobalTypesHolder('tsconfigProject');
17+
await openDocument('tsconfigProject/foo.ts', 'typescript', `export const foo = 'foo';`);
18+
await assertDefinition('tsconfigProject/fixture.vue', 'vue', `
19+
<script setup lang="ts">
20+
import { foo| } from '@/foo';
21+
</script>
22+
`);
23+
});
24+
25+
it('#2600', async () => {
26+
await ensureGlobalTypesHolder('tsconfigProject');
27+
await openDocument('tsconfigProject/foo.vue', 'vue', `
28+
<template>
29+
<h1>{{ msg }}</h1>
30+
</template>
31+
32+
<script lang="ts">
33+
export default defineProps<{ msg: string }>()
34+
</script>
35+
`);
36+
await assertDefinition('tsconfigProject/fixture.vue', 'vue', `
37+
<script setup lang="ts">
38+
import Foo from '|@/foo.vue';
39+
</script>
40+
`);
41+
});
42+
43+
const openedDocuments: TextDocument[] = [];
44+
45+
afterEach(async () => {
46+
const server = await getLanguageServer();
47+
for (const document of openedDocuments) {
48+
await server.closeTextDocument(document.uri);
49+
}
50+
openedDocuments.length = 0;
51+
});
52+
53+
/**
54+
* @deprecated Remove this when #4717 fixed.
55+
*/
56+
async function ensureGlobalTypesHolder(folderName: string) {
57+
const document = await openDocument(`${folderName}/globalTypesHolder.vue`, 'vue', '');
58+
const server = await getLanguageServer();
59+
await server.sendDocumentDiagnosticRequest(document.uri);
60+
}
61+
62+
async function assertDefinition(fileName: string, languageId: string, content: string) {
63+
const offset = content.indexOf('|');
64+
content = content.slice(0, offset) + content.slice(offset + 1);
65+
66+
const server = await getLanguageServer();
67+
let document = await openDocument(fileName, languageId, content);
68+
69+
const position = document.positionAt(offset);
70+
const definition = await server.sendDefinitionRequest(document.uri, position) as Location[] | null;
71+
expect(definition).toBeDefined();
72+
expect(definition!.length).greaterThan(0);
73+
74+
for (const loc of definition!) {
75+
expect(path.relative(testWorkspacePath, URI.parse(loc.uri).fsPath)).toMatchSnapshot();
76+
expect(loc.range).toMatchSnapshot();
77+
}
78+
}
79+
80+
async function openDocument(fileName: string, languageId: string, content: string) {
81+
const server = await getLanguageServer();
82+
const uri = URI.file(`${testWorkspacePath}/${fileName}`);
83+
const document = await server.openInMemoryDocument(uri.toString(), languageId, content);
84+
if (openedDocuments.every(d => d.uri !== document.uri)) {
85+
openedDocuments.push(document);
86+
}
87+
return document;
88+
}
89+
});

packages/language-service/tests/complete.ts

-118
This file was deleted.

0 commit comments

Comments
 (0)