mirror of
https://github.com/withastro/astro.git
synced 2025-02-03 22:29:08 -05:00
Add a test to ensure that types.d.ts
is valid (#11277)
This commit is contained in:
parent
dc41ece28f
commit
9056d8dd05
5 changed files with 33 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
||||||
import assert from 'node:assert/strict';
|
import assert from 'node:assert/strict';
|
||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
import { before, describe, it } from 'node:test';
|
import { before, describe, it } from 'node:test';
|
||||||
|
import ts from 'typescript';
|
||||||
import { loadFixture } from './test-utils.js';
|
import { loadFixture } from './test-utils.js';
|
||||||
|
|
||||||
const createFixture = () => {
|
const createFixture = () => {
|
||||||
|
@ -62,6 +63,20 @@ const createFixture = () => {
|
||||||
const expectedPath = new URL(path, astroFixture.config.root).href;
|
const expectedPath = new URL(path, astroFixture.config.root).href;
|
||||||
assert.equal(writtenFiles[expectedPath].includes(content), true, error);
|
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' {`,
|
`declare module 'astro:content' {`,
|
||||||
'Types file does not include `astro:content` module declaration'
|
'Types file does not include `astro:content` module declaration'
|
||||||
);
|
);
|
||||||
|
fixture.thenFileShouldBeValidTypescript('.astro/types.d.ts');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Writes types for empty collections', async () => {
|
it('Writes types for empty collections', async () => {
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
import { defineCollection, z } from 'astro:content';
|
import { defineCollection, z } from 'astro:content';
|
||||||
|
|
||||||
|
const withData = defineCollection({
|
||||||
|
type: 'data',
|
||||||
|
schema: z.object({
|
||||||
|
title: z.string(),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
const withCustomSlugs = defineCollection({
|
const withCustomSlugs = defineCollection({
|
||||||
// Ensure schema passes even when `slug` is present
|
// Ensure schema passes even when `slug` is present
|
||||||
schema: z.object({}).strict(),
|
schema: z.object({}).strict(),
|
||||||
|
@ -46,6 +53,7 @@ const withSymlinkedContent = defineCollection({
|
||||||
});
|
});
|
||||||
|
|
||||||
export const collections = {
|
export const collections = {
|
||||||
|
'with-data': withData,
|
||||||
'with-custom-slugs': withCustomSlugs,
|
'with-custom-slugs': withCustomSlugs,
|
||||||
'with-schema-config': withSchemaConfig,
|
'with-schema-config': withSchemaConfig,
|
||||||
'with-union-schema': withUnionSchema,
|
'with-union-schema': withUnionSchema,
|
||||||
|
|
3
packages/astro/test/fixtures/content-collections/src/content/with-data/one.json
vendored
Normal file
3
packages/astro/test/fixtures/content-collections/src/content/with-data/one.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"title": "One"
|
||||||
|
}
|
3
packages/astro/test/fixtures/content-collections/src/content/with-data/three.json
vendored
Normal file
3
packages/astro/test/fixtures/content-collections/src/content/with-data/three.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"title": "Three"
|
||||||
|
}
|
3
packages/astro/test/fixtures/content-collections/src/content/with-data/two.json
vendored
Normal file
3
packages/astro/test/fixtures/content-collections/src/content/with-data/two.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"title": "Two"
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue