diff --git a/packages/astro/test/units/vite-plugin-astro/compile.test.js b/packages/astro/test/units/vite-plugin-astro/compile.test.js new file mode 100644 index 000000000..49fedb3ed --- /dev/null +++ b/packages/astro/test/units/vite-plugin-astro/compile.test.js @@ -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(`

Hello World

`, '/src/components/index.astro'); + expect(result.code).to.be.ok; + }); + + it('should compile typescript', async () => { + const result = await compile( + `\ +--- +const name: string = 'world' +--- + +

Hello {name}

`, + '/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 +--- + +

Hello {name}

`, + '/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(`

Hello World

`, '/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(`

Hello World

`, '/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'); + }); +});