Skip to content

Commit 384927d

Browse files
authored
chore: TypeScript to JavaScript + JSDoc for tests (#8526)
1 parent 8ffd211 commit 384927d

File tree

22 files changed

+124
-50
lines changed

22 files changed

+124
-50
lines changed

.mocharc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
const is_unit_test = process.env.UNIT_TEST;
22

33
module.exports = {
4-
file: is_unit_test ? [] : ['test/test.ts'],
4+
file: is_unit_test ? [] : ['test/test.js'],
55
require: [
66
'sucrase/register'
7+
],
8+
"node-option": [
9+
"experimental-modules"
710
]
811
};
912

File renamed without changes.

test/custom-elements/index.ts renamed to test/custom-elements/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const page = `
1616

1717
const assert = fs.readFileSync(`${__dirname}/assert.js`, 'utf-8');
1818

19-
describe('custom-elements', function() {
19+
describe('custom-elements', function () {
2020
// Note: Increase the timeout in preparation for restarting Chromium due to SIGSEGV.
2121
this.timeout(10000);
2222
let svelte;
@@ -81,6 +81,7 @@ describe('custom-elements', function() {
8181
const bundle = await rollup({
8282
input: `${__dirname}/samples/${dir}/test.js`,
8383
plugins: [
84+
// @ts-ignore -- TODO: fix this
8485
{
8586
resolveId(importee) {
8687
if (importee === 'svelte/internal' || importee === './internal') {

test/helpers.ts renamed to test/helpers.js

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import glob from 'tiny-glob/sync';
44
import * as path from 'path';
55
import * as fs from 'fs';
66
import * as colors from 'kleur';
7-
export const assert = (assert$1 as unknown) as typeof assert$1 & { htmlEqual: (actual: string, expected: string, message?: string) => void, htmlEqualWithOptions: (actual: string, expected: string, options: { preserveComments?: boolean, withoutNormalizeHtml?: boolean }, message?: string) => void };
7+
8+
/**
9+
* @type {typeof assert$1 & { htmlEqual: (actual: string, expected: string, message?: string) => void, htmlEqualWithOptions: (actual: string, expected: string, options: { preserveComments?: boolean, withoutNormalizeHtml?: boolean }, message?: string) => void }}
10+
*/
11+
export const assert = /** @type {any} */ (assert$1);
812

913
// for coverage purposes, we need to test source files,
1014
// but for sanity purposes, we need to test dist files
11-
export function loadSvelte(test: boolean = false) {
15+
export function loadSvelte(test = false) {
1216
process.env.TEST = test ? 'true' : '';
1317

1418
const resolved = require.resolve('../compiler.js');
@@ -68,7 +72,7 @@ for (const key of Object.getOwnPropertyNames(global)) {
6872
}
6973

7074
// implement mock scroll
71-
window.scrollTo = function(pageXOffset, pageYOffset) {
75+
window.scrollTo = function (pageXOffset, pageYOffset) {
7276
window.pageXOffset = pageXOffset;
7377
window.pageYOffset = pageYOffset;
7478
};
@@ -140,7 +144,14 @@ function cleanChildren(node) {
140144
}
141145
}
142146

143-
export function normalizeHtml(window, html, { removeDataSvelte = false, preserveComments = false }: { removeDataSvelte?: boolean, preserveComments?: boolean }) {
147+
/**
148+
*
149+
* @param {Window} window
150+
* @param {string} html
151+
* @param {{ removeDataSvelte?: boolean, preserveComments?: boolean }} param2
152+
* @returns
153+
*/
154+
export function normalizeHtml(window, html, { removeDataSvelte = false, preserveComments = false }) {
144155
try {
145156
const node = window.document.createElement('div');
146157
node.innerHTML = html
@@ -155,11 +166,18 @@ export function normalizeHtml(window, html, { removeDataSvelte = false, preserve
155166
}
156167
}
157168

158-
export function normalizeNewline(html: string) {
169+
/**
170+
* @param {string} html
171+
* @returns {string}
172+
*/
173+
export function normalizeNewline(html) {
159174
return html.replace(/\r\n/g, '\n');
160175
}
161176

162-
export function setupHtmlEqual(options: { removeDataSvelte?: boolean } = {}) {
177+
/**
178+
* @param {{ removeDataSvelte?: boolean }} options
179+
*/
180+
export function setupHtmlEqual(options = {}) {
163181
const window = env();
164182

165183
// eslint-disable-next-line no-import-assign
@@ -170,8 +188,15 @@ export function setupHtmlEqual(options: { removeDataSvelte?: boolean } = {}) {
170188
message
171189
);
172190
};
173-
// eslint-disable-next-line no-import-assign
174-
assert.htmlEqualWithOptions = (actual: string, expected: string, { preserveComments, withoutNormalizeHtml }: { preserveComments?: boolean, withoutNormalizeHtml?: boolean }, message?: string) => {
191+
192+
/**
193+
*
194+
* @param {string} actual
195+
* @param {string} expected
196+
* @param {{ preserveComments?: boolean, withoutNormalizeHtml?: boolean }} param2
197+
* @param {string?} message
198+
*/
199+
assert.htmlEqualWithOptions = (actual, expected, { preserveComments, withoutNormalizeHtml }, message) => {
175200
assert.deepEqual(
176201
withoutNormalizeHtml
177202
? normalizeNewline(actual).replace(/(\sdata-svelte-h="[^"]+")/g, options.removeDataSvelte ? '' : '$1')
@@ -252,7 +277,8 @@ const original_set_timeout = global.setTimeout;
252277
export function useFakeTimers() {
253278
const callbacks = [];
254279

255-
global.setTimeout = function(fn) {
280+
// @ts-ignore
281+
global.setTimeout = function (fn) {
256282
callbacks.push(fn);
257283
};
258284

@@ -289,7 +315,14 @@ export function prettyPrintPuppeteerAssertionError(message) {
289315
}
290316
}
291317

292-
export async function retryAsync<T>(fn: () => Promise<T>, maxAttempts: number = 3, interval: number = 1000): Promise<T> {
318+
/**
319+
*
320+
* @param {() => Promise<import ('puppeteer').Browser>} fn
321+
* @param {number} maxAttempts
322+
* @param {number} interval
323+
* @returns {Promise<import ('puppeteer').Browser>}
324+
*/
325+
export async function retryAsync(fn, maxAttempts = 3, interval = 1000) {
293326
let attempts = 0;
294327
while (attempts <= maxAttempts) {
295328
try {
@@ -301,15 +334,23 @@ export async function retryAsync<T>(fn: () => Promise<T>, maxAttempts: number =
301334
}
302335
}
303336

304-
// NOTE: Chromium may exit with SIGSEGV, so retry in that case
305-
export async function executeBrowserTest<T>(browser, launchPuppeteer: () => Promise<T>, additionalAssertion: () => void, onError: (err: Error) => void) {
337+
/**
338+
* NOTE: Chromium may exit with SIGSEGV, so retry in that case
339+
* @param {import ('puppeteer').Browser} browser
340+
* @param {() => Promise<import ('puppeteer').Browser>} launchPuppeteer
341+
* @param {() => void} additionalAssertion
342+
* @param {(err: Error) => void} onError
343+
* @returns {Promise<import ('puppeteer').Browser>}
344+
*/
345+
export async function executeBrowserTest(browser, launchPuppeteer, additionalAssertion, onError) {
306346
let count = 0;
307347
do {
308348
count++;
309349
try {
310350
const page = await browser.newPage();
311351

312352
page.on('console', (type) => {
353+
// @ts-ignore -- TODO: Fix type
313354
console[type._type](type._text);
314355
});
315356

@@ -318,7 +359,10 @@ export async function executeBrowserTest<T>(browser, launchPuppeteer: () => Prom
318359
console.error(error);
319360
});
320361
await page.goto('http://localhost:6789');
321-
const result = await page.evaluate(() => test(document.querySelector('main')));
362+
const result = await page.evaluate(() => {
363+
// @ts-ignore -- It runs in browser context.
364+
return test(document.querySelector('main'));
365+
});
322366
if (result) console.log(result);
323367
additionalAssertion();
324368
await page.close();

test/hydration/index.ts renamed to test/hydration/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('hydration', () => {
1919
before(() => {
2020
const svelte = loadSvelte();
2121

22-
require.extensions['.svelte'] = function(module, filename) {
22+
require.extensions['.svelte'] = function (module, filename) {
2323
const options = Object.assign(
2424
{
2525
filename,
@@ -135,6 +135,6 @@ describe('hydration', () => {
135135
}
136136

137137
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
138-
runTest(dir, null);
138+
runTest(dir);
139139
});
140140
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)