Skip to content

Commit

Permalink
Use real filesystem for unit testing (#12172)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Oct 15, 2024
1 parent 5ab2d98 commit 64bb796
Show file tree
Hide file tree
Showing 38 changed files with 583 additions and 945 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ package-lock.json

packages/astro/src/**/*.prebuilt.ts
packages/astro/src/**/*.prebuilt-dev.ts
!packages/astro/vendor/vite/dist
packages/astro/test/units/_temp-fixtures/*
!packages/astro/test/units/_temp-fixtures/package.json
packages/integrations/**/.netlify/

# exclude IntelliJ/WebStorm stuff
Expand Down
1 change: 1 addition & 0 deletions biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"**/dist/**",
"**/smoke/**",
"**/fixtures/**",
"**/_temp-fixtures/**",
"**/vendor/**",
"**/.vercel/**",
],
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
"allowAny": [
"astro"
]
},
"patchedDependencies": {
"[email protected]": "patches/[email protected]"
}
}
}
9 changes: 5 additions & 4 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,15 @@
"build": "pnpm run prebuild && astro-scripts build \"src/**/*.{ts,js}\" --copy-wasm && tsc",
"build:ci": "pnpm run prebuild && astro-scripts build \"src/**/*.{ts,js}\" --copy-wasm",
"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}\"",
"test": "pnpm run test:node && pnpm run test:types",
"test:match": "pnpm run test:node --match",
"test": "pnpm run test:unit && pnpm run test:integration && pnpm run test:types",
"test:match": "astro-scripts test \"test/**/*.test.js\" --match",
"test:e2e": "pnpm test:e2e:chrome && pnpm test:e2e:firefox",
"test:e2e:match": "playwright test -g",
"test:e2e:chrome": "playwright test",
"test:e2e:firefox": "playwright test --config playwright.firefox.config.js",
"test:types": "tsc --project tsconfig.tests.json",
"test:node": "astro-scripts test \"test/**/*.test.js\""
"test:unit": "astro-scripts test \"test/units/**/*.test.js\" --teardown ./test/units/teardown.js",
"test:integration": "astro-scripts test \"test/*.test.js\""
},
"dependencies": {
"@astrojs/compiler": "^2.10.3",
Expand Down Expand Up @@ -210,9 +211,9 @@
"eol": "^0.10.0",
"execa": "^8.0.1",
"expect-type": "^1.1.0",
"fs-fixture": "^2.4.0",
"mdast-util-mdx": "^3.0.0",
"mdast-util-mdx-jsx": "^3.1.3",
"memfs": "^4.14.0",
"node-mocks-http": "^1.16.1",
"parse-srcset": "^1.0.2",
"rehype-autolink-headings": "^7.1.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/astro/src/vite-plugin-astro-server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export default function createVitePluginAstroServer({
}

process.on('unhandledRejection', handleUnhandledRejection);
viteServer.httpServer?.on('close', () => {
process.off('unhandledRejection', handleUnhandledRejection);
});

return () => {
// Push this middleware to the front of the stack so that it can intercept responses.
Expand Down

This file was deleted.

16 changes: 0 additions & 16 deletions packages/astro/test/fixtures/content-mixed-errors/package.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

8 changes: 8 additions & 0 deletions packages/astro/test/units/_temp-fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "astro-temp-fixtures",
"description": "This directory contains nested directories of dynamically created unit test fixtures. The deps here can be used by them",
"dependencies": {
"@astrojs/mdx": "workspace:*",
"astro": "workspace:*"
}
}
20 changes: 7 additions & 13 deletions packages/astro/test/units/config/format.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import * as assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';
import { createFs, runInContainer } from '../test-utils.js';

const root = new URL('../../fixtures/tailwindcss-ts/', import.meta.url);
import { createFixture, runInContainer } from '../test-utils.js';

describe('Astro config formats', () => {
it('An mjs config can import TypeScript modules', async () => {
const fs = createFs(
{
'/src/pages/index.astro': ``,
'/src/stuff.ts': `export default 'works';`,
'/astro.config.mjs': `
const fixture = await createFixture({
'/src/pages/index.astro': ``,
'/src/stuff.ts': `export default 'works';`,
'/astro.config.mjs': `\
import stuff from './src/stuff.ts';
export default {}
`,
},
root,
);
});

await runInContainer({ fs, inlineConfig: { root: fileURLToPath(root) } }, () => {
await runInContainer({ inlineConfig: { root: fixture.path } }, () => {
assert.equal(
true,
true,
Expand Down
36 changes: 8 additions & 28 deletions packages/astro/test/units/content-collections/frontmatter.test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
import nodeFS from 'node:fs';
import path from 'node:path';
import { describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';
import { attachContentServerListeners } from '../../../dist/content/index.js';
import { createFs, runInContainer, triggerFSEvent } from '../test-utils.js';

const root = new URL('../../fixtures/alias/', import.meta.url);

function getTypesDts() {
const typesdtsURL = new URL('../../../templates/content/types.d.ts', import.meta.url);
const relpath = path
.relative(fileURLToPath(root), fileURLToPath(typesdtsURL))
.replace(/\\/g, '/');
return {
[relpath]: nodeFS.readFileSync(typesdtsURL, 'utf-8'),
};
}
import { createFixture, runInContainer } from '../test-utils.js';

describe('frontmatter', () => {
it('errors in content/ does not crash server', async () => {
const fs = createFs(
{
...getTypesDts(),
'/src/content/posts/blog.md': `
const fixture = await createFixture({
'/src/content/posts/blog.md': `\
---
title: One
---
`,
'/src/content/config.ts': `
'/src/content/config.ts': `\
import { defineCollection, z } from 'astro:content';
const posts = defineCollection({
Expand All @@ -38,7 +21,7 @@ describe('frontmatter', () => {
posts
};
`,
'/src/pages/index.astro': `
'/src/pages/index.astro': `\
---
---
<html>
Expand All @@ -48,14 +31,12 @@ describe('frontmatter', () => {
</body>
</html>
`,
},
root,
);
});

await runInContainer({ fs, inlineConfig: { root: fileURLToPath(root) } }, async (container) => {
await runInContainer({ inlineConfig: { root: fixture.path } }, async (container) => {
await attachContentServerListeners(container);

fs.writeFileFromRootSync(
await fixture.writeFile(
'/src/content/posts/blog.md',
`
---
Expand All @@ -64,7 +45,6 @@ describe('frontmatter', () => {
---
`,
);
triggerFSEvent(container, fs, '/src/content/posts/blog.md', 'change');
await new Promise((resolve) => setTimeout(resolve, 100));
// Note, if we got here, it didn't crash
});
Expand Down
70 changes: 0 additions & 70 deletions packages/astro/test/units/correct-path.js

This file was deleted.

Loading

0 comments on commit 64bb796

Please sign in to comment.