Skip to content

Commit 0645631

Browse files
authored
Use TypeScript in tests (sveltejs#5433)
1 parent 3970def commit 0645631

File tree

18 files changed

+40
-44
lines changed

18 files changed

+40
-44
lines changed

.mocharc.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
module.exports = {
22
file: [
3-
'test/test.js'
3+
'test/test.ts'
4+
],
5+
require: [
6+
'sucrase/register'
47
]
58
};
69

710
// add coverage options when running 'npx c8 mocha'
811
if (process.env.NODE_V8_COVERAGE) {
9-
Object.assign(module.exports, {
10-
fullTrace: true,
11-
require: [
12-
'source-map-support/register'
13-
]
14-
});
12+
module.exports.fullTrace = true;
13+
module.exports.require.push('source-map-support/register');
1514
}

test/.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"rules": {
3-
"no-console": "off"
3+
"no-console": "off",
4+
"@typescript-eslint/no-var-requires": "off"
45
}
56
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import * as assert from 'assert';
21
import * as fs from 'fs';
3-
import { env, svelte, setupHtmlEqual, shouldUpdateExpected } from '../helpers.js';
2+
import { assert, env, svelte, setupHtmlEqual, shouldUpdateExpected } from '../helpers';
43

54
function try_require(file) {
65
try {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as fs from 'fs';
22
import * as path from 'path';
33
import * as http from 'http';
44
import { rollup } from 'rollup';
5-
import * as virtual from '@rollup/plugin-virtual';
6-
import * as puppeteer from 'puppeteer';
7-
import { addLineNumbers, loadConfig, loadSvelte } from "../helpers.js";
5+
import virtual from '@rollup/plugin-virtual';
6+
import puppeteer from 'puppeteer';
7+
import { addLineNumbers, loadConfig, loadSvelte } from "../helpers";
88
import { deepEqual } from 'assert';
99

1010
const page = `

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import * as assert$1 from 'assert';
12
import * as jsdom from 'jsdom';
2-
import * as assert from 'assert';
3-
import * as glob from 'tiny-glob/sync.js';
3+
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, expected, message?) => void };
78

89
// for coverage purposes, we need to test source files,
910
// but for sanity purposes, we need to test dist files
@@ -63,7 +64,7 @@ global.window = window;
6364

6465
// add missing ecmascript globals to window
6566
for (const key of Object.getOwnPropertyNames(global)) {
66-
window[key] = window[key] || global[key];
67+
if (!(key in window)) window[key] = global[key];
6768
}
6869

6970
// implement mock scroll

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import * as assert from 'assert';
21
import * as path from 'path';
32
import * as fs from 'fs';
43

54
import {
5+
assert,
66
showOutput,
77
loadConfig,
88
loadSvelte,
99
env,
1010
setupHtmlEqual,
1111
shouldUpdateExpected
12-
} from '../helpers.js';
12+
} from '../helpers';
1313

1414
let compileOptions = null;
1515

@@ -58,13 +58,7 @@ describe('hydration', () => {
5858
try {
5959
global.window = window;
6060

61-
let SvelteComponent;
62-
63-
try {
64-
SvelteComponent = require(`${cwd}/main.svelte`).default;
65-
} catch (err) {
66-
throw err;
67-
}
61+
const SvelteComponent = require(`${cwd}/main.svelte`).default;
6862

6963
const target = window.document.body;
7064
const head = window.document.head;
@@ -75,7 +69,9 @@ describe('hydration', () => {
7569
try {
7670
before_head = fs.readFileSync(`${cwd}/_before_head.html`, 'utf-8');
7771
head.innerHTML = before_head;
78-
} catch (err) {}
72+
} catch (err) {
73+
// continue regardless of error
74+
}
7975

8076
const snapshot = config.snapshot ? config.snapshot(target) : {};
8177

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as assert from "assert";
22
import * as fs from "fs";
33
import * as path from "path";
44
import * as colors from "kleur";
5-
import { loadConfig, svelte, shouldUpdateExpected } from "../helpers.js";
5+
import { loadConfig, svelte, shouldUpdateExpected } from "../helpers";
66

77
describe("js", () => {
88
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
File renamed without changes.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as assert from 'assert';
22
import * as fs from 'fs';
3-
import { svelte, tryToLoadJson, shouldUpdateExpected } from '../helpers.js';
3+
import { svelte, tryToLoadJson, shouldUpdateExpected } from '../helpers';
44

55
describe('parse', () => {
66
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
@@ -38,7 +38,7 @@ describe('parse', () => {
3838
} catch (err) {
3939
if (err.name !== 'ParseError') throw err;
4040
if (!expectedError) throw err;
41-
const { code, message, pos, start } = err
41+
const { code, message, pos, start } = err;
4242
try {
4343
assert.deepEqual({ code, message, pos, start }, expectedError);
4444
} catch (err2) {
File renamed without changes.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs';
22
import * as assert from 'assert';
3-
import { loadConfig, svelte } from '../helpers.js';
3+
import { loadConfig, svelte } from '../helpers';
44

55
describe('preprocess', () => {
66
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import * as assert from "assert";
21
import * as path from "path";
32
import * as fs from "fs";
43
import { rollup } from 'rollup';
5-
import * as virtual from '@rollup/plugin-virtual';
6-
import * as glob from 'tiny-glob/sync.js';
4+
import virtual from '@rollup/plugin-virtual';
5+
import glob from 'tiny-glob/sync.js';
76
import { clear_loops, flush, set_now, set_raf } from "../../internal";
87

98
import {
9+
assert,
1010
showOutput,
1111
loadConfig,
1212
loadSvelte,
1313
cleanRequireCache,
1414
env,
1515
setupHtmlEqual,
1616
mkdirp
17-
} from "../helpers.js";
17+
} from "../helpers";
1818

1919
let svelte$;
2020
let svelte;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import * as assert from "assert";
21
import * as fs from "fs";
32
import * as path from "path";
4-
import * as glob from 'tiny-glob/sync.js';
3+
import glob from 'tiny-glob/sync.js';
54

65
import {
6+
assert,
77
showOutput,
88
loadConfig,
99
loadSvelte,
@@ -12,7 +12,7 @@ import {
1212
cleanRequireCache,
1313
shouldUpdateExpected,
1414
mkdirp
15-
} from "../helpers.js";
15+
} from "../helpers";
1616

1717
function tryToReadFile(file) {
1818
try {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from "fs";
22
import * as path from "path";
33
import * as assert from "assert";
4-
import { svelte } from "../helpers.js";
4+
import { svelte } from "../helpers";
55
import { SourceMapConsumer } from "source-map";
66
import { getLocator } from "locate-character";
77

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs';
22
import * as assert from 'assert';
3-
import { svelte, loadConfig, tryToLoadJson } from '../helpers.js';
3+
import { svelte, loadConfig, tryToLoadJson } from '../helpers';
44

55
describe('stats', () => {
66
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ const glob = require('tiny-glob/sync.js');
33
require('./setup');
44

55
// bind internal to jsdom
6-
require('./helpers');
6+
require('./helpers.ts');
77
require('../internal');
88

99
console.clear();
1010

11-
const test_folders = glob('*/index.js', { cwd: 'test' });
11+
const test_folders = glob('*/index.ts', { cwd: 'test' });
1212
const solo_folders = test_folders.filter(folder => /\.solo/.test(folder));
1313

1414
if (solo_folders.length) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from "fs";
22
import * as assert from "assert";
3-
import { svelte, loadConfig, tryToLoadJson } from "../helpers.js";
3+
import { svelte, loadConfig, tryToLoadJson } from "../helpers";
44

55
describe("validate", () => {
66
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
@@ -30,7 +30,7 @@ describe("validate", () => {
3030
legacy: config.legacy,
3131
generate: false,
3232
customElement: config.customElement,
33-
...options,
33+
...options
3434
});
3535

3636
assert.deepEqual(warnings.map(w => ({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs';
22
import * as assert from 'assert';
3-
import { svelte, loadConfig, tryToLoadJson } from '../helpers.js';
3+
import { svelte, loadConfig, tryToLoadJson } from '../helpers';
44

55
describe('vars', () => {
66
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {

0 commit comments

Comments
 (0)