Skip to content

Commit 7bdcfb7

Browse files
mingjunluematipico
andauthored
chore: migrate several tests to node:test (#10133)
* chore: migrate test files whose names start with `a` to `node:test` * update assertion * chore: remove mocha script --------- Co-authored-by: Emanuele Stoppa <[email protected]>
1 parent 4b62bfb commit 7bdcfb7

22 files changed

+358
-300
lines changed

packages/astro/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
"build:ci": "pnpm run prebuild && astro-scripts build \"src/**/*.{ts,js}\" && pnpm run postbuild",
108108
"dev": "astro-scripts dev --copy-wasm --prebuild \"src/runtime/server/astro-island.ts\" --prebuild \"src/runtime/client/{idle,load,media,only,visible}.ts\" \"src/**/*.{ts,js}\"",
109109
"postbuild": "astro-scripts copy \"src/**/*.astro\" && astro-scripts copy \"src/**/*.wasm\"",
110-
"test": "pnpm run test:node && mocha ./test/*.test.js --exit --timeout 30000",
110+
"test": "pnpm run test:node",
111111
"test:match": "mocha ./test/*.test.js --timeout 30000 -g",
112112
"test:e2e": "playwright test",
113113
"test:e2e:match": "playwright test -g",

packages/astro/test/api-routes.test.js renamed to packages/astro/test/api-routes.nodetest.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { expect } from 'chai';
1+
import assert from 'node:assert/strict';
2+
import { before, describe, it } from 'node:test';
23
import { loadFixture } from './test-utils.js';
34

45
describe('API routes', () => {
@@ -13,8 +14,8 @@ describe('API routes', () => {
1314
describe('Binary data', () => {
1415
it('can be returned from a response', async () => {
1516
const dat = await fixture.readFile('/binary.dat', null);
16-
expect(dat.length).to.equal(1);
17-
expect(dat[0]).to.equal(0xff);
17+
assert.equal(dat.length, 1);
18+
assert.equal(dat[0], 0xff);
1819
});
1920
});
2021
});

packages/astro/test/astro-client-only.test.js renamed to packages/astro/test/astro-client-only.nodetest.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { expect } from 'chai';
21
import { load as cheerioLoad } from 'cheerio';
2+
import assert from 'node:assert/strict';
3+
import { before, describe, it } from 'node:test';
34
import { loadFixture } from './test-utils.js';
45

56
describe('Client only components', () => {
@@ -20,10 +21,10 @@ describe('Client only components', () => {
2021
const $ = cheerioLoad(html);
2122

2223
// test 1: <astro-island> is empty
23-
expect($('astro-island').html()).to.equal('');
24+
assert.equal($('astro-island').html(), '');
2425

2526
// test 2: svelte renderer is on the page
26-
expect($('astro-island').attr('renderer-url')).to.be.ok;
27+
assert.ok($('astro-island').attr('renderer-url'));
2728
});
2829

2930
it('Adds the CSS to the page', async () => {
@@ -38,32 +39,32 @@ describe('Client only components', () => {
3839
const css = stylesheets.join('');
3940

4041
// yellowgreen minified
41-
expect(css).to.contain('#9acd32', 'Svelte styles are added');
42-
expect(css).to.include('Courier New', 'Global styles are added');
42+
assert.match(css, /#9acd32/, 'Svelte styles are added');
43+
assert.match(css, /Courier New/, 'Global styles are added');
4344
});
4445

4546
it('Adds the CSS to the page - standalone svelte component', async () => {
4647
const html = await fixture.readFile('/persistent-counter-standalone/index.html');
4748
const $ = cheerioLoad(html);
4849

49-
expect($('head link[rel=stylesheet]')).to.have.a.lengthOf(1);
50+
assert.equal($('head link[rel=stylesheet]').length, 1);
5051

5152
const href = $('link[rel=stylesheet]').attr('href');
5253
const css = await fixture.readFile(href);
5354

54-
expect(css).to.match(/tomato/, 'Svelte styles are added');
55+
assert.match(css, /tomato/, 'Svelte styles are added');
5556
});
5657

5758
it('Includes CSS from components that use CSS modules', async () => {
5859
const html = await fixture.readFile('/css-modules/index.html');
5960
const $ = cheerioLoad(html);
60-
expect($('link[rel=stylesheet]')).to.have.a.lengthOf(1);
61+
assert.equal($('link[rel=stylesheet]').length, 1);
6162
});
6263

6364
it('Includes CSS from package components', async () => {
6465
const html = await fixture.readFile('/pkg/index.html');
6566
const $ = cheerioLoad(html);
66-
expect($('link[rel=stylesheet]')).to.have.a.lengthOf(1);
67+
assert.equal($('link[rel=stylesheet]').length, 1);
6768
});
6869
});
6970

@@ -86,10 +87,10 @@ describe('Client only components subpath', () => {
8687
const $ = cheerioLoad(html);
8788

8889
// test 1: <astro-island> is empty
89-
expect($('astro-island').html()).to.equal('');
90+
assert.equal($('astro-island').html(), '');
9091

9192
// test 2: svelte renderer is on the page
92-
expect($('astro-island').attr('renderer-url')).to.be.ok;
93+
assert.ok($('astro-island').attr('renderer-url'));
9394
});
9495

9596
it('Adds the CSS to the page', async () => {
@@ -104,8 +105,8 @@ describe('Client only components subpath', () => {
104105
const css = stylesheets.join('');
105106

106107
// yellowgreen minified
107-
expect(css).to.contain('#9acd32', 'Svelte styles are added');
108-
expect(css).to.include('Courier New', 'Global styles are added');
108+
assert.match(css, /#9acd32/, 'Svelte styles are added');
109+
assert.match(css, /Courier New/, 'Global styles are added');
109110
});
110111

111112
it('Adds the CSS to the page for TSX components', async () => {
@@ -115,6 +116,6 @@ describe('Client only components subpath', () => {
115116
const href = $('link[rel=stylesheet]').attr('href');
116117
const css = await fixture.readFile(href.replace(/\/blog/, ''));
117118

118-
expect(css).to.match(/purple/, 'Global styles from tsx component are added');
119+
assert.match(css, /purple/, 'Global styles from tsx component are added');
119120
});
120121
});
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { expect } from 'chai';
21
import * as cheerio from 'cheerio';
2+
import assert from 'node:assert/strict';
3+
import { before, describe, it } from 'node:test';
34
import { loadFixture } from './test-utils.js';
45

56
describe('<Code>', () => {
@@ -13,33 +14,35 @@ describe('<Code>', () => {
1314
it('<Code> without lang or theme', async () => {
1415
let html = await fixture.readFile('/no-lang/index.html');
1516
const $ = cheerio.load(html);
16-
expect($('pre')).to.have.lengthOf(1);
17-
expect($('pre').attr('style')).to.equal(
17+
assert.equal($('pre').length, 1);
18+
assert.equal(
19+
$('pre').attr('style'),
1820
'background-color:#24292e;color:#e1e4e8; overflow-x: auto;',
1921
'applies default and overflow'
2022
);
21-
expect($('pre > code')).to.have.lengthOf(1);
23+
assert.equal($('pre > code').length, 1);
2224

2325
// test: contains some generated spans
24-
expect($('pre > code span').length).to.be.greaterThan(1);
26+
assert.equal($('pre > code span').length > 1, true);
2527
});
2628

2729
it('<Code lang="...">', async () => {
2830
let html = await fixture.readFile('/basic/index.html');
2931
const $ = cheerio.load(html);
30-
expect($('pre')).to.have.lengthOf(1);
31-
expect($('pre').attr('class'), 'astro-code nord');
32-
expect($('pre > code')).to.have.lengthOf(1);
32+
assert.equal($('pre').length, 1);
33+
assert.equal($('pre').attr('class'), 'astro-code github-dark');
34+
assert.equal($('pre > code').length, 1);
3335
// test: contains many generated spans
34-
expect($('pre > code span').length).to.be.greaterThanOrEqual(6);
36+
assert.equal($('pre > code span').length >= 6, true);
3537
});
3638

3739
it('<Code theme="...">', async () => {
3840
let html = await fixture.readFile('/custom-theme/index.html');
3941
const $ = cheerio.load(html);
40-
expect($('pre')).to.have.lengthOf(1);
41-
expect($('pre').attr('class')).to.equal('astro-code nord');
42-
expect($('pre').attr('style')).to.equal(
42+
assert.equal($('pre').length, 1);
43+
assert.equal($('pre').attr('class'), 'astro-code nord');
44+
assert.equal(
45+
$('pre').attr('style'),
4346
'background-color:#2e3440ff;color:#d8dee9ff; overflow-x: auto;',
4447
'applies custom theme'
4548
);
@@ -49,67 +52,73 @@ describe('<Code>', () => {
4952
{
5053
let html = await fixture.readFile('/wrap-true/index.html');
5154
const $ = cheerio.load(html);
52-
expect($('pre')).to.have.lengthOf(1);
55+
assert.equal($('pre').length, 1);
5356
// test: applies wrap overflow
54-
expect($('pre').attr('style')).to.equal(
57+
assert.equal(
58+
$('pre').attr('style'),
5559
'background-color:#24292e;color:#e1e4e8; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;'
5660
);
5761
}
5862
{
5963
let html = await fixture.readFile('/wrap-false/index.html');
6064
const $ = cheerio.load(html);
61-
expect($('pre')).to.have.lengthOf(1);
65+
assert.equal($('pre').length, 1);
6266
// test: applies wrap overflow
63-
expect($('pre').attr('style')).to.equal(
67+
assert.equal(
68+
$('pre').attr('style'),
6469
'background-color:#24292e;color:#e1e4e8; overflow-x: auto;'
6570
);
6671
}
6772
{
6873
let html = await fixture.readFile('/wrap-null/index.html');
6974
const $ = cheerio.load(html);
70-
expect($('pre')).to.have.lengthOf(1);
75+
assert.equal($('pre').length, 1);
7176
// test: applies wrap overflow
72-
expect($('pre').attr('style')).to.equal('background-color:#24292e;color:#e1e4e8');
77+
assert.equal($('pre').attr('style'), 'background-color:#24292e;color:#e1e4e8');
7378
}
7479
});
7580

7681
it('<Code lang="..." theme="css-variables">', async () => {
7782
let html = await fixture.readFile('/css-theme/index.html');
7883
const $ = cheerio.load(html);
79-
expect($('pre')).to.have.lengthOf(1);
80-
expect($('pre').attr('class')).to.equal('astro-code css-variables');
81-
expect(
84+
assert.equal($('pre').length, 1);
85+
assert.equal($('pre').attr('class'), 'astro-code css-variables');
86+
assert.deepEqual(
8287
$('pre, pre span')
8388
.map((i, f) => (f.attribs ? f.attribs.style : 'no style found'))
84-
.toArray()
85-
).to.deep.equal([
86-
'background-color:var(--astro-code-color-background);color:var(--astro-code-color-text); overflow-x: auto;',
87-
'color:var(--astro-code-token-constant)',
88-
'color:var(--astro-code-token-function)',
89-
'color:var(--astro-code-color-text)',
90-
'color:var(--astro-code-token-string-expression)',
91-
'color:var(--astro-code-color-text)',
92-
]);
89+
.toArray(),
90+
[
91+
'background-color:var(--astro-code-color-background);color:var(--astro-code-color-text); overflow-x: auto;',
92+
'color:var(--astro-code-token-constant)',
93+
'color:var(--astro-code-token-function)',
94+
'color:var(--astro-code-color-text)',
95+
'color:var(--astro-code-token-string-expression)',
96+
'color:var(--astro-code-color-text)',
97+
]
98+
);
9399
});
94100

95101
it('<Code> with custom theme and lang', async () => {
96102
let html = await fixture.readFile('/imported/index.html');
97103
const $ = cheerio.load(html);
98104

99-
expect($('#theme > pre')).to.have.lengthOf(1);
100-
expect($('#theme > pre').attr('style'), 'background-color: #FDFDFE; overflow-x: auto;');
105+
assert.equal($('#theme > pre').length, 1);
106+
assert.equal(
107+
$('#theme > pre').attr('style'),
108+
'background-color:#FDFDFE;color:#4E5377; overflow-x: auto;'
109+
);
101110

102-
expect($('#lang > pre')).to.have.lengthOf(1);
103-
expect($('#lang > pre > code span').length).to.equal(3);
111+
assert.equal($('#lang > pre').length, 1);
112+
assert.equal($('#lang > pre > code span').length, 3);
104113
});
105114

106115
it('<Code inline> has no pre tag', async () => {
107116
let html = await fixture.readFile('/inline/index.html');
108117
const $ = cheerio.load(html);
109118
const codeEl = $('.astro-code');
110119

111-
expect(codeEl.prop('tagName')).to.eq('CODE');
112-
expect(codeEl.attr('style')).to.include('background-color:');
113-
expect($('pre')).to.have.lengthOf(0);
120+
assert.equal(codeEl.prop('tagName'), 'CODE');
121+
assert.match(codeEl.attr('style'), /background-color:/);
122+
assert.equal($('pre').length, 0);
114123
});
115124
});

packages/astro/test/astro-cookies.test.js renamed to packages/astro/test/astro-cookies.nodetest.js

+25-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { expect } from 'chai';
21
import * as cheerio from 'cheerio';
2+
import assert from 'node:assert/strict';
3+
import { after, before, describe, it } from 'node:test';
34
import { loadFixture } from './test-utils.js';
45
import testAdapter from './test-adapter.js';
56

@@ -33,22 +34,22 @@ describe('Astro.cookies', () => {
3334
cookie: `prefs=${encodeURIComponent(JSON.stringify({ mode: 'light' }))}`,
3435
},
3536
});
36-
expect(response.status).to.equal(200);
37+
assert.equal(response.status, 200);
3738
const html = await response.text();
3839

3940
const $ = cheerio.load(html);
40-
expect($('dd').text()).to.equal('light');
41+
assert.equal($('dd').text(), 'light');
4142
});
4243

4344
it('can set the cookie value', async () => {
4445
const response = await fixture.fetch('/set-value', {
4546
method: 'POST',
4647
});
47-
expect(response.status).to.equal(200);
48+
assert.equal(response.status, 200);
4849
// Bug in 18.14.1 where `set-cookie` will not be defined
4950
// Should be fixed in 18.14.2
5051
if (process.versions.node !== '18.14.1') {
51-
expect(response.headers.has('set-cookie')).to.equal(true);
52+
assert.equal(response.headers.has('set-cookie'), true);
5253
}
5354
});
5455
});
@@ -72,41 +73,41 @@ describe('Astro.cookies', () => {
7273
cookie: `prefs=${encodeURIComponent(JSON.stringify({ mode: 'light' }))}`,
7374
},
7475
});
75-
expect(response.status).to.equal(200);
76+
assert.equal(response.status, 200);
7677
const html = await response.text();
7778

7879
const $ = cheerio.load(html);
79-
expect($('dd').text()).to.equal('light');
80+
assert.equal($('dd').text(), 'light');
8081
});
8182

8283
it('can set the cookie value', async () => {
8384
const response = await fetchResponse('/set-value', {
8485
method: 'POST',
8586
});
86-
expect(response.status).to.equal(200);
87+
assert.equal(response.status, 200);
8788
let headers = Array.from(app.setCookieHeaders(response));
88-
expect(headers).to.have.a.lengthOf(1);
89-
expect(headers[0]).to.match(/Expires/);
89+
assert.equal(headers.length, 1);
90+
assert.match(headers[0], /Expires/);
9091
});
9192

9293
it('app.render can include the cookie in the Set-Cookie header', async () => {
9394
const request = new Request('http://example.com/set-value', {
9495
method: 'POST',
9596
});
9697
const response = await app.render(request, { addCookieHeader: true });
97-
expect(response.status).to.equal(200);
98-
expect(response.headers.get('Set-Cookie'))
99-
.to.be.a('string')
100-
.and.satisfy((value) => value.startsWith('admin=true; Expires='));
98+
assert.equal(response.status, 200);
99+
const value = response.headers.get('Set-Cookie');
100+
assert.equal(typeof value, 'string');
101+
assert.equal(value.startsWith('admin=true; Expires='), true);
101102
});
102103

103104
it('app.render can exclude the cookie from the Set-Cookie header', async () => {
104105
const request = new Request('http://example.com/set-value', {
105106
method: 'POST',
106107
});
107108
const response = await app.render(request, { addCookieHeader: false });
108-
expect(response.status).to.equal(200);
109-
expect(response.headers.get('Set-Cookie')).to.equal(null);
109+
assert.equal(response.status, 200);
110+
assert.equal(response.headers.get('Set-Cookie'), null);
110111
});
111112

112113
it('Early returning a Response still includes set headers', async () => {
@@ -115,13 +116,13 @@ describe('Astro.cookies', () => {
115116
cookie: `prefs=${encodeURIComponent(JSON.stringify({ mode: 'light' }))}`,
116117
},
117118
});
118-
expect(response.status).to.equal(302);
119+
assert.equal(response.status, 302);
119120
let headers = Array.from(app.setCookieHeaders(response));
120-
expect(headers).to.have.a.lengthOf(1);
121+
assert.equal(headers.length, 1);
121122
let raw = headers[0].slice(6);
122123
let data = JSON.parse(decodeURIComponent(raw));
123-
expect(data).to.be.an('object');
124-
expect(data.mode).to.equal('dark');
124+
assert.equal(typeof data, 'object');
125+
assert.equal(data.mode, 'dark');
125126
});
126127

127128
it('API route can get and set cookies', async () => {
@@ -131,13 +132,13 @@ describe('Astro.cookies', () => {
131132
cookie: `prefs=${encodeURIComponent(JSON.stringify({ mode: 'light' }))}`,
132133
},
133134
});
134-
expect(response.status).to.equal(302);
135+
assert.equal(response.status, 302);
135136
let headers = Array.from(app.setCookieHeaders(response));
136-
expect(headers).to.have.a.lengthOf(1);
137+
assert.equal(headers.length, 1);
137138
let raw = headers[0].slice(6);
138139
let data = JSON.parse(decodeURIComponent(raw));
139-
expect(data).to.be.an('object');
140-
expect(data.mode).to.equal('dark');
140+
assert.equal(typeof data, 'object');
141+
assert.equal(data.mode, 'dark');
141142
});
142143
});
143144
});

0 commit comments

Comments
 (0)