Skip to content

Commit 6a1a17d

Browse files
authored
Add CSS @import test (#4454)
* Add css hmr test * Allow HMR in e2e tests * Add changeset * Actually fix test * Fix lint * Skip windows for now
1 parent 5e568e4 commit 6a1a17d

File tree

8 files changed

+77
-2
lines changed

8 files changed

+77
-2
lines changed

.changeset/odd-swans-collect.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Allow HMR for internal e2e tests

packages/astro/e2e/css.test.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { expect } from '@playwright/test';
2+
import { getColor, isWindows, testFactory } from './test-utils.js';
3+
4+
const test = testFactory({
5+
root: './fixtures/css/',
6+
});
7+
8+
let devServer;
9+
10+
test.beforeAll(async ({ astro }) => {
11+
devServer = await astro.startDevServer();
12+
});
13+
14+
test.afterAll(async () => {
15+
await devServer.stop();
16+
});
17+
18+
test.describe('CSS HMR', () => {
19+
test.skip(isWindows, 'TODO: fix css hmr in windows');
20+
21+
test('edit CSS from @import', async ({ page, astro }) => {
22+
await page.goto(astro.resolveUrl('/'));
23+
24+
const h = page.locator('h1');
25+
expect(await getColor(h)).toBe('rgb(255, 0, 0)');
26+
27+
await astro.editFile('./src/styles/main.css', (original) =>
28+
original.replace('--h1-color: red;', '--h1-color: green;')
29+
);
30+
31+
expect(await getColor(h)).toBe('rgb(0, 128, 0)');
32+
});
33+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@e2e/css",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"astro": "workspace:*"
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h1>hello world</h1>
2+
3+
<style>
4+
@import "../styles/main.css";
5+
6+
h1 {
7+
color: var(--h1-color);
8+
}
9+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:root {
2+
--h1-color: red;
3+
}

packages/astro/e2e/test-utils.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { test as testBase, expect } from '@playwright/test';
22
import { loadFixture as baseLoadFixture } from '../test/test-utils.js';
33

4+
export const isWindows = process.platform === 'win32';
5+
46
export function loadFixture(inlineConfig) {
57
if (!inlineConfig || !inlineConfig.root)
68
throw new Error("Must provide { root: './fixtures/...' }");
@@ -40,3 +42,11 @@ export async function getErrorOverlayMessage(page) {
4042

4143
return await overlay.$$eval('.message-body', (m) => m[0].textContent);
4244
}
45+
46+
/**
47+
* @param {import('@playwright/test').Locator} el
48+
* @returns {Promise<string>}
49+
*/
50+
export async function getColor(el) {
51+
return await el.evaluate((e) => getComputedStyle(e).color);
52+
}

packages/astro/src/vite-plugin-astro/hmr.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import { info } from '../core/logger/core.js';
77
import * as msg from '../core/messages.js';
88
import { isAstroScript } from './query.js';
99

10-
const PKG_PREFIX = new URL('../../', import.meta.url);
10+
const PKG_PREFIX = fileURLToPath(new URL('../../', import.meta.url));
11+
const E2E_PREFIX = fileURLToPath(new URL('../../e2e', import.meta.url));
1112
const isPkgFile = (id: string | null) => {
12-
return id?.startsWith(fileURLToPath(PKG_PREFIX)) || id?.startsWith(PKG_PREFIX.pathname);
13+
return id && id.startsWith(PKG_PREFIX) && !id.startsWith(E2E_PREFIX);
1314
};
1415

1516
export interface HandleHotUpdateOptions {

pnpm-lock.yaml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)