Browse Source

Add smoketest and run it in CI

Alex Dima 3 years ago
parent
commit
78b2edf1e3
7 changed files with 1015 additions and 0 deletions
  1. 3 0
      .github/workflows/ci.yml
  2. 744 0
      package-lock.json
  3. 5 0
      package.json
  4. 18 0
      test/smoke/amd.html
  5. 167 0
      test/smoke/amd.test.js
  6. 6 0
      test/smoke/common.js
  7. 72 0
      test/smoke/runner.js

+ 3 - 0
.github/workflows/ci.yml

@@ -37,3 +37,6 @@ jobs:
 
       - name: Run unit tests
         run: npm test
+
+      - name: Run smoke test
+        run: npm run smoketest

File diff suppressed because it is too large
+ 744 - 0
package-lock.json


+ 5 - 0
package.json

@@ -10,6 +10,8 @@
 		"import-typescript": "node ./build/importTypescript",
 		"watch": "tsc -w -p ./src",
 		"test": "node ./test/unit/all.js",
+		"smoketest": "node ./test/smoke/runner.js",
+		"smoketest-debug": "node ./test/smoke/runner.js --debug-tests",
 		"release": "node ./build/build && gulp release",
 		"website": "gulp build-website && npm run typedoc && gulp prepare-website-branch",
 		"build-website": "gulp build-website && npm run typedoc",
@@ -30,6 +32,7 @@
 		"@types/event-stream": "^3.3.34",
 		"@types/tape": "^4.13.2",
 		"@typescript/vfs": "^1.3.5",
+		"chai": "^4.3.4",
 		"clean-css": "^5.1.1",
 		"esbuild": "^0.13.13",
 		"esbuild-plugin-alias": "^0.2.0",
@@ -39,7 +42,9 @@
 		"husky": "^7.0.4",
 		"jsdom": "^17.0.0",
 		"jsonc-parser": "^3.0.0",
+		"mocha": "^9.1.3",
 		"monaco-editor-core": "0.30.1",
+		"playwright": "^1.16.3",
 		"prettier": "^2.4.1",
 		"pretty-quick": "^3.1.1",
 		"requirejs": "^2.3.6",

+ 18 - 0
test/smoke/amd.html

@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html>
+	<head>
+		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
+	</head>
+	<body>
+		<div id="editor-container" style="position: absolute; width: 500px; height: 400px"></div>
+		<script src="../../release/dev/vs/loader.js"></script>
+		<script>
+			require.config({
+				paths: {
+					vs: '../../release/dev/vs'
+				}
+			});
+			require(['vs/editor/editor.main'], () => {});
+		</script>
+	</body>
+</html>

+ 167 - 0
test/smoke/amd.test.js

