|
1 | 1 | import assert from 'node:assert/strict'; |
2 | | -import { before, describe, it } from 'node:test'; |
| 2 | +import { after, before, describe, it } from 'node:test'; |
3 | 3 | import { loadFixture } from './test-utils.js'; |
4 | 4 |
|
5 | 5 | describe('Component bundling', () => { |
| 6 | + /** @type {import('./test-utils.js').Fixture} */ |
6 | 7 | let fixture; |
7 | 8 |
|
8 | 9 | before(async () => { |
9 | 10 | fixture = await loadFixture({ root: './fixtures/astro-component-bundling/' }); |
10 | | - await fixture.build(); |
11 | 11 | }); |
12 | 12 |
|
13 | | - it('should treeshake FooComponent', async () => { |
14 | | - const astroChunkDir = await fixture.readdir('/_astro'); |
15 | | - const manyComponentsChunkName = astroChunkDir.find((chunk) => |
16 | | - chunk.startsWith('ManyComponents'), |
17 | | - ); |
18 | | - const manyComponentsChunkContent = await fixture.readFile(`/_astro/${manyComponentsChunkName}`); |
19 | | - assert.equal(manyComponentsChunkContent.includes('FooComponent'), false); |
| 13 | + describe('dev', () => { |
| 14 | + /** @type {import('./test-utils.js').DevServer} */ |
| 15 | + let devServer; |
| 16 | + |
| 17 | + before(async () => { |
| 18 | + devServer = await fixture.startDevServer(); |
| 19 | + }); |
| 20 | + |
| 21 | + after(async () => { |
| 22 | + await devServer.stop(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should not include Astro components in client bundles', async () => { |
| 26 | + const importedComponent = await fixture.fetch('/src/components/AstroComponent.astro'); |
| 27 | + const moduleContent = await importedComponent.text(); |
| 28 | + assert( |
| 29 | + moduleContent.includes('Astro components cannot be used in the browser.'), |
| 30 | + 'Astro component imported from client should include error text in dev.', |
| 31 | + ); |
| 32 | + assert( |
| 33 | + moduleContent.length < 3500, |
| 34 | + 'Module content should be small and not include full server-side code.', |
| 35 | + ); |
| 36 | + assert( |
| 37 | + !moduleContent.includes('import '), |
| 38 | + 'Astro component imported from client should not include import statements.', |
| 39 | + ); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('build', () => { |
| 44 | + before(async () => { |
| 45 | + await fixture.build(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should treeshake FooComponent', async () => { |
| 49 | + const astroChunkDir = await fixture.readdir('/_astro'); |
| 50 | + const manyComponentsChunkName = astroChunkDir.find((chunk) => |
| 51 | + chunk.startsWith('ManyComponents'), |
| 52 | + ); |
| 53 | + const manyComponentsChunkContent = await fixture.readFile( |
| 54 | + `/_astro/${manyComponentsChunkName}`, |
| 55 | + ); |
| 56 | + assert.equal(manyComponentsChunkContent.includes('FooComponent'), false); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should not include Astro components in client bundles', async () => { |
| 60 | + const html = await fixture.readFile('/astro-in-client/index.html'); |
| 61 | + const match = /<script.+<\/script>/.exec(html); |
| 62 | + assert(match, 'Expected a <script> tag to be present'); |
| 63 | + assert.match( |
| 64 | + match[0], |
| 65 | + /^<script type="module">const \w=\{\};console.log\(\w\);<\/script>$/, |
| 66 | + 'Astro component on the client should be an empty object in prod', |
| 67 | + ); |
| 68 | + }); |
20 | 69 | }); |
21 | 70 | }); |
0 commit comments