Skip to content

Commit 6d503c8

Browse files
committed
refactor(astro): migrate 19 tests to typescript
1 parent 32b361d commit 6d503c8

19 files changed

Lines changed: 90 additions & 97 deletions

packages/astro/test/scoped-style-strategy.test.js renamed to packages/astro/test/scoped-style-strategy.test.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import assert from 'node:assert/strict';
22
import { before, describe, it } from 'node:test';
33
import * as cheerio from 'cheerio';
4-
import { loadFixture } from './test-utils.js';
4+
import { type Fixture, loadFixture } from './test-utils.js';
55

66
describe('scopedStyleStrategy', () => {
77
describe('scopedStyleStrategy: "where"', () => {
8-
/** @type {import('./test-utils').Fixture} */
9-
let fixture;
10-
let stylesheet;
8+
let fixture: Fixture;
9+
let stylesheet: string;
1110

1211
before(async () => {
1312
fixture = await loadFixture({
@@ -21,7 +20,7 @@ describe('scopedStyleStrategy', () => {
2120
const html = await fixture.readFile('/index.html');
2221
const $ = cheerio.load(html);
2322
const $link = $('link[rel=stylesheet]');
24-
const href = $link.attr('href');
23+
const href = $link.attr('href')!;
2524
stylesheet = await fixture.readFile(href);
2625
});
2726

@@ -35,9 +34,8 @@ describe('scopedStyleStrategy', () => {
3534
});
3635

3736
describe('scopedStyleStrategy: "class"', () => {
38-
/** @type {import('./test-utils').Fixture} */
39-
let fixture;
40-
let stylesheet;
37+
let fixture: Fixture;
38+
let stylesheet: string;
4139

4240
before(async () => {
4341
fixture = await loadFixture({
@@ -51,7 +49,7 @@ describe('scopedStyleStrategy', () => {
5149
const html = await fixture.readFile('/index.html');
5250
const $ = cheerio.load(html);
5351
const $link = $('link[rel=stylesheet]');
54-
const href = $link.attr('href');
52+
const href = $link.attr('href')!;
5553
stylesheet = await fixture.readFile(href);
5654
});
5755

@@ -65,9 +63,8 @@ describe('scopedStyleStrategy', () => {
6563
});
6664

6765
describe('default', () => {
68-
/** @type {import('./test-utils').Fixture} */
69-
let fixture;
70-
let stylesheet;
66+
let fixture: Fixture;
67+
let stylesheet: string;
7168

7269
before(async () => {
7370
fixture = await loadFixture({
@@ -80,7 +77,7 @@ describe('scopedStyleStrategy', () => {
8077
const html = await fixture.readFile('/index.html');
8178
const $ = cheerio.load(html);
8279
const $link = $('link[rel=stylesheet]');
83-
const href = $link.attr('href');
80+
const href = $link.attr('href')!;
8481
stylesheet = await fixture.readFile(href);
8582
});
8683

packages/astro/test/serializeManifest.test.js renamed to packages/astro/test/serializeManifest.test.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import * as cheerio from 'cheerio';
44
import { ServerOnlyModule } from '../dist/core/errors/errors-data.js';
55
import { AstroError } from '../dist/core/errors/index.js';
66
import testAdapter from './test-adapter.js';
7-
import { loadFixture } from './test-utils.js';
7+
import { type App, type DevServer, type Fixture, loadFixture } from './test-utils.js';
88

99
describe('astro:config/client', () => {
10-
/** @type {import('./test-utils').Fixture} */
11-
let fixture;
12-
let devServer;
10+
let fixture: Fixture;
11+
let devServer: DevServer;
1312

1413
describe('in dev', async () => {
1514
before(async () => {
@@ -90,8 +89,7 @@ describe('astro:config/client', () => {
9089
});
9190

9291
describe('astro:config/client in a client script', () => {
93-
/** @type {import('./test-utils').Fixture} */
94-
let fixture;
92+
let fixture: Fixture;
9593

9694
describe('when build', () => {
9795
before(async () => {
@@ -110,10 +108,9 @@ describe('astro:config/client in a client script', () => {
110108
});
111109

112110
describe('astro:config/server', () => {
113-
/** @type {import('./test-utils').Fixture} */
114-
let fixture;
115-
let devServer;
116-
let app;
111+
let fixture: Fixture;
112+
let devServer: DevServer;
113+
let app: App;
117114

118115
describe('when build', () => {
119116
before(async () => {
Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
// @ts-check
2-
31
import { describe, it } from 'node:test';
4-
import { loadFixture } from './test-utils.js';
2+
import { type App, type Fixture, loadFixture } from './test-utils.js';
53
import testAdapter, { selfTestAdapter } from './test-adapter.js';
64
import assert from 'node:assert/strict';
7-
import fakeAdapter from './fixtures/server-entry/fake-adapter/index.js';
85
import { existsSync } from 'node:fs';
96
import { fileURLToPath } from 'node:url';
7+
import type { AstroIntegration } from 'astro';
8+
9+
type FakeAdapter = (
10+
options:
11+
| { type: 'rollupInput'; shape: 'string' | 'object' | 'array' }
12+
| { type: 'serverEntrypoint' },
13+
) => AstroIntegration;
14+
15+
const fakeAdapter: FakeAdapter = await (async () => {
16+
const importPath: string = './fixtures/server-entry/fake-adapter/index.js';
17+
const mod = await import(importPath);
18+
return mod.default;
19+
})();
1020

1121
describe('Server entry', () => {
12-
/** @type {import('./test-utils').Fixture} */
13-
let fixture;
14-
let app;
22+
let fixture: Fixture;
23+
let app: App;
1524

1625
it('should load the custom entry when using legacy entrypoint', async () => {
1726
fixture = await loadFixture({
@@ -23,7 +32,7 @@ describe('Server entry', () => {
2332
},
2433
});
2534

26-
await fixture.build({});
35+
await fixture.build();
2736
app = await fixture.loadTestAdapterApp(false);
2837

2938
const request = new Request('http://example.com/');
@@ -41,7 +50,7 @@ describe('Server entry', () => {
4150
},
4251
});
4352

44-
await fixture.build({});
53+
await fixture.build();
4554
app = await fixture.loadSelfAdapterApp(false);
4655

4756
const request = new Request('http://example.com/');
@@ -59,7 +68,7 @@ describe('Server entry', () => {
5968
},
6069
});
6170

62-
await fixture.build({});
71+
await fixture.build();
6372

6473
assert.ok(existsSync(fileURLToPath(new URL('server/custom.mjs', fixture.config.outDir))));
6574
});
@@ -74,7 +83,7 @@ describe('Server entry', () => {
7483
},
7584
});
7685

77-
await fixture.build({});
86+
await fixture.build();
7887

7988
assert.ok(existsSync(fileURLToPath(new URL('server/custom.mjs', fixture.config.outDir))));
8089
});
@@ -89,7 +98,7 @@ describe('Server entry', () => {
8998
},
9099
});
91100

92-
await fixture.build({});
101+
await fixture.build();
93102

94103
assert.ok(existsSync(fileURLToPath(new URL('server/custom.mjs', fixture.config.outDir))));
95104
});
@@ -104,7 +113,7 @@ describe('Server entry', () => {
104113
},
105114
});
106115

107-
await fixture.build({});
116+
await fixture.build();
108117

109118
assert.ok(existsSync(fileURLToPath(new URL('server/custom.mjs', fixture.config.outDir))));
110119
});

packages/astro/test/server-islands.test.js renamed to packages/astro/test/server-islands.test.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import * as cheerio from 'cheerio';
55

66
import { encryptString } from '../dist/core/encryption.js';
77
import testAdapter from './test-adapter.js';
8-
import { loadFixture } from './test-utils.js';
8+
import { type DevServer, type Fixture, loadFixture } from './test-utils.js';
99

1010
// Helper to create encryption key from test key string
11-
async function createKeyFromString(keyString) {
11+
async function createKeyFromString(keyString: string) {
1212
const binaryString = atob(keyString);
1313
const bytes = new Uint8Array(binaryString.length);
1414
for (let i = 0; i < binaryString.length; i++) {
@@ -30,8 +30,7 @@ async function getEncryptedComponentExport(
3030

3131
describe('Server islands', () => {
3232
describe('SSR', () => {
33-
/** @type {import('./test-utils').Fixture} */
34-
let fixture;
33+
let fixture: Fixture;
3534
before(async () => {
3635
fixture = await loadFixture({
3736
root: './fixtures/server-islands/ssr',
@@ -43,7 +42,7 @@ describe('Server islands', () => {
4342
});
4443

4544
describe('dev', () => {
46-
let devServer;
45+
let devServer: DevServer;
4746

4847
before(async () => {
4948
process.env.ASTRO_KEY = 'eKBaVEuI7YjfanEXHuJe/pwZKKt3LkAHeMxvTU7aR0M=';
@@ -167,7 +166,7 @@ describe('Server islands', () => {
167166
const res = await fixture.fetch('/test');
168167
assert.equal(res.status, 200);
169168
const html = await res.text();
170-
const fetchMatch = html.match(/fetch\('\/_server-islands\/Island\?[^']*p=([^&']*)/);
169+
const fetchMatch = html.match(/fetch\('\/_server-islands\/Island\?[^']*p=([^&']*)/)!;
171170
assert.equal(fetchMatch.length, 2, 'should include props in the query string');
172171
assert.equal(fetchMatch[1], '', 'should not include encrypted empty props');
173172
});
@@ -176,7 +175,7 @@ describe('Server islands', () => {
176175
const res = await fixture.fetch('/fragment');
177176
assert.equal(res.status, 200);
178177
const html = await res.text();
179-
const fetchMatch = html.match(/fetch\('\/_server-islands\/Island\?[^']*p=([^&']*)/);
178+
const fetchMatch = html.match(/fetch\('\/_server-islands\/Island\?[^']*p=([^&']*)/)!;
180179
assert.equal(fetchMatch.length, 2, 'should include props in the query string');
181180
assert.equal(fetchMatch[1], '', 'should not include encrypted empty props');
182181
});
@@ -185,7 +184,7 @@ describe('Server islands', () => {
185184
const res = await fixture.fetch('/fragment');
186185
assert.equal(res.status, 200);
187186
const html = await res.text();
188-
const fetchMatch = html.match(/fetch\('\/_server-islands\/Island\?[^']*p=([^&']*)/);
187+
const fetchMatch = html.match(/fetch\('\/_server-islands\/Island\?[^']*p=([^&']*)/)!;
189188
assert.equal(fetchMatch.length, 2, 'should include props in the query string');
190189
assert.equal(fetchMatch[1], '', 'should not include encrypted empty props');
191190
});
@@ -195,7 +194,7 @@ describe('Server islands', () => {
195194
assert.equal(res.status, 200);
196195
const html = await res.text();
197196
// Extract the island fetch URL from the page
198-
const urlMatch = html.match(/fetch\('(\/_server-islands\/Wrapper\?[^']+)'/);
197+
const urlMatch = html.match(/fetch\('(\/_server-islands\/Wrapper\?[^']+)'/)!;
199198
assert.ok(urlMatch, 'should have a server island fetch URL');
200199
const islandRes = await fixture.fetch(urlMatch[1]);
201200
assert.equal(islandRes.status, 200);
@@ -345,16 +344,15 @@ describe('Server islands', () => {
345344
const res = await app.render(request);
346345
assert.equal(res.status, 200);
347346
const html = await res.text();
348-
const fetchMatch = html.match(/fetch\('\/_server-islands\/Island\?[^']*p=([^&']*)/);
347+
const fetchMatch = html.match(/fetch\('\/_server-islands\/Island\?[^']*p=([^&']*)/)!;
349348
assert.equal(fetchMatch.length, 2, 'should include props in the query string');
350349
assert.equal(fetchMatch[1], '', 'should not include encrypted empty props');
351350
});
352351
});
353352
});
354353

355354
describe('Hybrid mode', () => {
356-
/** @type {import('./test-utils').Fixture} */
357-
let fixture;
355+
let fixture: Fixture;
358356
before(async () => {
359357
fixture = await loadFixture({
360358
root: './fixtures/server-islands/hybrid',
@@ -384,7 +382,7 @@ describe('Server islands', () => {
384382

385383
const $ = cheerio.load(html);
386384
const serverIslandScript = $('script').filter((_, el) =>
387-
$(el).html().trim().startsWith('async function replaceServerIsland'),
385+
($(el).html() ?? '').trim().startsWith('async function replaceServerIsland'),
388386
);
389387
assert.equal(
390388
serverIslandScript.length,
@@ -411,7 +409,7 @@ describe('Server islands', () => {
411409
const res = await devFixture.fetch('/');
412410
assert.equal(res.status, 200);
413411
const html = await res.text();
414-
const fetchMatch = /fetch\('(\/_server-islands\/Island[^']*)/.exec(html);
412+
const fetchMatch = /fetch\('(\/_server-islands\/Island[^']*)/.exec(html)!;
415413
assert.ok(fetchMatch, 'should have a server island fetch URL');
416414
const islandRes = await devFixture.fetch(fetchMatch[1]);
417415
assert.equal(
@@ -425,7 +423,7 @@ describe('Server islands', () => {
425423
});
426424

427425
describe('with no adapter', () => {
428-
let devServer;
426+
let devServer: DevServer;
429427

430428
it('Errors during the build', async () => {
431429
try {
@@ -434,7 +432,10 @@ describe('Server islands', () => {
434432
});
435433
assert.equal(true, false, 'should not have succeeded');
436434
} catch (err) {
437-
assert.equal(err.title, 'Cannot use Server Islands without an adapter.');
435+
assert.equal(
436+
(err as { title: string }).title,
437+
'Cannot use Server Islands without an adapter.',
438+
);
438439
}
439440
});
440441

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import assert from 'node:assert/strict';
22
import { before, describe, it } from 'node:test';
33
import * as cheerio from 'cheerio';
4-
import { loadFixture } from './test-utils.js';
4+
import { type Fixture, loadFixture } from './test-utils.js';
55

66
describe('Slots: Preact', () => {
7-
let fixture;
7+
let fixture: Fixture;
88

99
before(async () => {
1010
fixture = await loadFixture({ root: './fixtures/slots-preact/' });
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import assert from 'node:assert/strict';
22
import { before, describe, it } from 'node:test';
33
import * as cheerio from 'cheerio';
4-
import { loadFixture } from './test-utils.js';
4+
import { type Fixture, loadFixture } from './test-utils.js';
55

66
describe('Slots: React', () => {
7-
let fixture;
7+
let fixture: Fixture;
88

99
before(async () => {
1010
fixture = await loadFixture({ root: './fixtures/slots-react/' });
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import assert from 'node:assert/strict';
22
import { before, describe, it } from 'node:test';
33
import * as cheerio from 'cheerio';
4-
import { loadFixture } from './test-utils.js';
4+
import { type Fixture, loadFixture } from './test-utils.js';
55

66
describe('Slots: Solid', () => {
7-
let fixture;
7+
let fixture: Fixture;
88

99
before(async () => {
1010
fixture = await loadFixture({ root: './fixtures/slots-solid/' });
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import assert from 'node:assert/strict';
22
import { before, describe, it } from 'node:test';
33
import * as cheerio from 'cheerio';
4-
import { loadFixture } from './test-utils.js';
4+
import { type Fixture, loadFixture } from './test-utils.js';
55

66
describe('Slots: Svelte', () => {
7-
let fixture;
7+
let fixture: Fixture;
88

99
before(async () => {
1010
fixture = await loadFixture({ root: './fixtures/slots-svelte/' });
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import assert from 'node:assert/strict';
22
import { before, describe, it } from 'node:test';
33
import * as cheerio from 'cheerio';
4-
import { loadFixture } from './test-utils.js';
4+
import { type Fixture, loadFixture } from './test-utils.js';
55

66
describe('Slots: Vue', () => {
7-
let fixture;
7+
let fixture: Fixture;
88

99
before(async () => {
1010
fixture = await loadFixture({ root: './fixtures/slots-vue/' });
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import assert from 'node:assert/strict';
22
import { before, describe, it } from 'node:test';
3-
import { loadFixture } from './test-utils.js';
3+
import { type Fixture, loadFixture } from './test-utils.js';
44

55
describe('Sourcemap', async () => {
6-
let fixture;
6+
let fixture: Fixture;
77

88
before(async () => {
99
fixture = await loadFixture({ root: './fixtures/sourcemap/' });

0 commit comments

Comments
 (0)