0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/astro/test/static-build-vite-plugins.test.js
Emanuele Stoppa ddf0f924b5
chore: move nodetest.js to test.js (#10142)
* chore: move `nodetest.js` to `test.js`

* chore: move `nodetest.js` to `test.js`

* remove script
2024-02-16 16:20:49 +00:00

83 lines
2.7 KiB
JavaScript

import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';
describe('Static build: vite plugins included when required', () => {
/** @type {Map<string, boolean>} */
const pluginsCalled = new Map();
/** @type {Map<string, boolean>} */
const expectedPluginResult = new Map([
['prepare-no-apply-plugin', true],
['prepare-serve-plugin', false],
['prepare-apply-fn-plugin', true],
['prepare-dont-apply-fn-plugin', false],
['prepare-build-plugin', true],
]);
before(async () => {
/** @type {import('./test-utils').Fixture} */
const fixture = await loadFixture({
root: './fixtures/astro pages/',
integrations: [
{
name: '@astrojs/prepare-vite-plugins',
hooks: {
'astro:config:setup': ({ updateConfig }) => {
pluginsCalled.set('prepare-no-apply-plugin', false);
pluginsCalled.set('prepare-serve-plugin', false);
pluginsCalled.set('prepare-apply-fn-plugin', false);
pluginsCalled.set('prepare-dont-apply-fn-plugin', false);
pluginsCalled.set('prepare-build-plugin', false);
updateConfig({
vite: {
plugins: [
{
name: 'prepare-no-apply-plugin',
configResolved: () => {
pluginsCalled.set('prepare-no-apply-plugin', true);
},
},
{
name: 'prepare-serve-plugin',
apply: 'serve',
configResolved: () => {
pluginsCalled.set('prepare-serve-plugin', true);
},
},
{
name: 'prepare-apply-fn-plugin',
apply: (_, { command }) => command === 'build',
configResolved: () => {
pluginsCalled.set('prepare-apply-fn-plugin', true);
},
},
{
name: 'prepare-dont-apply-fn-plugin',
apply: (_, { command }) => command === 'serve',
configResolved: () => {
pluginsCalled.set('prepare-dont-apply-fn-plugin', true);
},
},
{
name: 'prepare-build-plugin',
apply: 'build',
configResolved: () => {
pluginsCalled.set('prepare-build-plugin', true);
},
},
],
},
});
},
},
},
],
});
await fixture.build();
});
it('Vite Plugins are included/excluded properly', async () => {
assert.equal(pluginsCalled.size, expectedPluginResult.size, 'Not all plugins were initialized');
Array.from(expectedPluginResult.entries()).forEach(([plugin, called]) =>
assert.equal(pluginsCalled.get(plugin), called, `${plugin} was ${called ? 'not' : ''} called`)
);
});
});