From a75bf4e1cdc2a0d407ac17f797a91991ab6650f3 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Thu, 20 Oct 2022 11:28:32 -0400 Subject: [PATCH] test: fix some broken tests --- .../node/server/__tests__/moduleGraph.spec.ts | 24 +++++++++++++++---- .../server/__tests__/pluginContainer.spec.ts | 8 +++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/packages/vite/src/node/server/__tests__/moduleGraph.spec.ts b/packages/vite/src/node/server/__tests__/moduleGraph.spec.ts index 2285d2fa4fa8b9..8a354135cefaf7 100644 --- a/packages/vite/src/node/server/__tests__/moduleGraph.spec.ts +++ b/packages/vite/src/node/server/__tests__/moduleGraph.spec.ts @@ -14,21 +14,35 @@ describe('moduleGraph', () => { moduleGraph.invalidateModule(entryModule) expect(entryModule.ssrError).toBe(null) }) + }) + + describe('ensureEntryFromUrl', () => { + it('reuses an entry with same resolved id', async () => { + const moduleGraph = new ModuleGraph(async (url) => { + if (url === '/xx.js') { + return { id: '/x.js', meta: { vite: 'test' } } + } else { + return { id: url, meta: { vite: 'test' } } + } + }) + + const mod1 = await moduleGraph.ensureEntryFromUrl('/x.js', false) + const mod2 = await moduleGraph.ensureEntryFromUrl('/xx.js', false) + expect(mod1 === mod2).to.be.true + }) - it('ensureEntryFromUrl should based on resolvedId', async () => { + it('creates a new entry if resolved "meta" differs', async () => { const moduleGraph = new ModuleGraph(async (url) => { if (url === '/xx.js') { return { id: '/x.js' } } else { - return { id: url } + return { id: url, meta: { vite: 'test' } } } }) - const meta = { vite: 'test' } const mod1 = await moduleGraph.ensureEntryFromUrl('/x.js', false) - mod1.meta = meta const mod2 = await moduleGraph.ensureEntryFromUrl('/xx.js', false) - expect(mod2.meta).to.equal(meta) + expect(mod1 === mod2).to.be.false }) }) }) diff --git a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts index d7d9f1aed1e419..031e198c475231 100644 --- a/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts +++ b/packages/vite/src/node/server/__tests__/pluginContainer.spec.ts @@ -205,8 +205,12 @@ async function getPluginContainer( config.plugins = config.plugins.filter((p) => !/pre-alias/.test(p.name)) // @ts-ignore: So does this one and this mock one seems to work - const iap = config.plugins.find((p) => p.name === 'vite:import-analysis') - iap.configureServer({ moduleGraph }) + const iap = config.plugins.find((p) => p.name === 'vite:import-analysis')! + if (typeof iap.configureServer === 'function') { + iap.configureServer({ moduleGraph }) + } else { + throw 'Expected vite:import-analysis plugin to have a "configureServer" method' + } resolveId = (id) => container.resolveId(id) const container = await createPluginContainer(config, moduleGraph)