Skip to content

Add mock.value() to mock a valueΒ #58322

Closed
@regseb

Description

@regseb

What is the problem this feature will solve?

I want to mock an value of object in my unit tests.

{ value: {…}, writable: true, enumerable: true, configurable: true }

The MockTracker class can mock method, getter and setter, but not value.


I'm currently using sinon, but I'd like to replace it with a native Node.js mock.

sinon.stub(browser.contextMenus, "ContextType").value({ LINK: "link", PAGE: "page" });

That would also replace this kind of case:

const originalContextType = browser.contextMenus.ContextType;
try {
    browser.contextMenus.ContextType = { LINK: "link", PAGE: "page" };

    // Do test.
} finally {
    browser.contextMenus.ContextType = originalContextType;
}

What is the feature you are proposing to solve the problem?

mock.value(object, valueName, value)

  • object <Object> The object whose value is being mocked.
  • valueName <string> | <symbol> The identifier of the value on object to mock. If object[valueName] is not a value, an error is thrown.
  • value <any> A value used as the mock value for object[valueName].

What alternatives have you considered?

The mock.getter() function can mock a value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    Status

    Awaiting Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions