test(core): add unit tests for has-facet-values promotion condition#4882
test(core): add unit tests for has-facet-values promotion condition#4882GabrielRoc wants to merge 1 commit into
Conversation
Adds a spec file covering the `at_least_n_with_facets` check() via equivalence partitioning and boundary-value analysis. Uses a lightweight mock injected via init() to avoid database access. 7 tests. Relates to vendurehq#4835
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
I have read the CLA Document and I hereby sign the CLA You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot. |
📝 WalkthroughWalkthroughAdds a Vitest spec for 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Adds a focused Vitest unit test suite for the at_least_n_with_facets (hasFacetValues) promotion condition in @vendure/core, improving direct function coverage of this business-logic condition without requiring DB/TestServer setup.
Changes:
- Introduces a new spec file covering threshold/boundary behavior for
minimumvs summed matching quantities. - Adds multi-line aggregation and non-matching exclusion scenarios.
- Verifies
FacetValueChecker.hasFacetValues()is invoked with the expected(line, facets, ctx)arguments via an injected mock.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const ctx = createRequestContext({ pricesIncludeTax: false }); | ||
|
|
||
| async function check(order: Order, args: ConfigArg[]) { | ||
| return hasFacetValues.check(ctx, order, args, undefined as any); | ||
| } |
| it('passes the ctx and facet ID list to hasFacetValues', async () => { | ||
| const order = orderWith([line('v1', 1)]); | ||
| const args = buildArgs(1, ['f1', 'f2']); | ||
| mockHasFacetValues.mockResolvedValue(true); | ||
|
|
||
| await check(order, args); | ||
|
|
||
| expect(mockHasFacetValues).toHaveBeenCalledWith(order.lines[0], ['f1', 'f2'], ctx); | ||
| }); |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@packages/core/src/config/promotion/conditions/has-facet-values-condition.spec.ts`:
- Around line 25-28: The mock in has-facet-values-condition.spec.ts is using the
old two-parameter vi.fn generic form, which is incompatible with Vitest 3.x.
Update the mock declaration for mockHasFacetValues to use the current single
function-type generic signature based on FacetValueChecker['hasFacetValues'], so
the test type-checks correctly with the newer vi.fn API.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: b11cadd5-a9db-40ab-b6ce-43959280bbec
📒 Files selected for processing (1)
packages/core/src/config/promotion/conditions/has-facet-values-condition.spec.ts
| const mockHasFacetValues = vi.fn< | ||
| Parameters<FacetValueChecker['hasFacetValues']>, | ||
| ReturnType<FacetValueChecker['hasFacetValues']> | ||
| >(); |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# Locate Vitest version and the spec file
git ls-files 'packages/core/src/config/promotion/conditions/has-facet-values-condition.spec.ts' 'package.json' 'pnpm-lock.yaml' 'package-lock.json' 'yarn.lock' '**/package.json' | sed -n '1,120p'
echo '--- package.json candidates with vitest ---'
rg -n '"vitest"|vi\.fn<|FacetValueChecker\[' -S . --glob '!**/node_modules/**' --glob '!**/dist/**' --glob '!**/build/**'
echo '--- file excerpt ---'
sed -n '1,120p' packages/core/src/config/promotion/conditions/has-facet-values-condition.spec.tsRepository: vendurehq/vendure
Length of output: 10308
🌐 Web query:
Vitest 3.2.4 vi.fn generic signature Parameters ReturnType two generics single function type docs
💡 Result:
In Vitest, including version 3.2.4, the vi.fn function accepts a single optional generic type parameter that represents the function signature [1][2]. This generic is used to strictly type the mock's arguments and return value [2][3]. To specify the signature, you pass a function type to the generic slot [2]. For example, if you want a mock that accepts a string and a boolean and returns a number, you would define it as follows [2]: const myMock = vi.fn<(arg1: string, arg2: boolean) => number>; When you provide this signature: 1. TypeScript uses the Parameters utility type to infer the argument types for the mock's implementation and call tracking methods [4][3]. 2. TypeScript uses the ReturnType utility type to enforce the return type of the mock's implementation and return-value methods (such as.mockReturnValue) [5][4][3]. If no generic type is provided, Vitest defaults to a signature of (...args: any[]) => any [2]. It is also possible to pass an implementation directly to vi.fn, in which case Vitest will attempt to infer the types automatically [1][6]. If the inferred type is insufficient or you are creating a mock without an initial implementation, explicitly providing the generic signature as shown above is the recommended approach [2].
Citations:
- 1: https://github.com/vitest-dev/vitest/blob/fda52af9c3974d5fd16161e1ed502dd47fd38ceb/packages/spy/src/index.ts
- 2: https://github.com/vitest-dev/eslint-plugin-vitest/blob/master/docs/rules/require-mock-type-parameters.md
- 3: https://github.com/vitest-dev/vitest/blob/v4.1.9/docs/api/mock.md
- 4: https://github.com/vitest-dev/vitest/blob/206e8cff/packages/spy/src/types.ts
- 5: https://vitest.dev/api/mock.html
- 6: https://vitest.dev/api/vi
Use the current vi.fn generic signature
vi.fn in Vitest 3.x takes a single function-type generic, so this two-parameter form should be rewritten to vi.fn<FacetValueChecker['hasFacetValues']>() to avoid a type error.
Suggested fix
-const mockHasFacetValues = vi.fn<
- Parameters<FacetValueChecker['hasFacetValues']>,
- ReturnType<FacetValueChecker['hasFacetValues']>
->();
+const mockHasFacetValues = vi.fn<FacetValueChecker['hasFacetValues']>();📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const mockHasFacetValues = vi.fn< | |
| Parameters<FacetValueChecker['hasFacetValues']>, | |
| ReturnType<FacetValueChecker['hasFacetValues']> | |
| >(); | |
| const mockHasFacetValues = vi.fn<FacetValueChecker['hasFacetValues']>(); |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In
`@packages/core/src/config/promotion/conditions/has-facet-values-condition.spec.ts`
around lines 25 - 28, The mock in has-facet-values-condition.spec.ts is using
the old two-parameter vi.fn generic form, which is incompatible with Vitest 3.x.
Update the mock declaration for mockHasFacetValues to use the current single
function-type generic signature based on FacetValueChecker['hasFacetValues'], so
the test type-checks correctly with the newer vi.fn API.
Description
Adds a direct unit test suite for the
at_least_n_with_facetspromotion condition (has-facet-values-condition.ts), which previously had 0% function coverage.The condition delegates per-line facet matching to
FacetValueCheckerand sums the qualifying quantities againstminimum. BecauseFacetValueCheckeris a NestJS-injected service, the test injects avi.fn()mock via the condition'sinit()hook — no database or TestServer is required.7 tests covering:
minimum(below / at / above)ctxand the facet ID list are forwarded as-is tohasFacetValues()Breaking changes
None.
Checklist
Relates to #4835
Need help on this PR? Tag
/codesmithwith what you need. Autofix is disabled.