Skip to content

Commit

Permalink
Rewrite find definitions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Aug 24, 2024
1 parent 705cd4b commit c2e54b9
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 359 deletions.
124 changes: 0 additions & 124 deletions packages/language-server/tests/__snapshots__/completions.spec.ts.snap

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Definitions > #2600 1`] = `"tsconfigProject/foo.vue"`;

exports[`Definitions > #2600 2`] = `
{
"end": {
"character": 0,
"line": 0,
},
"start": {
"character": 0,
"line": 0,
},
}
`;

exports[`Definitions > Alias path 1`] = `"tsconfigProject/foo.ts"`;

exports[`Definitions > Alias path 2`] = `
{
"end": {
"character": 25,
"line": 0,
},
"start": {
"character": 0,
"line": 0,
},
}
`;

exports[`Definitions > TS to vue 1`] = `"tsconfigProject/empty.vue"`;

exports[`Definitions > TS to vue 2`] = `
{
"end": {
"character": 0,
"line": 0,
},
"start": {
"character": 0,
"line": 0,
},
}
`;

exports[`Definitions > TS to vue 3`] = `"tsconfigProject/empty.vue"`;

exports[`Definitions > TS to vue 4`] = `
{
"end": {
"character": 0,
"line": 0,
},
"start": {
"character": 0,
"line": 0,
},
}
`;
89 changes: 89 additions & 0 deletions packages/language-server/tests/definitions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Location, TextDocument } from '@volar/language-server';
import * as path from 'path';
import { afterEach, describe, expect, it } from 'vitest';
import { URI } from 'vscode-uri';
import { getLanguageServer, testWorkspacePath } from './server.js';

describe('Definitions', async () => {

it('TS to vue', async () => {
await ensureGlobalTypesHolder('tsconfigProject');
await assertDefinition('tsconfigProject/fixture1.ts', 'typescript', `import C|omponent from './empty.vue';`);
await assertDefinition('tsconfigProject/fixture2.ts', 'typescript', `import Component from '|./empty.vue';`);
});

it('Alias path', async () => {
await ensureGlobalTypesHolder('tsconfigProject');
await openDocument('tsconfigProject/foo.ts', 'typescript', `export const foo = 'foo';`);
await assertDefinition('tsconfigProject/fixture.vue', 'vue', `
<script setup lang="ts">
import { foo| } from '@/foo';
</script>
`);
});

it('#2600', async () => {
await ensureGlobalTypesHolder('tsconfigProject');
await openDocument('tsconfigProject/foo.vue', 'vue', `
<template>
<h1>{{ msg }}</h1>
</template>
<script lang="ts">
export default defineProps<{ msg: string }>()
</script>
`);
await assertDefinition('tsconfigProject/fixture.vue', 'vue', `
<script setup lang="ts">
import Foo from '|@/foo.vue';
</script>
`);
});

const openedDocuments: TextDocument[] = [];

afterEach(async () => {
const server = await getLanguageServer();
for (const document of openedDocuments) {
await server.closeTextDocument(document.uri);
}
openedDocuments.length = 0;
});

/**
* @deprecated Remove this when #4717 fixed.
*/
async function ensureGlobalTypesHolder(folderName: string) {
const document = await openDocument(`${folderName}/globalTypesHolder.vue`, 'vue', '');
const server = await getLanguageServer();
await server.sendDocumentDiagnosticRequest(document.uri);
}

async function assertDefinition(fileName: string, languageId: string, content: string) {
const offset = content.indexOf('|');
content = content.slice(0, offset) + content.slice(offset + 1);

const server = await getLanguageServer();
let document = await openDocument(fileName, languageId, content);

const position = document.positionAt(offset);
const definition = await server.sendDefinitionRequest(document.uri, position) as Location[] | null;
expect(definition).toBeDefined();
expect(definition!.length).greaterThan(0);

for (const loc of definition!) {
expect(path.relative(testWorkspacePath, URI.parse(loc.uri).fsPath)).toMatchSnapshot();
expect(loc.range).toMatchSnapshot();
}
}

async function openDocument(fileName: string, languageId: string, content: string) {
const server = await getLanguageServer();
const uri = URI.file(`${testWorkspacePath}/${fileName}`);
const document = await server.openInMemoryDocument(uri.toString(), languageId, content);
if (openedDocuments.every(d => d.uri !== document.uri)) {
openedDocuments.push(document);
}
return document;
}
});
118 changes: 0 additions & 118 deletions packages/language-service/tests/complete.ts

This file was deleted.

Loading

0 comments on commit c2e54b9

Please sign in to comment.