Add Astro full compile unit test (#5531)

This commit is contained in:
Bjorn Lu 2022-12-06 22:11:23 +08:00 committed by GitHub
parent d8e00a4871
commit 0a62ecd894
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,77 @@
import { expect } from 'chai';
import { resolveConfig } from 'vite';
import { cachedFullCompilation } from '../../../dist/vite-plugin-astro/compile.js';
import { init, parse } from 'es-module-lexer';
const viteConfig = await resolveConfig({ configFile: false }, 'serve');
/**
* @param {string} source
* @param {string} id
*/
async function compile(source, id) {
return await cachedFullCompilation({
compileProps: {
astroConfig: { root: '/', base: '/' },
viteConfig,
filename: id,
source,
},
logging: {
level: 'info',
},
rawId: id,
});
}
describe('astro full compile', () => {
it('should compile a single file', async () => {
const result = await compile(`<h1>Hello World</h1>`, '/src/components/index.astro');
expect(result.code).to.be.ok;
});
it('should compile typescript', async () => {
const result = await compile(
`\
---
const name: string = 'world'
---
<h1>Hello {name}</h1>`,
'/src/components/index.astro'
);
expect(result.code).to.be.ok;
});
it('should error on invalid js', async () => {
let result;
try {
result = await compile(
`\
---
const name = 'world
---
<h1>Hello {name}</h1>`,
'/src/components/index.astro'
);
} catch (e) {
expect(e.message).to.include('Unterminated string literal');
}
expect(result).to.be.undefined;
});
it('injects hmr code', async () => {
const result = await compile(`<h1>Hello World</h1>`, '/src/components/index.astro');
expect(result.code).to.include('import.meta.hot');
});
it('has file and url exports for markdwon compat', async () => {
const result = await compile(`<h1>Hello World</h1>`, '/src/components/index.astro');
await init;
const [, exports] = parse(result.code);
expect(exports).to.include('default');
expect(exports).to.include('file');
expect(exports).to.include('url');
});
});