From 5c0ac4adf1786b0731294b35c8dcda0a4ff6a07a Mon Sep 17 00:00:00 2001 From: Vladimir Date: Wed, 21 Jun 2023 10:41:22 +0200 Subject: [PATCH] fix(browser): esm injector doesn't replace class expressions (#3641) --- packages/browser/src/node/esmInjector.ts | 2 +- test/core/test/injector-esm.test.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/browser/src/node/esmInjector.ts b/packages/browser/src/node/esmInjector.ts index a8e0514b1987..80b96cb01214 100644 --- a/packages/browser/src/node/esmInjector.ts +++ b/packages/browser/src/node/esmInjector.ts @@ -241,7 +241,7 @@ export function injectVitestModule(code: string, id: string, parse: (code: strin s.prependRight(topNode.start, `const ${id.name} = ${binding};\n`) } } - else { + else if (parent.type !== 'ClassExpression') { s.update(id.start, id.end, binding) } }, diff --git a/test/core/test/injector-esm.test.ts b/test/core/test/injector-esm.test.ts index af034297940f..3a2b3eb34038 100644 --- a/test/core/test/injector-esm.test.ts +++ b/test/core/test/injector-esm.test.ts @@ -933,3 +933,13 @@ function test() { }" `) }) + +test('avoid binding ClassExpression', () => { + const result = injectSimpleCode( + 'import Foo, {Bar} from \'./foo\';console.log(Foo, Bar);const obj = {foo: class Foo{}, bar: class Bar{}}', + ) + expect(result).toMatchInlineSnapshot(` + "import { __vi_inject__ as __vi_esm_0__ } from './foo' + console.log(__vi_esm_0__.default, __vi_esm_0__.Bar);const obj = {foo: class Foo{}, bar: class Bar{}}" + `) +})