Skip to content

Commit d198e82

Browse files
authored
test: port 16 routing unit tests to TypeScript (#16266)
1 parent 673a871 commit d198e82

17 files changed

Lines changed: 94 additions & 62 deletions

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe('createAPIContext', () => {
88

99
const context = createContext({
1010
request,
11+
defaultLocale: '',
1112
clientAddress: '192.0.2.43',
1213
});
1314

@@ -19,11 +20,12 @@ describe('createAPIContext', () => {
1920

2021
const context = createContext({
2122
request,
23+
defaultLocale: '',
2224
});
2325

2426
assert.throws(
2527
() => context.clientAddress,
26-
(err) => {
28+
(err: Error) => {
2729
assert.equal(err.name, 'StaticClientAddressNotAvailable');
2830
return true;
2931
},
@@ -39,12 +41,13 @@ describe('createAPIContext', () => {
3941

4042
const context = createContext({
4143
request,
44+
defaultLocale: '',
4245
});
4346

4447
// Should throw instead of reading from the header
4548
assert.throws(
4649
() => context.clientAddress,
47-
(err) => {
50+
(err: Error) => {
4851
assert.equal(err.name, 'StaticClientAddressNotAvailable');
4952
return true;
5053
},
@@ -56,6 +59,7 @@ describe('createAPIContext', () => {
5659

5760
const context = createContext({
5861
request,
62+
defaultLocale: '',
5963
locals: {
6064
foo: 'bar',
6165
},
File renamed without changes.

packages/astro/test/units/routing/dynamic-route-collision.test.js renamed to packages/astro/test/units/routing/dynamic-route-collision.test.ts

File renamed without changes.

packages/astro/test/units/routing/generator.test.js renamed to packages/astro/test/units/routing/generator.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
import * as assert from 'node:assert/strict';
1+
import assert from 'node:assert/strict';
22
import { describe, it } from 'node:test';
3-
3+
import type { AstroConfig } from '../../../dist/types/public/config.js';
4+
import type { RoutePart } from '../../../dist/types/public/internal.js';
45
import { getRouteGenerator } from '../../../dist/core/routing/generator.js';
56

7+
interface TestCase {
8+
routeData: RoutePart[][];
9+
trailingSlash: AstroConfig['trailingSlash'];
10+
params: Record<string, string | number>;
11+
path: string;
12+
}
13+
614
describe('routing - generator', () => {
7-
[
15+
const cases: TestCase[] = [
816
{
917
routeData: [],
1018
trailingSlash: 'never',
@@ -135,7 +143,9 @@ describe('routing - generator', () => {
135143
params: { page: 1 },
136144
path: '/fix/1',
137145
},
138-
].forEach(({ routeData, trailingSlash, params, path }) => {
146+
];
147+
148+
cases.forEach(({ routeData, trailingSlash, params, path }) => {
139149
it(`generates ${path}`, () => {
140150
const generator = getRouteGenerator(routeData, trailingSlash);
141151
assert.equal(generator(params), path);

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import assert from 'node:assert/strict';
22
import { describe, it, before, beforeEach } from 'node:test';
3+
import type { ComponentInstance } from '../../../dist/types/astro.js';
4+
import type { LogMessage, LogWritable } from '../../../dist/core/logger/core.js';
35
import { Logger } from '../../../dist/core/logger/core.js';
46
import { RouteCache, callGetStaticPaths } from '../../../dist/core/render/route-cache.js';
57
import { dynamicPart, makeRoute } from './test-helpers.js';
68

9+
function mod(overrides: Partial<ComponentInstance>): ComponentInstance {
10+
return overrides as ComponentInstance;
11+
}
12+
713
describe('getStaticPaths caching behavior', () => {
8-
let routeCache;
9-
let logger;
10-
let callCount;
14+
let routeCache: RouteCache;
15+
let logger: Logger;
16+
let callCount: number;
17+
18+
const dest: LogWritable<LogMessage> = {
19+
write: () => true,
20+
};
1121

1222
before(() => {
13-
logger = new Logger({ dest: 'memory', level: 'error' });
23+
logger = new Logger({ dest, level: 'error' });
1424
});
1525

1626
beforeEach(() => {
@@ -28,17 +38,16 @@ describe('getStaticPaths caching behavior', () => {
2838
prerender: true,
2939
});
3040

31-
const mod = {
32-
default: () => {},
41+
const testMod = mod({
3342
getStaticPaths: async () => {
3443
callCount++;
3544
return [{ params: { param: 'a' } }, { params: { param: 'b' } }, { params: { param: 'c' } }];
3645
},
37-
};
46+
});
3847

3948
// First call should execute getStaticPaths
4049
const result1 = await callGetStaticPaths({
41-
mod,
50+
mod: testMod,
4251
route,
4352
routeCache,
4453
ssr: false,
@@ -51,7 +60,7 @@ describe('getStaticPaths caching behavior', () => {
5160

5261
// Second call should use cache
5362
const result2 = await callGetStaticPaths({
54-
mod,
63+
mod: testMod,
5564
route,
5665
routeCache,
5766
ssr: false,
@@ -65,7 +74,7 @@ describe('getStaticPaths caching behavior', () => {
6574

6675
// Third call should also use cache
6776
const result3 = await callGetStaticPaths({
68-
mod,
77+
mod: testMod,
6978
route,
7079
routeCache,
7180
ssr: false,
@@ -87,17 +96,16 @@ describe('getStaticPaths caching behavior', () => {
8796
prerender: true,
8897
});
8998

90-
const mod = {
91-
default: () => {},
99+
const testMod = mod({
92100
getStaticPaths: async () => {
93101
callCount++;
94102
return [{ params: { test: 'value' } }];
95103
},
96-
};
104+
});
97105

98106
// First call
99107
await callGetStaticPaths({
100-
mod,
108+
mod: testMod,
101109
route,
102110
routeCache,
103111
ssr: false,
@@ -112,7 +120,7 @@ describe('getStaticPaths caching behavior', () => {
112120

113121
// Second call after clearing should call getStaticPaths again
114122
await callGetStaticPaths({
115-
mod,
123+
mod: testMod,
116124
route,
117125
routeCache,
118126
ssr: false,

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import assert from 'node:assert/strict';
22
import { describe, it, before } from 'node:test';
3+
import type { ComponentInstance } from '../../../dist/types/astro.js';
4+
import type { LogMessage, LogWritable } from '../../../dist/core/logger/core.js';
35
import { Logger } from '../../../dist/core/logger/core.js';
46
import { RouteCache, callGetStaticPaths } from '../../../dist/core/render/route-cache.js';
57
import { makeRoute } from './test-helpers.js';
68

9+
function mod(overrides: Partial<ComponentInstance>): ComponentInstance {
10+
return overrides as ComponentInstance;
11+
}
12+
713
describe('getStaticPaths param validation', () => {
8-
let routeCache;
9-
let logger;
14+
let routeCache: RouteCache;
15+
let logger: Logger;
16+
17+
const dest: LogWritable<LogMessage> = {
18+
write: () => true,
19+
};
1020

1121
before(() => {
12-
logger = new Logger({ dest: 'memory', level: 'error' });
22+
logger = new Logger({ dest, level: 'error' });
1323
routeCache = new RouteCache(logger, 'production');
1424
});
1525

@@ -43,18 +53,18 @@ describe('getStaticPaths param validation', () => {
4353
// Clear route cache before each test to ensure isolation
4454
routeCache.clearAll();
4555

46-
const mod = {
47-
default: () => {},
56+
const testMod = mod({
4857
getStaticPaths: async () => [
4958
{
50-
params: { testParam: value },
59+
// Intentionally passing invalid param types to test validation
60+
params: { testParam: value as string | undefined },
5161
},
5262
],
53-
};
63+
});
5464

5565
try {
5666
await callGetStaticPaths({
57-
mod,
67+
mod: testMod,
5868
route,
5969
routeCache,
6070
ssr: false,
@@ -65,7 +75,7 @@ describe('getStaticPaths param validation', () => {
6575
if (!shouldPass) {
6676
assert.fail(`Expected validation error for param type ${type}`);
6777
}
68-
} catch (err) {
78+
} catch (err: any) {
6979
if (shouldPass) {
7080
throw err;
7181
}
File renamed without changes.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import assert from 'node:assert/strict';
22
import { describe, it } from 'node:test';
3+
import type { AstroConfig } from '../../../dist/types/public/config.js';
34
import { Router } from '../../../dist/core/routing/router.js';
45
import { dynamicPart, makeRoute, staticPart } from './test-helpers.js';
56

67
describe('preview routing (unit)', () => {
7-
const routes = (trailingSlash) => [
8+
const routes = (trailingSlash: AstroConfig['trailingSlash']) => [
89
makeRoute({ segments: [], trailingSlash, route: '/', pathname: '/', isIndex: true }),
910
makeRoute({
1011
segments: [[staticPart('another')]],

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as assert from 'node:assert/strict';
1+
import assert from 'node:assert/strict';
22
import { describe, it } from 'node:test';
33
import { Router } from '../../../dist/core/routing/router.js';
44
import { dynamicPart, makeRoute, staticPart } from './test-helpers.js';

packages/astro/test/units/routing/rewrite.test.js renamed to packages/astro/test/units/routing/rewrite.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ describe('setOriginPathname / getOriginPathname', () => {
181181

182182
it('handles undefined pathname', () => {
183183
const req = new Request('http://example.com/');
184+
// @ts-expect-error Testing runtime behavior with undefined pathname
184185
setOriginPathname(req, undefined, 'ignore', 'directory');
185186
assert.equal(getOriginPathname(req), '/');
186187
});

0 commit comments

Comments
 (0)