Skip to content

Commit

Permalink
fix(browser): transform superclass identifier (#3681)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jun 27, 2023
1 parent 6d26b8c commit a1e043b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/browser/src/node/esmInjector.ts
Expand Up @@ -241,7 +241,10 @@ export function injectVitestModule(code: string, id: string, parse: (code: strin
s.prependRight(topNode.start, `const ${id.name} = ${binding};\n`)
}
}
else if (parent.type !== 'ClassExpression') {
else if (
// don't transform class name identifier
!(parent.type === 'ClassExpression' && id === parent.id)
) {
s.update(id.start, id.end, binding)
}
},
Expand Down
20 changes: 18 additions & 2 deletions test/core/test/injector-esm.test.ts
Expand Up @@ -936,10 +936,26 @@ 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{}}',
`
import Foo, { Bar } from './foo';
console.log(Foo, Bar);
const obj = {
foo: class Foo {},
bar: class Bar {}
}
const Baz = class extends Foo {}
`,
)
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{}}"
console.log(__vi_esm_0__.default, __vi_esm_0__.Bar);
const obj = {
foo: class Foo {},
bar: class Bar {}
}
const Baz = class extends __vi_esm_0__.default {}
"
`)
})

0 comments on commit a1e043b

Please sign in to comment.