@@ -0,0 +1,167 @@
+/*---------------------------------------------------------------------------------------------
+ *  Copyright (c) Microsoft Corporation. All rights reserved.
+ *  Licensed under the MIT License. See License.txt in the project root for license information.
+ *--------------------------------------------------------------------------------------------*/
+
+//@ts-check
+
+const playwright = require('playwright');
+const { assert } = require('chai');
+const { PORT } = require('./common');
+
+const browserType = process.env.BROWSER || 'chromium';
+const DEBUG_TESTS = Boolean(process.env.DEBUG_TESTS || false);
+const URL = `http://127.0.0.1:${PORT}/test/smoke/amd.html`;
+
+/** @type {playwright.Browser} */
+let browser;
+
+/** @type {playwright.Page} */
+let page;
+
+before(async () => {
+	console.log(`Starting browser: ${browserType}`);
+	browser = await playwright[browserType].launch({
+		headless: !DEBUG_TESTS,
+		devtools: DEBUG_TESTS
+		// slowMo: DEBUG_TESTS ? 2000 : 0
+	});
+});
+
+after(async () => {
+	await browser.close();
+});
+
+let pageErrors = [];
+
+beforeEach(async () => {
+	pageErrors = [];
+	page = await browser.newPage({
+		viewport: {
+			width: 800,
+			height: 600
+		}
+	});
+	page.on('pageerror', (e) => {
+		console.log(e);
+		pageErrors.push(e);
+	});
+	const response = await page.goto(URL);
+	assert.ok(!!response);
+	assert.strictEqual(response.status(), 200);
+});
+
+afterEach(async () => {
+	for (const e of pageErrors) {
+		throw e;
+	}
+	await page.close();
+});
+
+describe('Smoke Test', () => {
+	it('`monaco` is exposed as global', async () => {
+		assert.strictEqual(await page.evaluate(`typeof monaco`), 'object');
+	});
+
+	/**
+	 * @param {string} text
+	 * @param {string} language
+	 * @returns Promise<void>
+	 */
+	async function createEditor(text, language) {
+		return await page.evaluate(
+			`window.ed = monaco.editor.create(document.getElementById('editor-container'), { value: '${text}', language: '${language}' })`
+		);
+	}
+
+	/**
+	 * @param {number} lineNumber
+	 * @param {number} column
+	 * @returns Promise<void>
+	 */
+	async function setEditorPosition(lineNumber, column) {
+		return await page.evaluate(
+			`window.ed.setPosition({ lineNumber: ${lineNumber}, column: ${column} });`
+		);
+	}
+
+	/**
+	 * @param {string} commandId
+	 * @param {any} args
+	 * @returns Promise<void>
+	 */
+	async function triggerEditorCommand(commandId, args) {
+		return await page.evaluate(
+			`window.ed.trigger(null, '${commandId}', ${args ? JSON.stringify(args) : 'undefined'});`
+		);
+	}
+
+	async function focusEditor() {
+		await page.evaluate(`window.ed.focus();`);
+	}
+
+	it('should be able to create plaintext editor', async () => {
+		await createEditor('hello world', 'plaintext');
+
+		// type a link in it
+		await setEditorPosition(1, 12);
+		await triggerEditorCommand('type', { text: '\nhttps://www.microsoft.com' });
+
+		// check that the link gets highlighted, which indicates that the web worker is healthy
+		await page.waitForSelector('.detected-link');
+	});
+
+	it('css smoke test', async () => {
+		await createEditor('.sel1 { background: red; }\\n.sel2 {}', 'css');
+
+		// check that a squiggle appears, which indicates that the language service is up and running
+		await page.waitForSelector('.squiggly-warning');
+	});
+
+	it('html smoke test', async () => {
+		await createEditor('<title>hi</title>', 'html');
+
+		// trigger hover
+		await focusEditor();
+		await setEditorPosition(1, 3);
+		await page.keyboard.press('F1');
+		await page.keyboard.type('Show Hover');
+		await page.keyboard.press('Enter');
+
+		// check that a hover explaining the `<title>` element appears, which indicates that the language service is up and running
+		await page.waitForSelector(`text=The title element represents the document's title or name`);
+	});
+
+	it('json smoke test', async () => {
+		await createEditor('{}', 'json');
+
+		// trigger suggestions
+		await focusEditor();
+		await setEditorPosition(1, 2);
+		await triggerEditorCommand('editor.action.triggerSuggest');
+
+		// check that a suggestion item for `$schema` appears, which indicates that the language service is up and running
+		await page.waitForSelector(`text=$schema`);
+	});
+
+	it('typescript smoke test', async () => {
+		await createEditor('window.add', 'typescript');
+
+		// check that a squiggle appears, which indicates that the language service is up and running
+		await page.waitForSelector('.squiggly-error');
+
+		// trigger suggestions
+		await focusEditor();
+		await setEditorPosition(1, 11);
+		await triggerEditorCommand('editor.action.triggerSuggest');
+
+		// check that a suggestion item for `addEventListener` appears, which indicates that the language service is up and running
+		await page.waitForSelector(`text=addEventListener`);
+	});
+});
+
+function timeout(ms) {
+	return new Promise((resolve, reject) => {
+		setTimeout(resolve, ms);
+	});
+}

+ 6 - 0
test/smoke/common.js

@@ -0,0 +1,6 @@
+/*---------------------------------------------------------------------------------------------
+ *  Copyright (c) Microsoft Corporation. All rights reserved.
+ *  Licensed under the MIT License. See License.txt in the project root for license information.
+ *--------------------------------------------------------------------------------------------*/
+
+exports.PORT = 8563;

+ 72 - 0
test/smoke/runner.js

@@ -0,0 +1,72 @@
+/*---------------------------------------------------------------------------------------------
+ *  Copyright (c) Microsoft Corporation. All rights reserved.
+ *  Licensed under the MIT License. See License.txt in the project root for license information.
+ *--------------------------------------------------------------------------------------------*/
+
+//@ts-check
+
+const yaserver = require('yaserver');
+const http = require('http');
+const cp = require('child_process');
+const path = require('path');
+const { PORT } = require('./common');
+const DEBUG_TESTS = process.argv.includes('--debug-tests');
+
+const REPO_ROOT = path.join(__dirname, '../../');
+
+yaserver
+	.createServer({
+		rootDir: REPO_ROOT
+	})
+	.then((staticServer) => {
+		const server = http.createServer((request, response) => {
+			return staticServer.handle(request, response);
+		});
+		server.listen(PORT, '127.0.0.1', async () => {
+			try {
+				await runTests();
+				console.log(`All good`);
+				process.exit(0);
+			} catch (err) {
+				console.error(err);
+				process.exit(1);
+			}
+		});
+	});
+
+async function runTests() {
+	await runTest('chromium');
+	await runTest('firefox');
+	await runTest('webkit');
+}
+
+function runTest(browser) {
+	return new Promise((resolve, reject) => {
+		const env = { BROWSER: browser, ...process.env };
+		if (DEBUG_TESTS) {
+			env['DEBUG_TESTS'] = 'true';
+		}
+		const proc = cp.spawn(
+			'node',
+			[
+				path.join(REPO_ROOT, 'node_modules/mocha/bin/mocha'),
+				'test/smoke/*.test.js',
+				'--headless',
+				'--timeout',
+				'20000'
+			],
+			{
+				env,
+				stdio: 'inherit'
+			}
+		);
+		proc.on('error', reject);
+		proc.on('exit', (code) => {
+			if (code === 0) {
+				resolve();
+			} else {
+				reject(code);
+			}
+		});
+	});
+}

Some files were not shown because too many files changed in this diff