Skip to content

Commit 44fd3b8

Browse files
authored
Port 8 unit test files from JavaScript to TypeScript (#16249)
Part of the test-to-TypeScript migration (#16241). Ported files: - app/dev-url-construction.test - app/headers.test - app/url-attribute-xss.test - assets/image-layout.test - render/escape.test - render/hydration.test - routing/origin-pathname.test - routing/routing-helpers.test Removed @ts-check directives (redundant in .ts files), converted JSDoc type annotations to native TypeScript where needed, and added minimal type assertions for test mocks. typecheck:tests and both test:unit:ts / test:unit:js pass.
1 parent c2a52d6 commit 44fd3b8

8 files changed

Lines changed: 32 additions & 31 deletions

File tree

packages/astro/test/units/app/dev-url-construction.test.js renamed to packages/astro/test/units/app/dev-url-construction.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import * as assert from 'node:assert/strict';
22
import { describe, it } from 'node:test';
3+
import type { SSRManifest } from '../../../dist/core/app/types.js';
34
import {
45
getFirstForwardedValue,
56
validateForwardedHeaders,
67
} from '../../../dist/core/app/validate-headers.js';
78

8-
/**
9-
* Mirrors the URL construction logic in AstroServerApp.handleRequest so that
10-
* the protocol and host derivation can be exercised in isolation.
11-
*
12-
* @param {object} opts
13-
* @param {Record<string, string>} opts.headers - Incoming request headers
14-
* @param {boolean} [opts.isHttps=false] - Whether Vite itself is running TLS
15-
* @param {import('../../../dist/core/app/types.js').SSRManifest['allowedDomains']} [opts.allowedDomains]
16-
* @param {string} [opts.requestUrl='/']
17-
* @returns {URL}
18-
*/
19-
function buildDevUrl({ headers, isHttps = false, allowedDomains, requestUrl = '/' }) {
9+
function buildDevUrl({
10+
headers,
11+
isHttps = false,
12+
allowedDomains,
13+
requestUrl = '/',
14+
}: {
15+
headers: Record<string, string>;
16+
isHttps?: boolean;
17+
allowedDomains?: SSRManifest['allowedDomains'];
18+
requestUrl?: string;
19+
}): URL {
2020
const validated = validateForwardedHeaders(
2121
getFirstForwardedValue(headers['x-forwarded-proto']),
2222
getFirstForwardedValue(headers['x-forwarded-host']),
File renamed without changes.

packages/astro/test/units/app/url-attribute-xss.test.js renamed to packages/astro/test/units/app/url-attribute-xss.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-check
21
import assert from 'node:assert/strict';
32
import { describe, it } from 'node:test';
43
import { addAttribute } from '../../../dist/runtime/server/render/util.js';
File renamed without changes.

packages/astro/test/units/render/escape.test.js renamed to packages/astro/test/units/render/escape.test.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-check
21
import assert from 'node:assert/strict';
32
import { describe, it } from 'node:test';
43
import {
@@ -86,8 +85,8 @@ describe('unescapeHTML', () => {
8685
yield '<li>1</li>';
8786
yield '<li>2</li>';
8887
}
89-
const result = unescapeHTML(gen());
90-
const chunks = [];
88+
const result = unescapeHTML(gen()) as AsyncIterable<unknown>;
89+
const chunks: string[] = [];
9190
for await (const chunk of result) {
9291
chunks.push(String(chunk));
9392
}
@@ -100,8 +99,8 @@ describe('unescapeHTML', () => {
10099
yield '<li>a</li>';
101100
yield '<li>b</li>';
102101
}
103-
const result = unescapeHTML(gen());
104-
const chunks = [];
102+
const result = unescapeHTML(gen()) as AsyncIterable<unknown>;
103+
const chunks: string[] = [];
105104
for await (const chunk of result) {
106105
chunks.push(String(chunk));
107106
}
@@ -110,8 +109,8 @@ describe('unescapeHTML', () => {
110109

111110
it('can take a Response', async () => {
112111
const response = new Response('<p>hello</p>', { headers: { 'content-type': 'text/html' } });
113-
const result = unescapeHTML(response);
114-
const chunks = [];
112+
const result = unescapeHTML(response) as AsyncIterable<unknown>;
113+
const chunks: string[] = [];
115114
const dec = new TextDecoder();
116115
for await (const chunk of result) {
117116
chunks.push(chunk instanceof Uint8Array ? dec.decode(chunk) : String(chunk));
@@ -126,8 +125,8 @@ describe('unescapeHTML', () => {
126125
controller.close();
127126
},
128127
});
129-
const result = unescapeHTML(stream);
130-
const chunks = [];
128+
const result = unescapeHTML(stream) as AsyncIterable<unknown>;
129+
const chunks: string[] = [];
131130
for await (const chunk of result) {
132131
chunks.push(String(chunk));
133132
}

packages/astro/test/units/render/hydration.test.js renamed to packages/astro/test/units/render/hydration.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-check
21
import assert from 'node:assert/strict';
32
import { describe, it } from 'node:test';
43
import { extractDirectives } from '../../../dist/runtime/server/hydration.js';
@@ -138,9 +137,9 @@ describe('extractDirectives', () => {
138137
it('throws for an invalid hydration directive', () => {
139138
assert.throws(
140139
() => extractDirectives({ 'client:unknown': '' }, clientDirectives),
141-
(err) => {
142-
assert.ok(err.message.includes('invalid hydration directive'));
143-
assert.ok(err.message.includes('client:unknown'));
140+
(err: unknown) => {
141+
assert.ok((err as Error).message.includes('invalid hydration directive'));
142+
assert.ok((err as Error).message.includes('client:unknown'));
144143
return true;
145144
},
146145
);

packages/astro/test/units/routing/origin-pathname.test.js renamed to packages/astro/test/units/routing/origin-pathname.test.ts

File renamed without changes.

packages/astro/test/units/routing/routing-helpers.test.js renamed to packages/astro/test/units/routing/routing-helpers.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
// @ts-check
21
import assert from 'node:assert/strict';
32
import { describe, it } from 'node:test';
3+
import type { RouteData } from '../../../dist/types/public/internal.js';
44
import { hasNonPrerenderedRoute } from '../../../dist/core/routing/helpers.js';
55

6+
function route(overrides: Partial<RouteData>): RouteData {
7+
return overrides as RouteData;
8+
}
9+
610
describe('hasNonPrerenderedRoute', () => {
711
it('returns true when a non-prerendered project page exists', () => {
8-
const routes = [{ type: 'page', origin: 'project', prerender: false }];
12+
const routes = [route({ type: 'page', origin: 'project', prerender: false })];
913
assert.equal(hasNonPrerenderedRoute(routes), true);
1014
});
1115

1216
it('returns false when all project pages are prerendered', () => {
13-
const routes = [{ type: 'page', origin: 'project', prerender: true }];
17+
const routes = [route({ type: 'page', origin: 'project', prerender: true })];
1418
assert.equal(hasNonPrerenderedRoute(routes), false);
1519
});
1620

1721
it('excludes endpoints when includeEndpoints is false', () => {
18-
const routes = [{ type: 'endpoint', origin: 'project', prerender: false }];
22+
const routes = [route({ type: 'endpoint', origin: 'project', prerender: false })];
1923
assert.equal(hasNonPrerenderedRoute(routes, { includeEndpoints: false }), false);
2024
assert.equal(hasNonPrerenderedRoute(routes, { includeEndpoints: true }), true);
2125
});
2226

2327
it('returns true for injected (external) non-prerendered pages when includeExternal is true', () => {
24-
const routes = [{ type: 'page', origin: 'external', prerender: false }];
28+
const routes = [route({ type: 'page', origin: 'external', prerender: false })];
2529
assert.equal(hasNonPrerenderedRoute(routes, { includeExternal: true }), true);
2630
assert.equal(hasNonPrerenderedRoute(routes), false);
2731
});

0 commit comments

Comments
 (0)