Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Ship the starters with testing already configured

Damian Legawiec8afa9c412cc0
diff --git a/docs/developer/tutorial/testing.mdx b/docs/developer/tutorial/testing.mdx
index 0bafaa34d4..e3d82a552b 100644
--- a/docs/developer/tutorial/testing.mdx
+++ b/docs/developer/tutorial/testing.mdx
@@ -7,75 +7,26 @@ Most of the backend is generated, so most of the testing worth doing is on the c
## The backend
-Your project arrives with RSpec, FactoryBot and Spree's own helpers already configured — every factory core ships is available, so `create(:product)` works without any setup.
-
-The generator wrote a factory and a controller spec for each API side:
+Your project arrives with a fully configured testing environment for backend testing. The generator wrote all the files needed for running API tests:
```text
-server/spec/factories/spree/brand_factory.rb
server/spec/controllers/spree/api/v3/store/brands_controller_spec.rb
server/spec/controllers/spree/api/v3/admin/brands_controller_spec.rb
```
-Run them, a directory or a single file at a time:
+You can now run them, a directory or a single file at a time:
```bash
spree rspec spec/controllers/spree/api/v3/
spree rspec spec/models/spree/brand_spec.rb
-spree rspec spec/models/spree/brand_spec.rb:12 # one example
```
-Three conventions carry most of the weight. Build records through **factories** rather than creating them directly, so a model change does not mean editing fifty specs. Add `render_views` to a controller spec, or a broken serializer passes silently. And use the suite's `@default_store` rather than creating another store marked as the default — two of them produce failures that surface far from their cause.
-
-What is worth testing is the behaviour you chose, not what the framework already guarantees:
-
-```ruby server/spec/models/spree/brand_spec.rb
-RSpec.describe Spree::Brand, type: :model do
- it 'nullifies the reference instead of deleting products' do
- brand = create(:brand)
- product = create(:product, brand: brand)
-
- brand.destroy
-
- expect(product.reload.brand_id).to be_nil
- expect(product).to be_persisted
- end
-end
-```
-
-A presence validation on a required column needs no spec. The `dependent: :nullify` you picked in step 1 does, because a future change could quietly turn it into `:destroy` and take the catalog with it.
-
-The generated controller specs are worth one addition — proof that the Ransack allowlist actually permits what the dashboard filters by:
-
-```ruby server/spec/controllers/spree/api/v3/store/brands_controller_spec.rb
-it 'filters by name' do
- create(:brand, name: 'Wilson')
- create(:brand, name: 'Penn')
-
- get :index, params: { q: { name_cont: 'wil' }, format: :json }
-
- expect(json_response['data'].map { |b| b['name'] }).to eq(['Wilson'])
-end
-```
-
-Remove `name` from `whitelisted_ransackable_attributes` and this fails — which is the point, because the dashboard column that sorts by it would fail too.
-
## The dashboard
-This is where most of your own code lives, so it is where most of your tests belong. Unlike the backend, the starter ships no test setup — add it once:
+This is where most of your own code lives, so it is where most of your tests belong. Vitest and Playwright come configured, so there is nothing to set up:
```bash
-cd apps/dashboard
-pnpm add -D vitest @playwright/test
-```
-
-Then two scripts in `apps/dashboard/package.json`:
-
-```json
-"scripts": {
- "test": "vitest run",
- "test:e2e": "playwright test"
-}
+cd apps/dashboard && pnpm test
```
### What is worth a unit test
diff --git a/packages/dashboard-starter/AGENTS.md b/packages/dashboard-starter/AGENTS.md
index c87ada08e4..03493b3e10 100644
--- a/packages/dashboard-starter/AGENTS.md
+++ b/packages/dashboard-starter/AGENTS.md
@@ -64,5 +64,8 @@ of what exists, not an implementation.
```bash
pnpm typecheck
pnpm lint
+pnpm test # unit tests (Vitest, Node environment)
pnpm build
```
+
+Test the logic between the UI and the API — query keys, payload mapping, permission predicates. Do not write tests that render a component to assert its markup; that tests React. Use `pnpm test:e2e` when a browser is genuinely needed.
diff --git a/packages/dashboard-starter/README.md b/packages/dashboard-starter/README.md
index ebdcf56df0..35887b80d9 100644
--- a/packages/dashboard-starter/README.md
+++ b/packages/dashboard-starter/README.md
@@ -119,6 +119,19 @@ import en from './locales/en.json'
i18n.addResourceBundle('en', 'translation', en, true, true)
```
+## Test
+
+Vitest and Playwright are configured — no setup needed.
+
+```bash
+pnpm test # unit tests: src/**/*.test.ts
+pnpm test:e2e # end-to-end, through a browser
+```
+
+Unit tests run in Node rather than a DOM, because what is worth testing here is the logic between your UI and the API: query keys, payload mapping, permission predicates. Rendering a component to assert its markup tests React, not your feature — use an end-to-end test when you need a browser.
+
+`pnpm test:e2e` starts the dashboard itself and reuses one you already have running, but expects your API to be up (`spree dev`). Point it elsewhere with `E2E_BASE_URL`.
+
## Build & deploy
The app root `Dockerfile` already builds this package and bakes it into the production image, available at `https://yourstore.com/dashboard` URL.
diff --git a/packages/dashboard-starter/package.json b/packages/dashboard-starter/package.json
index ae77edfd12..204f9e4725 100644
--- a/packages/dashboard-starter/package.json
+++ b/packages/dashboard-starter/package.json
@@ -8,6 +8,9 @@
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
+ "test": "vitest run",
+ "test:watch": "vitest",
+ "test:e2e": "playwright test",
"typecheck": "tsc --noEmit",
"lint": "biome check .",
"lint:fix": "biome check --fix ."
@@ -24,11 +27,13 @@
},
"devDependencies": {
"@biomejs/biome": "^2.4.14",
+ "@playwright/test": "^1.59.1",
"@spree/dashboard-plugin-example": "workspace:^",
"@types/react": "^19.2.14",
"@types/react-dom": "^19.2.3",
"@vitejs/plugin-react": "^6.0.1",
"typescript": "~6.0.3",
- "vite": "^8.0.16"
+ "vite": "^8.0.16",
+ "vitest": "^4.1.5"
}
}
diff --git a/packages/dashboard-starter/playwright.config.ts b/packages/dashboard-starter/playwright.config.ts
new file mode 100644
index 0000000000..cecdcef948
--- /dev/null
+++ b/packages/dashboard-starter/playwright.config.ts
@@ -0,0 +1,37 @@
+import { defineConfig, devices } from '@playwright/test'
+
+// End-to-end tests run against your own stack: `spree dev` for the API, and
+// the dev server Playwright starts below. Point them somewhere else with
+// E2E_BASE_URL — a preview build, or a deployed environment.
+const BASE_URL = process.env.E2E_BASE_URL || 'http://localhost:5173'
+
+export default defineConfig({
+ testDir: './e2e',
+ // One worker: specs share a database, so parallel runs interfere unless
+ // every spec creates its own records under a unique name.
+ fullyParallel: false,
+ workers: 1,
+ retries: process.env.CI ? 2 : 0,
+ timeout: 30_000,
+ expect: { timeout: 10_000 },
+ reporter: process.env.CI ? [['github'], ['html', { open: 'never' }]] : 'list',
+
+ use: {
+ baseURL: BASE_URL,
+ trace: 'on-first-retry',
+ screenshot: 'only-on-failure',
+ },
+
+ projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
+
+ // Starts the dashboard for the run and reuses one you already have open.
+ // The API is expected to be running separately (`spree dev`).
+ webServer: process.env.E2E_BASE_URL
+ ? undefined
+ : {
+ command: 'pnpm dev --port 5173 --strictPort',
+ url: BASE_URL,
+ reuseExistingServer: true,
+ timeout: 60_000,
+ },
+})
diff --git a/packages/dashboard-starter/vitest.config.ts b/packages/dashboard-starter/vitest.config.ts
new file mode 100644
index 0000000000..7177e2bfb2
--- /dev/null
+++ b/packages/dashboard-starter/vitest.config.ts
@@ -0,0 +1,14 @@
+import { defineConfig } from 'vitest/config'
+
+export default defineConfig({
+ test: {
+ globals: true,
+ // Node, not jsdom: what is worth unit-testing here is the logic between
+ // your UI and the API — query keys, payload mapping, permission
+ // predicates. Rendering a component to assert its markup tests React.
+ // Reach for a browser through Playwright instead (`pnpm test:e2e`).
+ environment: 'node',
+ include: ['src/**/*.{test,spec}.{ts,tsx}'],
+ exclude: ['node_modules/**', 'e2e/**', 'dist/**'],
+ },
+})
diff --git a/packages/seller-dashboard-starter/AGENTS.md b/packages/seller-dashboard-starter/AGENTS.md
index 1007d8b671..8dce2416d9 100644
--- a/packages/seller-dashboard-starter/AGENTS.md
+++ b/packages/seller-dashboard-starter/AGENTS.md
@@ -68,5 +68,8 @@ of what exists, not an implementation.
```bash
pnpm typecheck
pnpm lint
+pnpm test # unit tests (Vitest, Node environment)
pnpm build
```
+
+Test the logic between the UI and the API — query keys, payload mapping, permission predicates. Do not write tests that render a component to assert its markup; that tests React. Use `pnpm test:e2e` when a browser is genuinely needed.
diff --git a/packages/seller-dashboard-starter/package.json b/packages/seller-dashboard-starter/package.json
index f4308746e6..c0f3c6bd3f 100644
--- a/packages/seller-dashboard-starter/package.json
+++ b/packages/seller-dashboard-starter/package.json
@@ -8,6 +8,9 @@
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
+ "test": "vitest run",
+ "test:watch": "vitest",
+ "test:e2e": "playwright test",
"typecheck": "tsc --noEmit",
"lint": "biome check .",
"lint:fix": "biome check --fix ."
@@ -24,10 +27,12 @@
},
"devDependencies": {
"@biomejs/biome": "^2.4.14",
+ "@playwright/test": "^1.59.1",
"@types/react": "^19.2.14",
"@types/react-dom": "^19.2.3",
"@vitejs/plugin-react": "^6.0.1",
"typescript": "~6.0.3",
- "vite": "^8.0.16"
+ "vite": "^8.0.16",
+ "vitest": "^4.1.5"
}
}
diff --git a/packages/seller-dashboard-starter/playwright.config.ts b/packages/seller-dashboard-starter/playwright.config.ts
new file mode 100644
index 0000000000..713ff49038
--- /dev/null
+++ b/packages/seller-dashboard-starter/playwright.config.ts
@@ -0,0 +1,37 @@
+import { defineConfig, devices } from '@playwright/test'
+
+// End-to-end tests run against your own stack: `spree dev` for the API, and
+// the dev server Playwright starts below. Point them somewhere else with
+// E2E_BASE_URL — a preview build, or a deployed environment.
+const BASE_URL = process.env.E2E_BASE_URL || 'http://localhost:5174'
+
+export default defineConfig({
+ testDir: './e2e',
+ // One worker: specs share a database, so parallel runs interfere unless
+ // every spec creates its own records under a unique name.
+ fullyParallel: false,
+ workers: 1,
+ retries: process.env.CI ? 2 : 0,
+ timeout: 30_000,
+ expect: { timeout: 10_000 },
+ reporter: process.env.CI ? [['github'], ['html', { open: 'never' }]] : 'list',
+
+ use: {
+ baseURL: BASE_URL,
+ trace: 'on-first-retry',
+ screenshot: 'only-on-failure',
+ },
+
+ projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
+
+ // Starts the dashboard for the run and reuses one you already have open.
+ // The API is expected to be running separately (`spree dev`).
+ webServer: process.env.E2E_BASE_URL
+ ? undefined
+ : {
+ command: 'pnpm dev --port 5174 --strictPort',
+ url: BASE_URL,
+ reuseExistingServer: true,
+ timeout: 60_000,
+ },
+})
diff --git a/packages/seller-dashboard-starter/vitest.config.ts b/packages/seller-dashboard-starter/vitest.config.ts
new file mode 100644
index 0000000000..7177e2bfb2
--- /dev/null
+++ b/packages/seller-dashboard-starter/vitest.config.ts
@@ -0,0 +1,14 @@
+import { defineConfig } from 'vitest/config'
+
+export default defineConfig({
+ test: {
+ globals: true,
+ // Node, not jsdom: what is worth unit-testing here is the logic between
+ // your UI and the API — query keys, payload mapping, permission
+ // predicates. Rendering a component to assert its markup tests React.
+ // Reach for a browser through Playwright instead (`pnpm test:e2e`).
+ environment: 'node',
+ include: ['src/**/*.{test,spec}.{ts,tsx}'],
+ exclude: ['node_modules/**', 'e2e/**', 'dist/**'],
+ },
+})
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 1694bfdd0f..246d074641 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -493,6 +493,9 @@ importers:
'@biomejs/biome':
specifier: ^2.4.14
version: 2.4.14
+ '@playwright/test':
+ specifier: ^1.59.1
+ version: 1.59.1
'@spree/dashboard-plugin-example':
specifier: workspace:^
version: link:../dashboard-plugin-example
@@ -511,6 +514,9 @@ importers:
vite:
specifier: ^8.0.16
+ vitest:
+ specifier: ^4.1.11
packages/dashboard-ui:
dependencies:
@@ -787,6 +793,9 @@ importers:
'@biomejs/biome':
specifier: ^2.4.14
version: 2.4.14
+ '@playwright/test':
+ specifier: ^1.59.1
+ version: 1.59.1
'@types/react':
specifier: ^19.2.14
version: 19.2.14
@@ -802,6 +811,9 @@ importers:
vite:
specifier: ^8.0.16
+ vitest:
+ specifier: ^4.1.11
packages/seller-sdk:
devDependencies: