Skip to content

Commit ff74d62

Browse files
committed
refactor(astro): migrate 39 tests to typescript
1 parent cf074b3 commit ff74d62

41 files changed

Lines changed: 308 additions & 338 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/astro/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
"@types/html-escaper": "3.0.4",
188188
"@types/http-cache-semantics": "^4.2.0",
189189
"@types/js-yaml": "^4.0.9",
190+
"@types/parse-srcset": "^1.0.0",
190191
"@types/picomatch": "^4.0.2",
191192
"@types/semver": "^7.7.1",
192193
"@types/yargs-parser": "^21.0.3",

packages/astro/test/astro-assets-dir.test.js renamed to packages/astro/test/astro-assets-dir.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { before, describe, it } from 'node:test';
33
import { loadFixture } from './test-utils.js';
44

55
describe('assets dir takes the URL path inside the output directory', () => {
6-
/** @type {URL} */
7-
let checkDir;
6+
let checkDir: URL;
87
before(async () => {
98
const fixture = await loadFixture({
109
root: './fixtures/astro-assets-dir/',
@@ -25,7 +24,7 @@ describe('assets dir takes the URL path inside the output directory', () => {
2524
await fixture.build();
2625
});
2726
it('generates the assets directory as per build.assets configuration', async () => {
28-
const removeTrailingSlash = (str) => str.replace(/\/$/, '');
27+
const removeTrailingSlash = (str: string) => str.replace(/\/$/, '');
2928
assert.equal(
3029
removeTrailingSlash(new URL('./custom_dir_1', checkDir).toString()),
3130
removeTrailingSlash(

packages/astro/test/astro-assets-prefix-multi-cdn.test.js renamed to packages/astro/test/astro-assets-prefix-multi-cdn.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
22
import { after, before, describe, it } from 'node:test';
33
import * as cheerio from 'cheerio';
44
import testAdapter from './test-adapter.js';
5-
import { loadFixture } from './test-utils.js';
5+
import { type App, type Fixture, loadFixture } from './test-utils.js';
66

77
const defaultAssetsPrefixRegex = /^https:\/\/example.com\/_astro\/.*/;
88
const jsAssetsPrefixRegex = /^https:\/\/js\.example\.com\/_astro\/.*/;
@@ -15,7 +15,7 @@ const assetsPrefix = {
1515

1616
// Asset prefix for CDN support
1717
describe('Assets Prefix Multiple CDN - Static', () => {
18-
let fixture;
18+
let fixture: Fixture;
1919

2020
before(async () => {
2121
fixture = await loadFixture({
@@ -44,15 +44,15 @@ describe('Assets Prefix Multiple CDN - Static', () => {
4444
const html = await fixture.readFile('/index.html');
4545
const $ = cheerio.load(html);
4646
const imgAsset = $('#image-asset');
47-
assert.match(imgAsset.attr('src'), defaultAssetsPrefixRegex);
47+
assert.match(imgAsset.attr('src')!, defaultAssetsPrefixRegex);
4848
});
4949

5050
it('react component astro-island should import from jsAssetsPrefix', async () => {
5151
const html = await fixture.readFile('/index.html');
5252
const $ = cheerio.load(html);
5353
const island = $('astro-island');
54-
assert.match(island.attr('component-url'), jsAssetsPrefixRegex);
55-
assert.match(island.attr('renderer-url'), jsAssetsPrefixRegex);
54+
assert.match(island.attr('component-url')!, jsAssetsPrefixRegex);
55+
assert.match(island.attr('renderer-url')!, jsAssetsPrefixRegex);
5656
});
5757

5858
it('import.meta.env.ASSETS_PREFIX works', async () => {
@@ -75,13 +75,13 @@ describe('Assets Prefix Multiple CDN - Static', () => {
7575
const html = await fixture.readFile('/blog/index.html');
7676
const $ = cheerio.load(html);
7777
const imgAsset = $('img');
78-
assert.match(imgAsset.attr('src'), defaultAssetsPrefixRegex);
78+
assert.match(imgAsset.attr('src')!, defaultAssetsPrefixRegex);
7979
});
8080
});
8181

8282
describe('Assets Prefix Multiple CDN, server', () => {
83-
let app;
84-
let fixture;
83+
let app: App;
84+
let fixture: Fixture;
8585
before(async () => {
8686
fixture = await loadFixture({
8787
root: './fixtures/astro-assets-prefix',
@@ -114,7 +114,7 @@ describe('Assets Prefix Multiple CDN, server', () => {
114114
const html = await response.text();
115115
const $ = cheerio.load(html);
116116
const imgAsset = $('#image-asset');
117-
assert.match(imgAsset.attr('src'), defaultAssetsPrefixRegex);
117+
assert.match(imgAsset.attr('src')!, defaultAssetsPrefixRegex);
118118
});
119119

120120
it('react component astro-island should import from assetsPrefix', async () => {
@@ -124,8 +124,8 @@ describe('Assets Prefix Multiple CDN, server', () => {
124124
const html = await response.text();
125125
const $ = cheerio.load(html);
126126
const island = $('astro-island');
127-
assert.match(island.attr('component-url'), jsAssetsPrefixRegex);
128-
assert.match(island.attr('renderer-url'), jsAssetsPrefixRegex);
127+
assert.match(island.attr('component-url')!, jsAssetsPrefixRegex);
128+
assert.match(island.attr('renderer-url')!, jsAssetsPrefixRegex);
129129
});
130130

131131
it('markdown optimized image src does not start with assetsPrefix in SSR', async () => {
@@ -135,6 +135,6 @@ describe('Assets Prefix Multiple CDN, server', () => {
135135
const html = await response.text();
136136
const $ = cheerio.load(html);
137137
const imgAsset = $('img');
138-
assert.doesNotMatch(imgAsset.attr('src'), defaultAssetsPrefixRegex);
138+
assert.doesNotMatch(imgAsset.attr('src')!, defaultAssetsPrefixRegex);
139139
});
140140
});

packages/astro/test/astro-assets-prefix.test.js renamed to packages/astro/test/astro-assets-prefix.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import assert from 'node:assert/strict';
22
import { after, before, describe, it } from 'node:test';
33
import * as cheerio from 'cheerio';
44
import testAdapter from './test-adapter.js';
5-
import { loadFixture } from './test-utils.js';
5+
import { type App, type Fixture, loadFixture } from './test-utils.js';
66

77
const assetsPrefix = 'http://localhost:4321';
88
const assetsPrefixRegex = /^http:\/\/localhost:4321\/_astro\/.*/;
99

1010
// Asset prefix for CDN support
1111
describe('Assets Prefix - Static', () => {
12-
let fixture;
12+
let fixture: Fixture;
1313

1414
before(async () => {
1515
fixture = await loadFixture({
@@ -36,15 +36,15 @@ describe('Assets Prefix - Static', () => {
3636
const html = await fixture.readFile('/index.html');
3737
const $ = cheerio.load(html);
3838
const imgAsset = $('#image-asset');
39-
assert.match(imgAsset.attr('src'), assetsPrefixRegex);
39+
assert.match(imgAsset.attr('src')!, assetsPrefixRegex);
4040
});
4141

4242
it('react component astro-island should import from assetsPrefix', async () => {
4343
const html = await fixture.readFile('/index.html');
4444
const $ = cheerio.load(html);
4545
const island = $('astro-island');
46-
assert.match(island.attr('component-url'), assetsPrefixRegex);
47-
assert.match(island.attr('renderer-url'), assetsPrefixRegex);
46+
assert.match(island.attr('component-url')!, assetsPrefixRegex);
47+
assert.match(island.attr('renderer-url')!, assetsPrefixRegex);
4848
});
4949

5050
it('import.meta.env.ASSETS_PREFIX works', async () => {
@@ -67,7 +67,7 @@ describe('Assets Prefix - Static', () => {
6767
const html = await fixture.readFile('/blog/index.html');
6868
const $ = cheerio.load(html);
6969
const imgAsset = $('img');
70-
assert.match(imgAsset.attr('src'), assetsPrefixRegex);
70+
assert.match(imgAsset.attr('src')!, assetsPrefixRegex);
7171
});
7272

7373
it('MDX content collection CSS imports should start with assetsPrefix', async () => {
@@ -82,7 +82,7 @@ describe('Assets Prefix - Static', () => {
8282
});
8383

8484
describe('Assets Prefix - with path prefix', () => {
85-
let fixture;
85+
let fixture: Fixture;
8686

8787
before(async () => {
8888
fixture = await loadFixture({
@@ -106,7 +106,7 @@ describe('Assets Prefix - with path prefix', () => {
106106
});
107107

108108
describe('Assets Prefix, server', () => {
109-
let app;
109+
let app: App;
110110

111111
before(async () => {
112112
const fixture = await loadFixture({
@@ -138,7 +138,7 @@ describe('Assets Prefix, server', () => {
138138
const html = await response.text();
139139
const $ = cheerio.load(html);
140140
const imgAsset = $('#image-asset');
141-
assert.match(imgAsset.attr('src'), assetsPrefixRegex);
141+
assert.match(imgAsset.attr('src')!, assetsPrefixRegex);
142142
});
143143

144144
it('react component astro-island should import from assetsPrefix', async () => {
@@ -148,8 +148,8 @@ describe('Assets Prefix, server', () => {
148148
const html = await response.text();
149149
const $ = cheerio.load(html);
150150
const island = $('astro-island');
151-
assert.match(island.attr('component-url'), assetsPrefixRegex);
152-
assert.match(island.attr('renderer-url'), assetsPrefixRegex);
151+
assert.match(island.attr('component-url')!, assetsPrefixRegex);
152+
assert.match(island.attr('renderer-url')!, assetsPrefixRegex);
153153
});
154154

155155
it('markdown optimized image src does not start with assetsPrefix in SSR', async () => {
@@ -159,12 +159,12 @@ describe('Assets Prefix, server', () => {
159159
const html = await response.text();
160160
const $ = cheerio.load(html);
161161
const imgAsset = $('img');
162-
assert.doesNotMatch(imgAsset.attr('src'), assetsPrefixRegex);
162+
assert.doesNotMatch(imgAsset.attr('src')!, assetsPrefixRegex);
163163
});
164164
});
165165

166166
describe('Assets Prefix, with path prefix', () => {
167-
let app;
167+
let app: App;
168168

169169
before(async () => {
170170
const fixture = await loadFixture({
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import assert from 'node:assert/strict';
22
import { before, describe, it } from 'node:test';
33
import * as cheerio from 'cheerio';
44
import parseSrcset from 'parse-srcset';
5-
import { loadFixture } from './test-utils.js';
5+
import { type Fixture, loadFixture } from './test-utils.js';
66

77
// Asset bundling
88
describe('Assets', () => {
9-
let fixture;
9+
let fixture: Fixture;
1010

1111
before(async () => {
1212
fixture = await loadFixture({
@@ -18,43 +18,43 @@ describe('Assets', () => {
1818
it('built the base image', async () => {
1919
const html = await fixture.readFile('/index.html');
2020
const $ = cheerio.load(html);
21-
const imgPath = $('img').attr('src');
21+
const imgPath = $('img').attr('src')!;
2222
const data = await fixture.readFile(imgPath);
2323
assert.equal(!!data, true);
2424
});
2525

2626
it('built the 2x image', async () => {
2727
const html = await fixture.readFile('/index.html');
2828
const $ = cheerio.load(html);
29-
const srcset = $('img').attr('srcset');
29+
const srcset = $('img').attr('srcset')!;
3030
const candidates = parseSrcset(srcset);
31-
const match = candidates.find((a) => a.d === 2);
31+
const match = candidates.find((a) => a.d === 2)!;
3232
const data = await fixture.readFile(match.url);
3333
assert.equal(!!data, true);
3434
});
3535

3636
it('built the 3x image', async () => {
3737
const html = await fixture.readFile('/index.html');
3838
const $ = cheerio.load(html);
39-
const srcset = $('img').attr('srcset');
39+
const srcset = $('img').attr('srcset')!;
4040
const candidates = parseSrcset(srcset);
41-
const match = candidates.find((a) => a.d === 3);
41+
const match = candidates.find((a) => a.d === 3)!;
4242
const data = await fixture.readFile(match.url);
4343
assert.equal(!!data, true);
4444
});
4545

4646
it('built image from an import specifier', async () => {
4747
const html = await fixture.readFile('/index.html');
4848
const $ = cheerio.load(html);
49-
const src = $('#import-no-url').attr('src');
49+
const src = $('#import-no-url').attr('src')!;
5050
const data = await fixture.readFile(src);
5151
assert.equal(!!data, true);
5252
});
5353

5454
it('built image from an import specifier using ?url', async () => {
5555
const html = await fixture.readFile('/index.html');
5656
const $ = cheerio.load(html);
57-
const src = $('#import-url').attr('src');
57+
const src = $('#import-url').attr('src')!;
5858
const data = await fixture.readFile(src);
5959
assert.equal(!!data, true);
6060
});
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import assert from 'node:assert/strict';
22
import { after, before, describe, it } from 'node:test';
33
import * as cheerio from 'cheerio';
4-
import { loadFixture } from './test-utils.js';
54
import createTestPrerenderer from './test-prerenderer.js';
5+
import { type DevServer, type Fixture, loadFixture, type PreviewServer } from './test-utils.js';
66

77
describe('Astro basic build', () => {
8-
/** @type {import('./test-utils').Fixture} */
9-
let fixture;
8+
let fixture: Fixture;
109

11-
let previewServer;
10+
let previewServer: PreviewServer;
1211

1312
before(async () => {
1413
fixture = await loadFixture({
@@ -28,13 +27,13 @@ describe('Astro basic build', () => {
2827
const $ = cheerio.load(html);
2928

3029
assert.equal($('#spread-plain').length, 1);
31-
assert.match($('#spread-plain').attr('class'), /astro-.*/);
30+
assert.match($('#spread-plain').attr('class')!, /astro-.*/);
3231

3332
assert.equal($('#spread-class').length, 1);
34-
assert.match($('#spread-class').attr('class'), /astro-.*/);
33+
assert.match($('#spread-class').attr('class')!, /astro-.*/);
3534

3635
assert.equal($('#spread-class-list').length, 1);
37-
assert.match($('#spread-class-list').attr('class'), /astro-.*/);
36+
assert.match($('#spread-class-list').attr('class')!, /astro-.*/);
3837
});
3938

4039
it('supports special chars in filename', async () => {
@@ -86,7 +85,7 @@ describe('Astro basic build', () => {
8685
it('Defines Astro.generator', async () => {
8786
const html = await fixture.readFile('/generator/index.html');
8887
const $ = cheerio.load(html);
89-
assert.match($('meta[name="generator"]').attr('content'), /^Astro v/);
88+
assert.match($('meta[name="generator"]').attr('content')!, /^Astro v/);
9089
});
9190

9291
describe('preview', () => {
@@ -103,10 +102,8 @@ describe('Astro basic build', () => {
103102
});
104103

105104
describe('Astro basic development', () => {
106-
/** @type {import('./test-utils').DevServer} */
107-
let devServer;
108-
/** @type {import('./test-utils').Fixture} */
109-
let fixture;
105+
let devServer: DevServer;
106+
let fixture: Fixture;
110107

111108
before(async () => {
112109
fixture = await loadFixture({
@@ -124,7 +121,7 @@ describe('Astro basic development', () => {
124121
const html = await res.text();
125122
const $ = cheerio.load(html);
126123
assert.equal($('h1').text(), '我的第一篇博客文章');
127-
assert.doesNotMatch(res.headers.get('content-type'), /charset=utf-8/);
124+
assert.doesNotMatch(res.headers.get('content-type')!, /charset=utf-8/);
128125
assert.match(html, /<meta charset="utf-8"/);
129126
});
130127

@@ -145,9 +142,8 @@ describe('Astro basic development', () => {
145142
});
146143

147144
describe('Astro custom prerenderer', () => {
148-
/** @type {import('./test-utils').Fixture} */
149-
let fixture;
150-
let testPrerenderer;
145+
let fixture: Fixture;
146+
let testPrerenderer: ReturnType<typeof createTestPrerenderer>;
151147

152148
before(async () => {
153149
testPrerenderer = createTestPrerenderer();

packages/astro/test/astro-children.test.js renamed to packages/astro/test/astro-children.test.ts

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('Component children', () => {
7-
let fixture;
7+
let fixture: Fixture;
88

99
before(async () => {
1010
fixture = await loadFixture({ root: './fixtures/astro-children/' });

0 commit comments

Comments
 (0)