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

type: vi.spyOn(window.localStorage, 'getItem') inferred as never #637

Closed
6 tasks done
cexbrayat opened this issue Jan 26, 2022 · 0 comments · Fixed by #643
Closed
6 tasks done

type: vi.spyOn(window.localStorage, 'getItem') inferred as never #637

cexbrayat opened this issue Jan 26, 2022 · 0 comments · Fixed by #643
Assignees

Comments

@cexbrayat
Copy link
Contributor

Describe the bug

vi.spyOn(window.localStorage, 'getItem') is wrongly inferred as never isntead of SpyInstance.

tests/composables/UserService.spec.ts:58:44 - error TS2339: Property 'mockReturnValue' does not exist on type 'never'.

58     vi.spyOn(window.localStorage, 'getItem').mockReturnValue(null));

Reproduction

This can be seen in the TS playground here

import { vi, SpyInstance } from 'vitest'

// does not compile when spying on Storage (spy inferred as never)
vi.spyOn(window.localStorage, 'getItem').mockReturnValue(null);
vi.spyOn(Storage.prototype, 'getItem').mockReturnValue(null);

// whereas it properly compiles on an object
const storage = {
    getItem(key: string): string | null { return null }
}
vi.spyOn(storage, 'getItem').mockReturnValue(null);

// workaround: explicit cast
(vi.spyOn(Storage.prototype, 'getItem') as SpyInstance<Array<string>, string | null>).mockReturnValue(null);

System Info

System:
    OS: macOS 12.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 15.84 GB / 64.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.2 - ~/.volta/tools/image/node/16.13.2/bin/node
    Yarn: 1.22.17 - ~/.volta/tools/image/yarn/1.22.17/bin/yarn
    npm: 8.3.2 - ~/.volta/tools/image/npm/8.3.2/bin/npm
  Browsers:
    Chrome: 97.0.4692.99
    Firefox: 96.0.2
    Safari: 15.2
  npmPackages:
    @vitejs/plugin-vue: 2.0.1 => 2.0.1
    vite: 2.7.13 => 2.7.13
    vitest: 0.2.3 => 0.2.3

Used Package Manager

npm

Validations

@DerYeger DerYeger added the bug label Jan 27, 2022
@DerYeger DerYeger self-assigned this Jan 27, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants