smoke.test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. //@ts-check
  6. const playwright = require('playwright');
  7. const { assert } = require('chai');
  8. const { PORT } = require('./common');
  9. const browserType = process.env.BROWSER || 'chromium';
  10. const DEBUG_TESTS = Boolean(process.env.DEBUG_TESTS || false);
  11. const TESTS_TYPE = process.env.TESTS_TYPE || 'amd';
  12. const URL =
  13. TESTS_TYPE === 'amd'
  14. ? `http://127.0.0.1:${PORT}/test/smoke/amd.html`
  15. : `http://127.0.0.1:${PORT}/test/smoke/webpack/webpack.html`;
  16. suite(`Smoke Test '${TESTS_TYPE}'`, () => {
  17. /** @type {playwright.Browser} */
  18. let browser;
  19. /** @type {playwright.Page} */
  20. let page;
  21. suiteSetup(async () => {
  22. browser = await playwright[browserType].launch({
  23. headless: !DEBUG_TESTS,
  24. devtools: DEBUG_TESTS && browserType === 'chromium'
  25. // slowMo: DEBUG_TESTS ? 2000 : 0
  26. });
  27. });
  28. suiteTeardown(async () => {
  29. await browser.close();
  30. });
  31. let pageErrors = [];
  32. setup(async () => {
  33. pageErrors = [];
  34. page = await browser.newPage({
  35. viewport: {
  36. width: 800,
  37. height: 600
  38. }
  39. });
  40. page.on('pageerror', (e) => {
  41. console.log(e);
  42. pageErrors.push(e);
  43. });
  44. const response = await page.goto(URL);
  45. assert.ok(!!response);
  46. assert.strictEqual(response.status(), 200);
  47. });
  48. teardown(async () => {
  49. for (const e of pageErrors) {
  50. throw e;
  51. }
  52. await page.close();
  53. });
  54. /**
  55. * @param {string} text
  56. * @param {string} language
  57. * @returns Promise<void>
  58. */
  59. async function createEditor(text, language) {
  60. return await page.evaluate(
  61. `window.ed = monacoAPI.editor.create(document.getElementById('editor-container'), { value: '${text}', language: '${language}' })`
  62. );
  63. }
  64. /**
  65. * @param {number} lineNumber
  66. * @param {number} column
  67. * @returns Promise<void>
  68. */
  69. async function setEditorPosition(lineNumber, column) {
  70. return await page.evaluate(
  71. `window.ed.setPosition({ lineNumber: ${lineNumber}, column: ${column} });`
  72. );
  73. }
  74. /**
  75. * @param {string} commandId
  76. * @param {any} args
  77. * @returns Promise<void>
  78. */
  79. async function triggerEditorCommand(commandId, args) {
  80. return await page.evaluate(
  81. `window.ed.trigger(null, '${commandId}', ${args ? JSON.stringify(args) : 'undefined'});`
  82. );
  83. }
  84. async function focusEditor() {
  85. await page.evaluate(`window.ed.focus();`);
  86. }
  87. test('`monacoAPI` is exposed as global', async () => {
  88. assert.strictEqual(await page.evaluate(`typeof monacoAPI`), 'object');
  89. });
  90. test('should be able to create plaintext editor', async () => {
  91. await createEditor('hello world', 'plaintext');
  92. // type a link in it
  93. await setEditorPosition(1, 12);
  94. await triggerEditorCommand('type', { text: '\nhttps://www.microsoft.com' });
  95. // check that the link gets highlighted, which indicates that the web worker is healthy
  96. await page.waitForSelector('.detected-link');
  97. });
  98. test('css smoke test', async () => {
  99. await createEditor('.sel1 { background: red; }\\n.sel2 {}', 'css');
  100. // check that a squiggle appears, which indicates that the language service is up and running
  101. await page.waitForSelector('.squiggly-warning');
  102. });
  103. test('html smoke test', async () => {
  104. await createEditor('<title>hi</title>', 'html');
  105. // trigger hover
  106. await focusEditor();
  107. await setEditorPosition(1, 3);
  108. await page.keyboard.press('F1');
  109. await page.keyboard.type('Show Hover');
  110. await page.keyboard.press('Enter');
  111. // check that a hover explaining the `<title>` element appears, which indicates that the language service is up and running
  112. await page.waitForSelector(`text=The title element represents the document's title or name`);
  113. });
  114. test('json smoke test', async () => {
  115. await createEditor('{}', 'json');
  116. // trigger suggestions
  117. await focusEditor();
  118. await setEditorPosition(1, 2);
  119. await triggerEditorCommand('editor.action.triggerSuggest');
  120. // check that a suggestion item for `$schema` appears, which indicates that the language service is up and running
  121. await page.waitForSelector(`text=$schema`);
  122. });
  123. test('typescript smoke test', async () => {
  124. await createEditor('window.add', 'typescript');
  125. // check that a squiggle appears, which indicates that the language service is up and running
  126. await page.waitForSelector('.squiggly-error');
  127. // trigger suggestions
  128. await focusEditor();
  129. await setEditorPosition(1, 11);
  130. await triggerEditorCommand('editor.action.triggerSuggest');
  131. // check that a suggestion item for `addEventListener` appears, which indicates that the language service is up and running
  132. await page.waitForSelector(`text=addEventListener`);
  133. // find the TypeScript worker
  134. const tsWorker = page.workers().find((worker) => {
  135. const url = worker.url();
  136. return /ts\.worker\.js$/.test(url) || /workerMain.js#typescript$/.test(url);
  137. });
  138. assert.ok(!!tsWorker);
  139. // check that the TypeScript worker exposes `ts` as a global
  140. assert.strictEqual(await tsWorker.evaluate(`typeof ts`), 'object');
  141. // check that the TypeScript worker exposes the full `ts` as a global
  142. assert.strictEqual(await tsWorker.evaluate(`typeof ts.optionDeclarations`), 'object');
  143. });
  144. });
  145. function timeout(ms) {
  146. return new Promise((resolve, reject) => {
  147. setTimeout(resolve, ms);
  148. });
  149. }