Skip to content

Child objects of a mocked instance can't access their own private fields #151

Description

@mchalapuk

Hi, I seem to be having a bit of a problem with mocked instances that have child objects that use private fields.

A short piece of code replicating the issue:

import test from "ava"
import { Mock } from "typemoq"
import * as crypto from "crypto"

class UUID {
  #uuid: string

  constructor(uuid: string = crypto.randomUUID()) {
    this.#uuid = uuid
  }
  toString() {
    return this.#uuid
  }
}

test("test mock with a private field in a child object", t => {
  const uuid = new UUID()
  t.assert(uuid.toString(), uuid.toString()) // this works

  const mock = Mock.ofInstance({ uuid })
  mock.object.uuid.toString() // this throws an error
})

The error I'm getting is the following:
Screenshot 2023-12-13 at 02 33 17

I think it has something to do with how proxies are used in typemoq.

If I'm missing something, I'd appreciate if someone could direct me to a solution. Otherwise, it seems to be a big problem which needs solving. I'm happy to work on it and create a PR but I would need some direction on how to solve it.

Thanks.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions