Skip to content

Commit 3969a62

Browse files
authored
Add test config, README, and setup files (#2352)
1 parent ff57b8a commit 3969a62

10 files changed

Lines changed: 195 additions & 12 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ dist
88
.catalyst
99
.env
1010
.env*.local
11+
.env*.test
1112
test-results/
1213
playwright-report/
1314
playwright/.cache/
15+
.tests
1416
bigcommerce.graphql
1517
bigcommerce-graphql.d.ts
1618
.DS_Store

core/.env.test.example

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# This file should hold any environment variables specific to test execution.
2+
# Any overlapping variables in here will overwrite variables from the other .env files.
3+
# For example, if BIGCOMMERCE_CHANNEL_ID=1 in .env.local, but BIGCOMMERCE_CHANNEL_ID=5 in this file, the test environment will use BIGCOMMERCE_CHANNEL_ID=5
4+
5+
# Access token client ID and client secret are required for JWT login tests. If not provided, tests that require these will be skipped.
6+
BIGCOMMERCE_CLIENT_ID=
7+
BIGCOMMERCE_CLIENT_SECRET=
8+
9+
# Base URL for all tests. This should always be set. It is recommended to run tests against a production build.
10+
# If not set or undefined, it will default to 'http://localhost:3000'
11+
PLAYWRIGHT_TEST_BASE_URL=http://localhost:3000
12+
13+
# If true, playwright will automatically start the Catalyst NextJS server before running tests.
14+
PLAYWRIGHT_START_WEBSERVER=false
15+
PLAYWRIGHT_START_WEBSERVER_COMMAND=pnpm start
16+
17+
# If true, no tests will execute that require modifying/deleting any data on your store.
18+
# This is particularly useful if you want to run the tests against a live store and avoid test data being visible to any customers.
19+
# If this is false, you should only run tests against a staging store where test data is okay.
20+
TESTS_READ_ONLY=false
21+
22+
# Locale to use during test execution
23+
TESTS_LOCALE=en
24+
25+
# The fallback locale to use in the occurence of missing translation keys in the selected TESTS_LOCALE
26+
# This should match the default locale you have set for your storefront
27+
TESTS_FALLBACK_LOCALE=en
28+
29+
# An existing customer account to use for all tests - use this if you don't want tests to create a new customer account.
30+
# If TESTS_READ_ONLY is `true`, then any test requiring a customer will be skipped if these values are not set.
31+
TEST_CUSTOMER_ID=1
32+
TEST_CUSTOMER_EMAIL=example@test.com
33+
TEST_CUSTOMER_PASSWORD=Password1
34+
35+
# The default product ID to use for tests. This needs to be a product without variants (simple)
36+
DEFAULT_PRODUCT_ID=111

core/.eslintrc.cjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,31 @@ const config = {
6262
},
6363
],
6464
},
65+
overrides: [
66+
{
67+
files: ['**/*.spec.ts', '**/*.test.ts'],
68+
rules: {
69+
'@typescript-eslint/no-restricted-imports': [
70+
'error',
71+
{
72+
paths: [
73+
{
74+
name: 'next-intl/server',
75+
importNames: ['getTranslations', 'getFormatter'],
76+
message:
77+
'Please import `getTranslations` from `~/tests/lib/i18n` and `getFormatter` from `~/tests/lib/formatter` instead.',
78+
},
79+
],
80+
},
81+
],
82+
},
83+
},
84+
],
6585
ignorePatterns: [
6686
'client/generated/**/*.ts',
6787
'playwright-report/**',
6888
'test-results/**',
89+
'.tests/**',
6990
'**/google_analytics4.js',
7091
],
7192
};

core/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/test-results/
1111
/playwright-report/
1212
/playwright/.cache/
13+
/.tests
1314

1415
# next.js
1516
/.next/

core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@radix-ui/react-toggle": "^1.1.3",
3232
"@radix-ui/react-toggle-group": "^1.1.3",
3333
"@radix-ui/react-tooltip": "^1.2.0",
34+
"@t3-oss/env-core": "^0.13.6",
3435
"@upstash/redis": "^1.34.7",
3536
"@vercel/analytics": "^1.5.0",
3637
"@vercel/kv": "^3.0.0",
@@ -64,7 +65,7 @@
6465
"sonner": "^1.7.4",
6566
"tailwindcss-radix": "^4.0.2",
6667
"uuid": "^11.1.0",
67-
"zod": "^3.24.2"
68+
"zod": "^3.25.48"
6869
},
6970
"devDependencies": {
7071
"@0no-co/graphqlsp": "^1.12.16",

core/playwright.config.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
import { defineConfig, devices } from '@playwright/test';
2-
import { config } from 'dotenv';
32

4-
config();
3+
import { testEnv } from '~/tests/environment';
54

65
export default defineConfig({
76
testDir: './tests',
7+
outputDir: './.tests/test-results',
8+
workers: 1, // TODO: Implement parallel workers in the future
89
expect: {
910
toHaveScreenshot: {
1011
maxDiffPixelRatio: 0.02,
1112
},
1213
},
13-
reporter: [['list'], ['html']],
14+
reporter: [
15+
['list', { outputFolder: './.tests/reports/list' }],
16+
['html', { outputFolder: './.tests/reports/html' }],
17+
],
1418
use: {
15-
baseURL: process.env.PLAYWRIGHT_TEST_BASE_URL,
19+
locale: testEnv.TESTS_LOCALE,
20+
baseURL: testEnv.PLAYWRIGHT_TEST_BASE_URL,
1621
screenshot: 'only-on-failure',
1722
video: 'retain-on-failure',
1823
trace: 'retain-on-failure',
1924
extraHTTPHeaders: {
20-
'x-vercel-protection-bypass': process.env.VERCEL_PROTECTION_BYPASS || '',
21-
'x-vercel-set-bypass-cookie': process.env.CI ? 'true' : 'false',
25+
'x-vercel-protection-bypass': testEnv.VERCEL_PROTECTION_BYPASS,
26+
'x-vercel-set-bypass-cookie': testEnv.CI.toString(),
2227
},
2328
},
2429
projects: [

core/tests/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Testing your Catalyst storefront
2+
3+
This document provides an overview of how to run tests for your Catalyst storefront. This includes the requirements, environment setup, folder structure, and best practices. This README assumes that you already have a Catalyst storefront set up and all of the dependencies installed.
4+
5+
## Table of Contents
6+
- [Prerequisites](#prerequisites)
7+
- [Environment setup](#environment-setup)
8+
- [Running tests](#running-tests)
9+
10+
## Prerequisites
11+
- Ensure that you have already installed and set up Playwright. (See the [Playwright documentation](https://playwright.dev/docs/intro) for more information.)
12+
- **Recommended**: Install the Playwright VSCode extension for easier test management and execution.
13+
14+
## Environment setup
15+
16+
To run tests, you first need to make sure you have the necessary environment setup. You should have already installed the required dependencies and configured your Catalyst environment in `.env.local`.
17+
18+
To begin, copy `.env.test.example` to `.env.test`. The Catalyst test environment merges `.env.local` with `.env.test.local`, so any test-related environment variables should **only** be in `.env.test.local`. This allows you to keep your test configuration separate from your development configuration.
19+
20+
**NOTE:** Any environment variables defined in `.env.test.local` will override those in `.env.local`.
21+
22+
## Running tests
23+
24+
When running tests locally, ensure that you have an active build for your Catalyst storefront by running `pnpm build`. This will ensure that the latest changes are included in the tests. Alternatively, you can use `pnpm dev` to run the development server, but this is not recommended.
25+
26+
When running tests against a live deployment, ensure that you have set the `PLAYWRIGHT_TEST_BASE_URL` environment variable to the URL of your live deployment storefront, and ensure that `TESTS_READ_ONLY` is set accordingly for your needs.
27+
28+
**NOTE:** The examples use `pnpm`, but you can also use `npx` if you prefer. Additionally, you can leverage the Playwright VSCode extension to run your tests.
29+
30+
### CLI commands
31+
To run all of the tests, use the following command:
32+
33+
```bash
34+
pnpm playwright test
35+
```
36+
37+
To run a specific test file, you can specify the path to the test file:
38+
39+
```bash
40+
pnpm playwright test auth/logout.spec.ts
41+
```
42+
43+
To run a specific test within a file, you can use the `-g` flag followed by the name of the test:
44+
45+
```bash
46+
pnpm playwright test auth/logout.spec.ts -g "Logout works as expected"
47+
```
48+
49+
For more information on the available CLI commands, refer to the [Playwright CLI documentation](https://playwright.dev/docs/test-cli).

core/tests/environment.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { createEnv } from '@t3-oss/env-core';
2+
import { config as dotenvConfig } from 'dotenv';
3+
import { z } from 'zod/v4';
4+
5+
import { defaultLocale, locales } from '~/i18n/locales';
6+
7+
dotenvConfig({ path: ['.env', '.env.local', '.env.test'], override: true });
8+
9+
const localeSchema = z.string().refine((val: string) => locales.includes(val), {
10+
error: `TESTS_LOCALE must be one of: ${locales.join(', ')}`,
11+
});
12+
13+
export const testEnv = createEnv({
14+
server: {
15+
BIGCOMMERCE_ADMIN_API_HOST: z.string().optional().default('api.bigcommerce.com'),
16+
BIGCOMMERCE_ACCESS_TOKEN: z.string().optional(),
17+
BIGCOMMERCE_ACCESS_TOKEN_CLIENT_ID: z.string().optional(),
18+
BIGCOMMERCE_ACCESS_TOKEN_CLIENT_SECRET: z.string().optional(),
19+
BIGCOMMERCE_CHANNEL_ID: z.coerce.number().optional(),
20+
BIGCOMMERCE_STORE_HASH: z.string().optional(),
21+
PLAYWRIGHT_TEST_BASE_URL: z.string().optional().default('http://localhost:3000'),
22+
VERCEL_PROTECTION_BYPASS: z.string().optional().default(''),
23+
CI: z.stringbool().optional().default(false),
24+
TESTS_READ_ONLY: z.stringbool().optional().default(false),
25+
TESTS_LOCALE: localeSchema.default(defaultLocale),
26+
TESTS_FALLBACK_LOCALE: localeSchema.default(defaultLocale),
27+
TEST_CUSTOMER_ID: z.coerce.number().optional(),
28+
TEST_CUSTOMER_EMAIL: z.string().optional(),
29+
TEST_CUSTOMER_PASSWORD: z.string().optional(),
30+
DEFAULT_PRODUCT_ID: z.coerce.number().optional(),
31+
},
32+
runtimeEnv: process.env,
33+
emptyStringAsUndefined: true,
34+
});

core/tests/tags.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const TAGS = {
2+
// @writes-data is used to mark tests that modify data on the storefront without directly using the API.
3+
writesData: '@writes-data',
4+
};

pnpm-lock.yaml

Lines changed: 35 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)