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

+4-1
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

+2-1
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

+57-13
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

+2-2
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.
File renamed without changes.
File renamed without changes.

test/server-side-rendering/index.ts renamed to test/server-side-rendering/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('ssr', () => {
6767
format: 'cjs'
6868
};
6969

70-
require('../../register')(compileOptions);
70+
require('../../register.js')(compileOptions);
7171

7272
const Component = require(`${dir}/main.svelte`).default;
7373

@@ -174,7 +174,7 @@ describe('ssr', () => {
174174
glob('**/*.svelte', { cwd }).forEach(file => {
175175
if (file[0] === '_') return;
176176

177-
const dir = `${cwd}/_output/ssr`;
177+
const dir = `${cwd}/_output/ssr`;
178178
const out = `${dir}/${file.replace(/\.svelte$/, '.js')}`;
179179

180180
if (fs.existsSync(out)) {
@@ -208,12 +208,12 @@ describe('ssr', () => {
208208

209209
if (config.ssrHtml) {
210210
assert.htmlEqualWithOptions(html, config.ssrHtml, {
211-
preserveComments: compileOptions.preserveComments,
211+
preserveComments: compileOptions.preserveComments,
212212
withoutNormalizeHtml: config.withoutNormalizeHtml
213213
});
214214
} else if (config.html) {
215215
assert.htmlEqualWithOptions(html, config.html, {
216-
preserveComments: compileOptions.preserveComments,
216+
preserveComments: compileOptions.preserveComments,
217217
withoutNormalizeHtml: config.withoutNormalizeHtml
218218
});
219219
}

test/setup.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ const fs = require('fs');
22

33
require('source-map-support').install();
44

5-
process.env.TEST = true;
5+
process.env.TEST = 'true';
66

7-
require.extensions['.js'] = function(module, filename) {
7+
require.extensions['.js'] = function (module, filename) {
88
const exports = [];
99

1010
let code = fs.readFileSync(filename, 'utf-8')
11-
.replace(/^import \* as (\w+) from ['"]([^'"]+)['"];?/gm, 'var $1 = require("$2");')
12-
.replace(/^import (\w+) from ['"]([^'"]+)['"];?/gm, 'var {default: $1} = require("$2");')
11+
.replace(/^import \* as (\S+) from ['"]([^'"]+)['"];?/gm, 'var $1 = require("$2");')
1312
.replace(/^import {([^}]+)} from ['"](.+)['"];?/gm, 'var {$1} = require("$2");')
13+
.replace(/^import (\S+),\s+?{([^}]+)} from ['"](.+)['"];?/gm, 'var {default: $1, $2} = require("$3");')
14+
.replace(/^import (\S+) from ['"]([./][^'"]+)['"];?/gm, 'var {default: $1} = require("$2");')
15+
.replace(/^import (\S+) from ['"]([^'"]+)['"];?/gm, 'var $1 = require("$2");')
1416
.replace(/^export default /gm, 'exports.default = ')
1517
.replace(/^export (const|let|var|class|function|async\s+function) (\w+)/gm, (match, type, name) => {
1618
exports.push(name);

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

+41-20
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ import * as assert from 'assert';
22
import { getLocator } from 'locate-character';
33
import MagicString, { Bundle } from 'magic-string';
44

5-
type AssertMappedParameters = {
6-
code: string;
7-
filename?: string;
8-
input: string | ReturnType<typeof getLocator>;
9-
input_code?: string;
10-
preprocessed: any;
11-
};
5+
/**
6+
* @typedef {{ code: string; filename?: string; input: string | ReturnType<typeof getLocator>; input_code?: string; preprocessed: any; }} AssertMappedParameters
7+
*/
128

9+
/**
10+
* @param {AssertMappedParameters} param
11+
*/
1312
export function assert_mapped(
14-
{ code, filename, input, input_code, preprocessed }: AssertMappedParameters
13+
{ code, filename, input, input_code, preprocessed }
1514
) {
1615
const locate_input = typeof input === 'function' ? input : getLocator(input);
1716
if (filename === undefined) filename = 'input.svelte';
18-
if (input_code === undefined) input_code = code;
17+
if (input_code === undefined) input_code = code;
1918

2019
const source_loc = locate_input(input_code);
2120
assert.notEqual(
@@ -43,14 +42,15 @@ export function assert_mapped(
4342
);
4443
}
4544

46-
type AssertNotMappedParameters = {
47-
code: string;
48-
filename?: string;
49-
preprocessed: any;
50-
};
45+
/**
46+
* @typedef {{ code: string; filename?: string; preprocessed: any; }} AssertNotMappedParameters
47+
*/
5148

49+
/**
50+
* @param {AssertNotMappedParameters} param
51+
*/
5252
export function assert_not_mapped(
53-
{ code, filename, preprocessed }: AssertNotMappedParameters
53+
{ code, filename, preprocessed }
5454
) {
5555
if (filename === undefined) filename = 'input.svelte';
5656

@@ -73,9 +73,14 @@ export function assert_not_mapped(
7373
);
7474
}
7575

76+
/**
77+
* @param {string} code
78+
* @param {ReturnType<typeof getLocator>} locate
79+
* @param {string} filename
80+
*/
7681
export function assert_not_located(
77-
code: string,
78-
locate: ReturnType<typeof getLocator>,
82+
code,
83+
locate,
7984
filename = 'input.svelte'
8085
) {
8186
assert.equal(
@@ -85,8 +90,14 @@ export function assert_not_located(
8590
);
8691
}
8792

93+
/**
94+
* @param {Array<{ code: string | MagicString, filename: string }>} inputs
95+
* @param {string} filename
96+
* @param {string} separator
97+
* @returns
98+
*/
8899
export function magic_string_bundle(
89-
inputs: Array<{ code: string | MagicString, filename: string }>,
100+
inputs,
90101
filename = 'bundle.js',
91102
separator = '\n'
92103
) {
@@ -107,7 +118,12 @@ export function magic_string_bundle(
107118
};
108119
}
109120

110-
export function magic_string_preprocessor_result(filename: string, src: MagicString) {
121+
/**
122+
* @param {string} filename
123+
* @param {MagicString} src
124+
* @returns
125+
*/
126+
export function magic_string_preprocessor_result(filename, src) {
111127
return {
112128
code: src.toString(),
113129
map: src.generateMap({
@@ -118,7 +134,12 @@ export function magic_string_preprocessor_result(filename: string, src: MagicStr
118134
};
119135
}
120136

121-
export function magic_string_replace_all(src: MagicString, search: string, replace: string) {
137+
/**
138+
* @param {MagicString} src
139+
* @param {string} search
140+
* @param {string} replace
141+
*/
142+
export function magic_string_replace_all(src, search, replace) {
122143
let idx = src.original.indexOf(search);
123144
if (idx == -1) throw new Error('search not found in src');
124145
do {
File renamed without changes.
File renamed without changes.

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ describe('store', () => {
101101
describe('readable', () => {
102102
it('creates a readable store', () => {
103103
let running;
104+
/** @type {import('svelte/store').Subscriber<unknown>} */
104105
let tick;
105106

106107
const store = readable(undefined, set => {
@@ -163,11 +164,12 @@ describe('store', () => {
163164
});
164165
});
165166

167+
/** @type {any} */
166168
const fake_observable = {
167169
subscribe(fn) {
168170
fn(42);
169171
return {
170-
unsubscribe: () => {}
172+
unsubscribe: () => { }
171173
};
172174
}
173175
};
@@ -290,7 +292,7 @@ describe('store', () => {
290292
});
291293

292294
it('derived dependency does not update and shared ancestor updates', () => {
293-
const root = writable({ a: 0, b:0 });
295+
const root = writable({ a: 0, b: 0 });
294296
const values = [];
295297

296298
const a = derived(root, $root => {
@@ -451,6 +453,7 @@ describe('store', () => {
451453
assert.equal(get(readableStore), 2);
452454
assert.equal(get(readableStore), get(writableStore));
453455

456+
// @ts-ignore
454457
assert.throws(() => readableStore.set(3));
455458
});
456459

0 commit comments

Comments
 (0)