SRVOCF-822: Replace test mocks with stubs and extract shared test infrastructure - #149
Conversation
|
@twoGiants: This pull request references SRVOCF-822 which is a valid jira issue. Warning: The referenced jira issue has an invalid target version for the target branch this PR targets: expected the story to target the "5.1.0" version, but no target version was set. DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository. |
1154e29 to
39ae7bb
Compare
0ef6397 to
c87bb9a
Compare
c87bb9a to
69a143c
Compare
|
Bad rebase, I think you removed this change:
|
The list page tests mocked useCluster entirely, hiding the hook's internals (status derivation, resource pairing, label selectors) behind pre-computed return values. The useCluster tests used a TestConsumer component to render hook output into DOM elements, then asserted via data-testid queries. Replace the useCluster mock with a shared useK8sWatchResourceStub that both useCluster.test.tsx and FunctionsListPage.test.tsx use. Tests now set up raw K8s resource fixtures (Knative Services, Deployments) and let the real useCluster derive status, replicas, and URL. Shape mismatches between the hook and its consumers are caught at test time instead of hidden by a mock. Replace TestConsumer with renderHook from @testing-library/react. Hook return values are asserted directly on result.current instead of through DOM nodes. Extract shared test infrastructure into src/common/testing: authFake (session auth helpers), constants (BACKEND_API), mswServer (default MSW handlers + server), and the K8s watch resource stub with fixture builders. Move FUNCTION_NAME_LABEL and REVISION_LABEL from useCluster.ts to types.ts so both production code and test stubs can reference them without circular imports. Keep .tmp directory for vitest output. Issue SRVOCF-822 Signed-off-by: Stanislav Jakuschevskij <sjakusch@redhat.com>
69a143c to
949ad22
Compare
|
/lgtm |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: pmeida The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
@twoGiants: all tests passed! Full PR test history. Your PR dashboard. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here. |
Summary
useClustermock in list page tests with a shareduseK8sWatchResourceStubthat lets the real hook run, catching shape mismatches between the hook and its consumersTestConsumerrender pattern inuseCluster.test.tsxwithrenderHookfrom@testing-library/reactFunctionTablemock from list page tests so the real table component is exercisedsrc/common/testing/:authFake(session auth helpers),constants,mswServer(default MSW handlers),functionsClientStub, and the K8s watch resource stub with fixture buildersFUNCTION_NAME_LABELandREVISION_LABELfromuseCluster.tstotypes.tsso both production code and test stubs can reference them without circular importsCOPY testing/ testing/from Dockerfile (directory no longer exists)TESTING.mdto reflect new MSW handler locationRelates to SRVOCF-822
Checklist
docs/ARCHITECTURE.md(if there are relevant changes to our layered architecture)Additional Info
This is preparatory test refactoring for the namespace scoping feature (SRVOCF-822). The feature implementation will land in a follow-up PR once SRVOCF-979 merges.
The key motivation is twofold. First, the list page tests mocked
useClusterentirely, hiding the hook's internals behind pre-computed return values. With the shareduseK8sWatchResourceStub, tests set up raw K8s resource fixtures (Knative Services, Deployments) and let the realuseClusterderive status, replicas, and URL. Type or field changes in the hook's return shape are now caught immediately instead of passing against a stale mock.Second, this PR cleans up and streamlines a large chunk of the test suite, introducing testing conventions and a common testing library under
src/common/testing/. This library is intended to grow with reusable test doubles and fixtures in future PRs. Not all tests are refactored here; the remaining test files will be migrated incrementally as related work touches them.