blitz-js / blitz-js/blitz

getQueryKey does not work in jest

Open
#2,569 7 comments 1 reaction 0 assignees View on GitHub
kind/bug status/ready-to-work-on
Dominant language
TypeScript
Stars
14.1k
Forks
803
PR merge metrics
No merged PRs in 30d

Description

### What is the problem?

When I try to use the `getQueryKey` function in a test environment, I get `Cannot read property 'apiUrl' of undefined` error. From what I see it is caused by a non-existent `_meta` prop on the resolver. An easy solution that comes to mind is to just add optional chaining like this:
```ts
// getQueryKey
return getQueryKeyFromUrlAndParams(sanitizeQuery(resolver)._meta?.apiUrl, params)
```
but maybe it's a bigger problem than this.
Thanks for your help in advance!

### Paste all your error logs here:

```
TypeError: Cannot read property 'apiUrl' of undefined
at getQueryKey (node_modules/@blitzjs/core/dist/blitzjs-core.cjs.dev.js:325:67)
```

### Paste all relevant code snippets here:

```
getQueryKey(getCurrentUser);
```

### What are detailed steps to reproduce this?

Just run frontend jest test on a component that is using `getQueryKey` function

### Run `blitz -v` and paste the output here:

```
macOS Big Sur | darwin-x64 | Node: v14.17.0

blitz: 0.33.1 (global)
blitz: 0.38.1 (local)

Package manager: yarn
System:
OS: macOS 11.0.1
CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 2.14 GB / 32.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 14.17.0 - ~/.nvm/versions/node/v14.17.0/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 6.14.13 - ~/.nvm/versions/node/v14.17.0/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
npmPackages:
@prisma/client: 2.26.0 => 2.26.0
blitz: 0.38.1 => 0.38.1
prisma: 2.26.0 => 2.26.0
react: 18.0.0-alpha-568dc3532 => 18.0.0-alpha-568dc3532
react-dom: 18.0.0-alpha-568dc3532 => 18.0.0-alpha-568dc3532
typescript: 4.4.0-dev.20210702 => 4.4.0-dev.20210702
```

Contributor guide

Open the contributing guide

Research direction

Locate the implementation of getQueryKey and any tests covering resolver query keys; the issue only identifies the compiled file in node_modules and the getCurrentUser call. Reproduce the failure in a frontend Jest test, then verify that getQueryKey(getCurrentUser) no longer throws and that the behavior is covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.