Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.18.1] TypeError: Symbol.hasInstance is read-only #1671

Closed
6 tasks done
wKovacs64 opened this issue Jul 18, 2022 · 9 comments · Fixed by #1786
Closed
6 tasks done

[0.18.1] TypeError: Symbol.hasInstance is read-only #1671

wKovacs64 opened this issue Jul 18, 2022 · 9 comments · Fixed by #1786

Comments

@wKovacs64
Copy link

Describe the bug

My tests that use vi.mock (in this case, mocking out the ora module) started failing as of vitest v0.18.1 with the following error:

TypeError: Cannot assign to read only property 'Symbol(Symbol.hasInstance)' of function 'function(...o) {
    if (e.called = !0, e.callCount++, e.calls.push(o), e.next) {
      let [s, l] = e.next;
  ...<omitted>... }'

In the reproduction (below), it presents itself as simply: TypeError: Symbol.hasInstance is read-only

I'm assuming this is related to PR #1648 but I'm not sure if I need to update my usage to comply with the intent of the PR or what.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-ht4a9x?file=test/mock.test.ts&view=editor

System Info

System:
    OS: Windows 10 10.0.19044
    CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
    Memory: 17.74 GB / 31.92 GB
  Binaries:
    Node: 16.16.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.18 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 8.12.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 103.0.5060.114
    Edge: Spartan (44.19041.1266.0), Chromium (103.0.1264.62)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    vitest: 0.18.1 => 0.18.1

Used Package Manager

npm

Validations

@spacedawwwg
Copy link

spacedawwwg commented Jul 22, 2022

I've just come across this issue with my mocks, too. Locked in my project to 0.18.0 for now

@sheremet-va
Copy link
Member

@simon-abbott do you have any ideas on how to fix this?

@spacedawwwg
Copy link

Still an issue in 0.19.0
image

@dojineko
Copy link

dojineko commented Jul 26, 2022

Reproduced it with 0.19.1.
I used a workaround like this. :)

vi.mock('something', async () => ({
  // @ts-ignore for mocking
  ...(await vi.importActual('something')),
}))

Edit:
@sheremet-va
I felt like sharing a means to fix it in case you really need to fix it in a hurry. Sorry if I made you feel disappointed by describing a reproduction by version.

@sheremet-va
Copy link
Member

Please, don't post comments like "still not working". When someone will work on a fix, it will be displayed here.

@nieyuyao
Copy link
Contributor

When the newContainer[property] is a function, is there no need to continue recursion?

if (isFunction) {
spyModule.spyOn(newContainer, property).mockImplementation(() => undefined)
// tinyspy retains length, but jest doesn't.
Object.defineProperty(newContainer[property], 'length', { value: 0 })
}

@simon-abbott
Copy link
Contributor

@simon-abbott do you have any ideas on how to fix this?

Hmm. I know roughly what is causing this, but I'll need a little bit of time to be able to fix it. I'll try to take a look this evening.

@simon-abbott
Copy link
Contributor

simon-abbott commented Aug 3, 2022

When the newContainer[property] is a function, is there no need to continue recursion?

@nieyuyao Not recursing into functions was the cause of #1523, since Classes in javascript are just functions with special properties.

simon-abbott added a commit to simon-abbott/vitest that referenced this issue Aug 4, 2022
…t-dev#1671)

These two methods seem like they should be the same, but they're not.
Some objects, especially native Node exports, behave strangely; they
have properties that are enumerated by `Object.getOwnPropertyNames()`
and/or `Object.getOwnPropertySymbols()`, but when you try to get the
descriptor for that property using `Object.getOwnPropertyDescriptor()`
it returns `undefined`. To combat this, instead of getting the property
names / symbols manually, we instead offload that work to the JS engine
itself via `Object.getOwnPropertyDescriptors`, and just iterate through
the result.

Fixes vitest-dev#1671
simon-abbott added a commit to simon-abbott/vitest that referenced this issue Aug 4, 2022
…-dev#1671)

Sometimes while automocking we will encounter a property that, for some
unknown reason, throws an error when you try to define it. Unfortunately
I can't figure out _why_ it errors, so instead I have opted to quietly
skip these unsettable properties. If that becomes a problem in the
future this can be revisited, but I don't forsee it being an issue since
these keys are mostly deeply internal prototype stuff that 99.999% of
people don't even know exists, let alone want to mock. Plus, if you
_really_ need to have this behavior you can always use `__mocks__` or
just shim it inline yourself.
@simon-abbott
Copy link
Contributor

Figured out a fix! #1786.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.