Skip to content

import a variable with the name "Object" will result in an unexpected error #4829

@yArna

Description

@yArna

Describe the bug

If you import a variable with the symbolic name "Object" in the test file, an error will occur.

// 1.test.js
import { Object } from "zod3"

test("1", ()=>{ Object })

Error:

TypeError: Object.defineProperty is not a function

After tracing, I found that in the code finally executed by vitest, code using Object.defineProperty was injected into the user code space.

I think vitest needs to use protective variable names here

// vite-node/dist/client.mjs
// const code = `${codeDefinition}${transformed}
// code :

 'use strict';async (__vite_ssr_import__,__vite_ssr_dynamic_import__,__vite_ssr_exports__,__vite_ssr_exportAll__,__vite_ssr_import_meta__,require,exports,module,__filename,__dirname)=>

{{const Object = {}};

Object.defineProperty(__vite_ssr_exports__, "Object",
 { enumerable: true,
 configurable: true, 
get(){ return Object }
}
);

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-xdxnxc?file=test%2Fbasic.test.ts

System Info

System:
    OS: macOS 12.7
    CPU: (8) x64 Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz
    Memory: 66.71 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - ~/.npm-global/bin/yarn
    npm: 9.8.1 - /usr/local/bin/npm
    pnpm: 8.8.0 - ~/.npm-global/bin/pnpm
    bun: 1.0.18 - ~/.bun/bin/bun
  Browsers:
    Chrome: 120.0.6099.129
    Chrome Canary: 119.0.6030.0
    Edge: 120.0.2210.91
  npmPackages:
    @vitejs/plugin-legacy: 5.2.0 => 5.2.0 
    @vitejs/plugin-vue2: 2.3.1 => 2.3.1 
    vite: 5.0.10 => 5.0.10 
    vitest: 1.1.0 => 1.1.0

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions