Skip to content

Commit a709996

Browse files
committed
test(query): add regression coverage for react-query onMutate option (#1177)
The generated TanStack Query mutation hook exposes its `mutation` option as `UseMutationOptions`, which includes the `onMutate` callback. Issue #1177 reported `onMutate` being missing; it is fixed on current orval but had no regression test. Add a compile-time consumption test that calls a generated mutation hook with `mutation.onMutate` so a narrowed option type fails the typecheck. Wire the existing (previously orphaned) `tests/regressions` directory into `typecheck-generated.mjs` so these hand-written type tests run in CI.
1 parent cd37aea commit a709996

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

tests/regressions/react-query.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { keepPreviousData } from '@tanstack/react-query';
22

33
import {
4+
useCreatePets,
45
useListPets,
56
useListPetsInfinite,
67
} from '../generated/react-query/mutator/endpoints';
@@ -40,3 +41,22 @@ export const useHookWithPlaceHolderData = () => {
4041

4142
return names;
4243
};
44+
45+
// Regression test for https://github.com/orval-labs/orval/issues/1177
46+
//
47+
// The generated TanStack Query mutation hook must accept an `onMutate`
48+
// callback through its `mutation` option. `onMutate` is part of TanStack's
49+
// `UseMutationOptions`; if orval ever narrows that option type (as it did when
50+
// #1177 was filed), this function stops compiling and fails the typecheck.
51+
export const useCreatePetsWithOnMutate = () =>
52+
useCreatePets({
53+
mutation: {
54+
onMutate: (variables) => {
55+
// `variables` must be the typed mutation input, not `any`.
56+
void variables.data;
57+
// @ts-expect-error - a key absent from the mutation input must error;
58+
// this proves `variables` is strongly typed (not `any`).
59+
void variables.notAField;
60+
},
61+
},
62+
});

tests/scripts/typecheck-generated.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ for (const folder of folders) {
3434

3535
const config = {
3636
extends: './tsconfig.json',
37-
include: [`generated/${folder}`, 'mutators'],
37+
// `regressions` holds hand-written compile-time tests that import generated
38+
// code and exercise its public types (e.g. the #1177 onMutate regression),
39+
// so a narrowed option type fails the typecheck.
40+
include: [`generated/${folder}`, 'mutators', 'regressions'],
3841
};
3942

4043
// Bun's flat node_modules makes the MCP SDK resolve `zod` to the project's v3.25

0 commit comments

Comments
 (0)