Skip to content

mock.property(): 'process.env' does not accept an accessor(getter/setter) descriptor #60486

@regseb

Description

@regseb

Version

v22.21.0

Platform

Linux regseblaptop4l 6.14.0-34-generic #34-Ubuntu SMP PREEMPT_DYNAMIC Wed Sep 17 09:21:29 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

  1. Create index.js:
    import { mock } from "node:test";
    
    console.log(Object.getOwnPropertyDescriptor(process.env, "PATH"));
    
    mock.property(process.env, "PATH", "foo");
    
    console.log(process.env.PATH);
  2. node index.js

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior? Why is that the expected behavior?

The variable process.env.PATH is mocked by mock.property(object, propertyName[, value]).

{
  value: '/usr/local/sbin:/usr/local/bin:/usr/sbin',
  writable: true,
  enumerable: true,
  configurable: true
}
foo

What do you see instead?

{
  value: '/usr/local/sbin:/usr/local/bin:/usr/sbin',
  writable: true,
  enumerable: true,
  configurable: true
}
node:internal/test_runner/mock/mock:308
    ObjectDefineProperty(object, propertyName, {
    ^

TypeError: 'process.env' does not accept an accessor(getter/setter) descriptor
    at defineProperty (<anonymous>)
    at new MockPropertyContext (node:internal/test_runner/mock/mock:308:5)
    at MockTracker.property (node:internal/test_runner/mock/mock:717:7)
    at file:///home/regseb/testcase/index.js:5:6
    at ModuleJob.run (node:internal/modules/esm/module_job:343:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:665:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:117:5) {
  code: 'ERR_INVALID_OBJECT_DEFINE_PROPERTY'
}

Node.js v22.21.0

Additional information

Pull request for adding the mock.property() method: #58438

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions