Skip to content

Can't use global changes w/ a module (webcrypto-jwt) #528

@fadi-george

Description

@fadi-george

Describe the bug

jsonwebtoken is not something that can be used with Vite and so I have to use an alternative package like webcrypto-jwt for signing jwts. For testing, I get the error Error: crypto.subtle not found. So w/ Jest, I had to set that property in my setup test file, e.g.

Object.defineProperty(global, 'crypto', {
  value: {
    subtle: {
      importKey: () => Promise.resolve(''),
      sign: () => Promise.resolve(''),
    },
  },
  writable: true,
});

But with vitest, this doesn't work.

Reproduction

I have an example here:
https://stackblitz.com/edit/node-htxtbj?file=test%2FsomeActions.spec.tsx

But for whatever reason, this works in Stackblitz but downloading and running the project locally, it errors.

System Info

System:
    OS: macOS 12.1
    CPU: (8) arm64 Apple M1 Pro
    Memory: 89.92 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.13.1/bin/yarn
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.1/bin/npm
  Browsers:
    Brave Browser: 97.1.34.80
    Chrome: 97.0.4692.71
    Safari: 15.2
  npmPackages:
    @vitejs/plugin-react: ^1.1.4 => 1.1.4 
    vite: 2.7.11 => 2.7.11

Used Package Manager

npm

Validations

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