Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Acceptance tests for ts plugin #789

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "template-imports-app-ts-plugin",
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Component from '@glimmer/component';

export interface GreetingSignature {
Args: { target: string };
}

export default class Greeting extends Component<GreetingSignature> {
private message = 'Hello';

<template>
{{this.message}}, {{@target}}!
</template>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import '@glint/environment-ember-loose';
import '@glint/environment-ember-template-imports';

import Greeting from './Greeting';

<template>
<Greeting @target="World" />
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../../../tsconfig.compileroptions.json",
"glint": {
"environment": ["ember-loose", "ember-template-imports"],
"enableTsPlugin": true
},
"compilerOptions": {
"baseUrl": ".",
"plugins": [{ "name": "@glint/typescript-plugin" }]
},
}
135 changes: 135 additions & 0 deletions packages/vscode/__tests__/smoketest-template-imports-ts-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import {
commands,
languages,
ViewColumn,
window,
Uri,
Range,
Position,
CodeAction,
workspace,
} from 'vscode';
import * as path from 'path';
import { describe, afterEach, test } from 'mocha';
import { expect } from 'expect';
import { waitUntil } from './helpers/async';

describe.only('Smoke test: ETI Environment (TS Plugin Mode)', () => {
const rootDir = path.resolve(__dirname, '../../__fixtures__/template-imports-app-ts-plugin');

afterEach(async () => {
while (window.activeTextEditor) {
await commands.executeCommand('workbench.action.files.revert');
await commands.executeCommand('workbench.action.closeActiveEditor');
}
});

describe('diagnostics for errors', () => {
test('with a custom extension', async () => {
let scriptURI = Uri.file(`${rootDir}/src/index.gts`);
let scriptEditor = await window.showTextDocument(scriptURI, { viewColumn: ViewColumn.One });

// Ensure we have a clean bill of health
expect(languages.getDiagnostics(scriptURI)).toEqual([]);

// Replace a string with a number
await scriptEditor.edit((edit) => {
edit.replace(new Range(6, 20, 6, 27), '{{123}}');
});

// Wait for the diagnostic to show up
await waitUntil(() => languages.getDiagnostics(scriptURI).length);

// Verify it's what we expect
expect(languages.getDiagnostics(scriptURI)).toMatchObject([
{
message: "Type 'number' is not assignable to type 'string'.",
source: 'glint',
code: 2322,
range: new Range(6, 13, 6, 19),
},
]);
});

describe('codeactions args', () => {
test('adds missing args from template into Args type', async () => {
let scriptURI = Uri.file(`${rootDir}/src/Greeting.gts`);

// Open the script and the template
let scriptEditor = await window.showTextDocument(scriptURI, { viewColumn: ViewColumn.One });

// Ensure neither has any diagnostic messages
expect(languages.getDiagnostics(scriptURI)).toEqual([]);

// Comment out a property in the script that's referenced in the template
await scriptEditor.edit((edit) => {
edit.insert(new Position(10, 4), '{{@undocumentedProperty}} ');
});

// Wait for a diagnostic to appear in the template
await waitUntil(() => languages.getDiagnostics(scriptURI).length);

const fixes = await commands.executeCommand<CodeAction[]>(
'vscode.executeCodeActionProvider',
scriptURI,
new Range(new Position(10, 9), new Position(10, 9)),
);

expect(fixes.length).toBe(4);

const fix = fixes.find((fix) => fix.title === "Declare property 'undocumentedProperty'");

expect(fix).toBeDefined();

// apply the missing arg fix
await workspace.applyEdit(fix!.edit!);

await waitUntil(
() =>
scriptEditor.document.getText().includes('undocumentedProperty: any') &&
languages.getDiagnostics(scriptURI).length === 0,
);
});
});

describe('codeactions locals', () => {
test('add local props to a class', async () => {
let scriptURI = Uri.file(`${rootDir}/src/Greeting.gts`);

// Open the script and the template
let scriptEditor = await window.showTextDocument(scriptURI, { viewColumn: ViewColumn.One });

// Ensure neither has any diagnostic messages
expect(languages.getDiagnostics(scriptURI)).toEqual([]);

await scriptEditor.edit((edit) => {
edit.insert(new Position(10, 4), '{{this.localProp}} ');
});

// Wait for a diagnostic to appear in the template
await waitUntil(() => languages.getDiagnostics(scriptURI).length);

const fixes = await commands.executeCommand<CodeAction[]>(
'vscode.executeCodeActionProvider',
scriptURI,
new Range(new Position(10, 12), new Position(10, 12)),
);

expect(fixes.length).toBe(4);

const fix = fixes.find((fix) => fix.title === "Declare property 'localProp'");

expect(fix).toBeDefined();

// select ignore
await workspace.applyEdit(fix!.edit!);

await waitUntil(
() =>
scriptEditor.document.getText().includes('localProp: any') &&
languages.getDiagnostics(scriptURI).length,
);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { launchTests } from './launch-from-cli-shared.mjs';

await launchTests(false);
58 changes: 58 additions & 0 deletions packages/vscode/__tests__/support/launch-from-cli-shared.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as path from 'node:path';
import * as os from 'node:os';
import { fileURLToPath } from 'node:url';
import { runTests } from '@vscode/test-electron';
import * as fs from 'node:fs';

export async function launchTests(tsPlugin: boolean): Promise<void> {
const dirname = path.dirname(fileURLToPath(import.meta.url));
const packageRoot = path.resolve(dirname, '../../..');
const emptyExtensionsDir = path.join(os.tmpdir(), `extensions-${Math.random()}`);
const emptyUserDataDir = path.join(os.tmpdir(), `user-data-${Math.random()}`);

const settingsDir = path.join(emptyUserDataDir, 'User');
fs.mkdirSync(settingsDir, { recursive: true });

const userPreferences = {
// "typescript.tsserver.log": "verbose",
};

fs.writeFileSync(
path.join(settingsDir, 'settings.json'),
JSON.stringify(userPreferences, null, 2),
);

const folders = tsPlugin
? [`${packageRoot}/__fixtures__/template-imports-app-ts-plugin`]
: [`${packageRoot}/__fixtures__/ember-app`, `${packageRoot}/__fixtures__/template-imports-app`];

// We want default TS enabled when testing TS Plugin, otherwise when testing language server
// we want it disabled for full "takeover" mode.
const disableExtensionArgs = tsPlugin
? []
: ['--disable-extension', 'vscode.typescript-language-features'];

try {
await runTests({
extensionDevelopmentPath: packageRoot,
extensionTestsPath: path.resolve(dirname, 'vscode-runner.js'),
launchArgs: [
// Don't show the "hey do you trust this folder?" prompt
'--disable-workspace-trust',
...disableExtensionArgs,
// Point at an empty directory so no third-party extensions load
'--extensions-dir',
emptyExtensionsDir,
// Point at an empty directory so we don't have to contend with any local user preferences
// (other than the ones explicitly set above in `userPreferences`)
'--user-data-dir',
emptyUserDataDir,
// Load the app fixtures
...folders,
],
});
} catch (error) {
console.error('Failed to launch tests:', error);
process.exit(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { launchTests } from './launch-from-cli-shared.mjs';

await launchTests(true);
34 changes: 0 additions & 34 deletions packages/vscode/__tests__/support/launch-from-cli.mts

This file was deleted.

4 changes: 3 additions & 1 deletion packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
],
"scripts": {
"pretest": "yarn build",
"test": "node lib/__tests__/support/launch-from-cli.mjs",
"test": "yarn test-language-server && yarn test-ts-plugin",
"test-language-server": "node lib/__tests__/support/launch-from-cli-language-server.mjs",
"test-ts-plugin": "node lib/__tests__/support/launch-from-cli-ts-plugin.mjs",
"test:typecheck": "echo 'no standalone typecheck within this project'",
"test:tsc": "echo 'no standalone tsc within this project'",
"build": "yarn compile && yarn bundle",
Expand Down
Loading