Add Astro full compile unit test (#5531)
This commit is contained in:
parent
d8e00a4871
commit
0a62ecd894
1 changed files with 77 additions and 0 deletions
77
packages/astro/test/units/vite-plugin-astro/compile.test.js
Normal file
77
packages/astro/test/units/vite-plugin-astro/compile.test.js
Normal 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');
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue