|
| 1 | +--- |
| 2 | +name: write-frontend-unit-test |
| 3 | +description: Write or update Baserow frontend unit tests for core, premium, or enterprise code using the repo's existing Vitest, Nuxt, Vue Test Utils, TestApp, and snapshot patterns. |
| 4 | +--- |
| 5 | + |
| 6 | +# Write Baserow Frontend Unit Tests |
| 7 | + |
| 8 | +Use this skill when a task is to add, fix, or extend a frontend unit test in `web-frontend`, `premium/web-frontend`, or `enterprise/web-frontend`. |
| 9 | + |
| 10 | +Do not invent a generic Vue testing style. This repo already has established patterns. Start by finding the closest existing spec and copy its setup shape. |
| 11 | + |
| 12 | +## First Step |
| 13 | + |
| 14 | +Before editing, identify the test target: |
| 15 | + |
| 16 | +1. Pure utility or parser function |
| 17 | +2. Vuex store logic |
| 18 | +3. Vue component mounted with the shared app context |
| 19 | +4. Nuxt/Vue 3 component mounted directly with `mountSuspended` |
| 20 | +5. Premium or enterprise variant of one of the above |
| 21 | + |
| 22 | +Then inspect the nearest existing spec in the same module area. |
| 23 | + |
| 24 | +Useful searches: |
| 25 | + |
| 26 | +- `rg --files web-frontend/test premium/web-frontend/test enterprise/web-frontend/test | rg '\.spec\.'` |
| 27 | +- `rg -n "new TestApp\\(|new PremiumTestApp\\(|mountSuspended\\(" web-frontend/test premium/web-frontend/test enterprise/web-frontend/test` |
| 28 | +- `rg -n "toMatchSnapshot\\(|vi\\.fn\\(|vi\\.spyOn\\(" web-frontend/test premium/web-frontend/test enterprise/web-frontend/test` |
| 29 | + |
| 30 | +## Tooling Used In This Repo |
| 31 | + |
| 32 | +Current frontend unit tests use: |
| 33 | + |
| 34 | +- `vitest` for `describe`, `test`, `expect`, `vi` |
| 35 | +- `@vue/test-utils` |
| 36 | +- `@nuxt/test-utils/runtime` with `mountSuspended` |
| 37 | +- Repo helpers such as `TestApp`, `PremiumTestApp`, `MockServer`, and fixtures under `web-frontend/test` |
| 38 | +- Snapshot assertions for rendered HTML when the component output matters |
| 39 | + |
| 40 | +Important local files: |
| 41 | + |
| 42 | +- `web-frontend/vitest.setup.ts` |
| 43 | +- `web-frontend/test/helpers/testApp.js` |
| 44 | +- `premium/web-frontend/test/helpers/premiumTestApp.js` |
| 45 | + |
| 46 | +`vitest.setup.ts` already mocks i18n, UUID generation, and `WebSocket`. Reuse that environment instead of re-mocking those globally in each spec. |
| 47 | + |
| 48 | +## Choose The Right Pattern |
| 49 | + |
| 50 | +### Pure utility tests |
| 51 | + |
| 52 | +For functions in `modules/*/utils/**`, keep the test simple: |
| 53 | + |
| 54 | +1. Import the function directly. |
| 55 | +2. Use plain inputs and deterministic assertions. |
| 56 | +3. Prefer `toStrictEqual`, `toBe`, or explicit formatted objects over snapshots. |
| 57 | + |
| 58 | +Good examples: |
| 59 | + |
| 60 | +- `web-frontend/test/unit/core/utils/date.spec.js` |
| 61 | +- `web-frontend/test/unit/core/utils/string.spec.js` |
| 62 | + |
| 63 | +### Store tests |
| 64 | + |
| 65 | +For Vuex store behavior, prefer `TestApp` unless the existing spec clearly uses a temporary local store: |
| 66 | + |
| 67 | +1. Create `testApp = new TestApp()` in `beforeEach`. |
| 68 | +2. Read `store = testApp.store`. |
| 69 | +3. Seed state through store actions or the mock server. |
| 70 | +4. Always `await testApp.afterEach()` in `afterEach`. |
| 71 | + |
| 72 | +Good examples: |
| 73 | + |
| 74 | +- `web-frontend/test/unit/core/store/auth.spec.js` |
| 75 | +- `web-frontend/test/unit/builder/store/dataSource.spec.js` |
| 76 | + |
| 77 | +If the code lives in premium and needs premium-only auth/license behavior, use `PremiumTestApp`. |
| 78 | + |
| 79 | +### Shared app component tests |
| 80 | + |
| 81 | +For many components, especially older patterns or components coupled to store, router, registry, or client behavior: |
| 82 | + |
| 83 | +1. Create `testApp = new TestApp()` or `new PremiumTestApp()`. |
| 84 | +2. Mount with `testApp.mount(Component, { props, propsData, slots, listeners, global })`. |
| 85 | +3. Prefer the existing helper in the file, for example `mountComponent(...)`. |
| 86 | +4. Clean up with `await testApp.afterEach()`. |
| 87 | + |
| 88 | +`TestApp.mount` supports both `props` and legacy `propsData`, and converts `listeners` into Vue 3 event props. Match the nearby spec instead of rewriting all setup. |
| 89 | + |
| 90 | +Good examples: |
| 91 | + |
| 92 | +- `web-frontend/test/unit/core/components/dropdown.spec.js` |
| 93 | +- `premium/web-frontend/test/unit/premium/view/calendar/calendarView.spec.js` |
| 94 | + |
| 95 | +### Direct `mountSuspended` component tests |
| 96 | + |
| 97 | +For newer Nuxt/Vue 3 component tests that do not need the full helper wrapper: |
| 98 | + |
| 99 | +1. Use `const testApp = useNuxtApp()` in `beforeEach` if the component expects injected app/store context. |
| 100 | +2. Mount with `mountSuspended(Component, { props, slots, global: { provide, stubs, mocks } })`. |
| 101 | +3. Provide injected dependencies explicitly. |
| 102 | + |
| 103 | +Good examples: |
| 104 | + |
| 105 | +- `web-frontend/test/unit/builder/components/elements/components/HeadingElement.spec.js` |
| 106 | + |
| 107 | +## Assertions |
| 108 | + |
| 109 | +Prefer the narrowest assertion that proves behavior: |
| 110 | + |
| 111 | +- Use `toStrictEqual` or `toEqual` for transformed data and store state. |
| 112 | +- Use `toBe` for scalar values. |
| 113 | +- Use `vi.fn()` and `vi.spyOn()` for event handlers and method calls. |
| 114 | +- Use snapshots for rendered markup where the repo already uses them. |
| 115 | + |
| 116 | +Do not default to snapshots for pure logic. |
| 117 | + |
| 118 | +When asserting reactive store objects, this repo sometimes normalizes them with: |
| 119 | + |
| 120 | +```js |
| 121 | +JSON.parse(JSON.stringify(value)) |
| 122 | +``` |
| 123 | + |
| 124 | +Use that only when the nearby test does it for Vue reactivity serialization issues. |
| 125 | + |
| 126 | +Don't assert internals, always assert visible result in the DOM. For instance don't |
| 127 | +use |
| 128 | + |
| 129 | +```js |
| 130 | +expect(wrapper.vm.values.use_instance_smtp_settings).toBe(false) # BAD |
| 131 | +``` |
| 132 | + |
| 133 | +Don't directly use vm properties. |
| 134 | + |
| 135 | +## Mocking And Fixtures |
| 136 | + |
| 137 | +Prefer repo helpers over bespoke mocks: |
| 138 | + |
| 139 | +1. Use `testApp.mockServer` when the behavior depends on store-backed API calls. |
| 140 | +2. Use fixtures under `web-frontend/test/fixtures` and premium or enterprise fixture folders when suitable. |
| 141 | +3. Use `testApp.dontFailOnErrorResponses()` only when the test intentionally exercises failing responses. |
| 142 | + |
| 143 | +Do not build a large custom mock environment if `TestApp` already provides the needed app, client, registry, router, and store wiring. |
| 144 | + |
| 145 | +## File Placement |
| 146 | + |
| 147 | +Follow the existing test tree: |
| 148 | + |
| 149 | +- Core: `web-frontend/test/unit/...` |
| 150 | +- Premium: `premium/web-frontend/test/unit/...` |
| 151 | +- Enterprise: `enterprise/web-frontend/test/unit/...` |
| 152 | + |
| 153 | +Keep the spec near the feature area rather than creating a new generic test folder. |
| 154 | + |
| 155 | +## Validation |
| 156 | + |
| 157 | +Run the narrowest relevant test command first. |
| 158 | + |
| 159 | +Examples: |
| 160 | + |
| 161 | +- `just f yarn test:core --run test/unit/core/components/dropdown.spec.js` |
| 162 | +- `just f yarn test:core --run test/unit/core/store/auth.spec.js` |
| 163 | +- `just f yarn test:premium --run ../premium/web-frontend/test/unit/premium/view/calendar/calendarView.spec.js` |
| 164 | +- `just f yarn test:enterprise --run ../enterprise/web-frontend/test/unit/enterprise/plugins.spec.js` |
| 165 | + |
| 166 | +If a snapshot changes intentionally, review the diff instead of blindly accepting it. |
| 167 | + |
| 168 | +## Guardrails |
| 169 | + |
| 170 | +- Do not introduce Jest APIs. Use Vitest APIs already present in the repo. |
| 171 | +- Do not add a standalone mount helper when `TestApp` or `PremiumTestApp` already fits. |
| 172 | +- Do not over-mock store, router, or client dependencies if the real test helpers can provide them. |
| 173 | +- Do not mix unrelated styles in one file. Match the nearest local spec. |
| 174 | +- Do not leave out `afterEach` cleanup when using `TestApp` or `PremiumTestApp`. |
| 175 | +- Do not create broad integration-style tests when a focused unit test is enough. |
0 commit comments