Skip to content

Commit 5c0ac4a

Browse files
authoredJun 21, 2023
fix(browser): esm injector doesn't replace class expressions (#3641)
1 parent 36c06d1 commit 5c0ac4a

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed
 

‎packages/browser/src/node/esmInjector.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export function injectVitestModule(code: string, id: string, parse: (code: strin
241241
s.prependRight(topNode.start, `const ${id.name} = ${binding};\n`)
242242
}
243243
}
244-
else {
244+
else if (parent.type !== 'ClassExpression') {
245245
s.update(id.start, id.end, binding)
246246
}
247247
},

‎test/core/test/injector-esm.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -933,3 +933,13 @@ function test() {
933933
}"
934934
`)
935935
})
936+
937+
test('avoid binding ClassExpression', () => {
938+
const result = injectSimpleCode(
939+
'import Foo, {Bar} from \'./foo\';console.log(Foo, Bar);const obj = {foo: class Foo{}, bar: class Bar{}}',
940+
)
941+
expect(result).toMatchInlineSnapshot(`
942+
"import { __vi_inject__ as __vi_esm_0__ } from './foo'
943+
console.log(__vi_esm_0__.default, __vi_esm_0__.Bar);const obj = {foo: class Foo{}, bar: class Bar{}}"
944+
`)
945+
})

0 commit comments

Comments
 (0)
Please sign in to comment.