0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-27 22:19:04 -05:00

Add a test to ensure that types.d.ts is valid (#11277)

This commit is contained in:
Mark Gaze 2024-06-20 13:30:43 +01:00 committed by GitHub
parent dc41ece28f
commit 9056d8dd05
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 33 additions and 0 deletions

View file

@ -1,6 +1,7 @@
import assert from 'node:assert/strict';
import * as fs from 'node:fs';
import { before, describe, it } from 'node:test';
import ts from 'typescript';
import { loadFixture } from './test-utils.js';
const createFixture = () => {
@ -62,6 +63,20 @@ const createFixture = () => {
const expectedPath = new URL(path, astroFixture.config.root).href;
assert.equal(writtenFiles[expectedPath].includes(content), true, error);
},
thenFileShouldBeValidTypescript(path) {
const expectedPath = new URL(path, astroFixture.config.root).href;
try {
const content = writtenFiles[expectedPath];
const result = ts.transpileModule(content, {
compilerOptions: {
module: ts.ModuleKind.ESNext,
},
});
assert.equal(result.outputText, '', `${path} should be valid TypeScript. Output: ${result.outputText}`);
} catch (error) {
assert.fail(`${path} is not valid TypeScript. Error: ${error.message}`);
}
},
};
};
@ -87,6 +102,7 @@ describe('astro sync', () => {
`declare module 'astro:content' {`,
'Types file does not include `astro:content` module declaration'
);
fixture.thenFileShouldBeValidTypescript('.astro/types.d.ts');
});
it('Writes types for empty collections', async () => {

View file

@ -1,5 +1,12 @@
import { defineCollection, z } from 'astro:content';
const withData = defineCollection({
type: 'data',
schema: z.object({
title: z.string(),
}),
});
const withCustomSlugs = defineCollection({
// Ensure schema passes even when `slug` is present
schema: z.object({}).strict(),
@ -46,6 +53,7 @@ const withSymlinkedContent = defineCollection({
});
export const collections = {
'with-data': withData,
'with-custom-slugs': withCustomSlugs,
'with-schema-config': withSchemaConfig,
'with-union-schema': withUnionSchema,

View file

@ -0,0 +1,3 @@
{
"title": "One"
}

View file

@ -0,0 +1,3 @@
{
"title": "Three"
}

View file

@ -0,0 +1,3 @@
{
"title": "Two"
}