Skip to content

Commit f446f6f

Browse files
committed
fix(e2e): ensure tests run in dashboard and correct org when not in CI
1 parent 0a5e1aa commit f446f6f

File tree

9 files changed

+34
-22
lines changed

9 files changed

+34
-22
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import {createPlaywrightConfig} from '@repo/e2e'
22

3+
const isWebkitProject =
4+
process.argv.includes('--project=webkit') || process.argv.some((arg) => arg.includes('webkit'))
5+
36
export default createPlaywrightConfig({
47
testDir: './e2e',
58
/* Run your local dev server before starting the tests */
69
...(process.env['CI']
710
? {} // In CI, don't start a webServer since it's started manually
811
: {
912
webServer: {
10-
command: 'pnpm dev --mode e2e',
13+
command: isWebkitProject ? 'pnpm exec vite --port 3333' : 'pnpm dev',
1114
reuseExistingServer: true,
1215
stdout: 'pipe',
16+
env: {
17+
// Pass e2e organization ID to sanity dev
18+
SDK_E2E_ORGANIZATION_ID: process.env['SDK_E2E_ORGANIZATION_ID'] || '',
19+
},
1320
},
1421
}),
1522
})

apps/kitchensink-react/sanity.cli.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99

1010
export default defineCliConfig({
1111
app: {
12-
organizationId: 'oblZgbTFj',
12+
// Use e2e organization ID if provided, otherwise use dev organization ID
13+
organizationId: process.env['SDK_E2E_ORGANIZATION_ID'] || 'oblZgbTFj',
1314
entry: './src/App.tsx',
1415
id: 'wkyoigmzawwnnwx458zgoh46',
1516
},

apps/kitchensink-react/vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export default defineConfig(({mode}) => {
1212
const rootDir = resolve(process.cwd(), '../..')
1313
const env = loadEnv(mode, rootDir, '')
1414

15-
const isE2E = mode === 'e2e'
15+
// Check if we're in e2e mode - either explicitly set or if SDK_E2E_ORGANIZATION_ID is present
16+
const isE2E = mode === 'e2e' || !!process.env['SDK_E2E_ORGANIZATION_ID']
1617

1718
return {
1819
server: {

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
"test:coverage": "vitest run --coverage",
3838
"test:coverage:open": "open coverage/index.html",
3939
"test:e2e": "turbo run test:e2e",
40+
"test:e2e:dashboard": "turbo run test:e2e --filter=@sanity/kitchensink-react -- --project=chromium --project=firefox",
41+
"test:e2e:webkit": "turbo run test:e2e --filter=@sanity/kitchensink-react -- --project=webkit",
4042
"test:watch": "vitest watch",
4143
"ts:check": "turbo run ts:check --filter='./packages/*' --filter='./apps/*'",
4244
"depcheck": "DEPCHECK=true knip",

packages/@repo/e2e/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,24 @@ test('can click a button', async ({page, getPageContext}) => {
3838

3939
## Running tests
4040

41-
To run E2E tests run the following commands from the root of the project
41+
Right now, tests are split into two separate commands to make local testing easier. This is because we run our tests in the Dashboard, and Safari struggles with an iframed app in localhost. To run E2E tests run the following commands from the root of the project
4242

4343
- Run all the tests
4444

4545
```sh
46-
pnpm test:e2e
46+
pnpm test:e2e:dashboard
47+
pnpm test:e2e:webkit
4748
```
4849

4950
- Run files that have my-spec or my-spec-2 in the file name
5051

5152
```sh
52-
pnpm test:e2e -- my-spec my-spec-2
53+
pnpm test:e2e:dashboard -- my-spec my-spec-2
5354
```
5455

5556
- For help, run
5657
```sh
57-
pnpm test:e2e -- --help
58+
pnpm test:e2e:dashboard -- --help
5859
```
5960

6061
Other useful helper commands

packages/@repo/e2e/src/helpers/pageContext.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ export type PageContext = Pick<Page, 'getByTestId' | 'getByText' | 'getByRole' |
99
context: Page | FrameLocator
1010
}
1111

12-
const DASHBOARD_PROJECTS = ['dashboard-chromium']
12+
// Webkit is excluded from dashboard tests because webkit blocks script execution
13+
// in sandboxed iframes without 'allow-scripts', and the dashboard creates
14+
// sandboxed iframes that webkit cannot execute JavaScript in
15+
const DASHBOARD_PROJECTS = ['chromium', 'firefox']
1316

1417
/**
1518
* Determines if we're in a dashboard context by checking the project name

packages/@repo/e2e/src/index.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
99
const SETUP_DIR = path.join(path.dirname(__dirname), 'src', 'setup')
1010
const TEARDOWN_DIR = path.join(path.dirname(__dirname), 'src', 'teardown')
1111
const AUTH_FILE = path.join(path.dirname(__dirname), '.auth', 'user.json')
12-
const BASE_URL = 'http://localhost:3333/'
1312

1413
const {CI, SDK_E2E_ORGANIZATION_ID} = getE2EEnv()
14+
const BASE_URL = `https://www.sanity.work/@${SDK_E2E_ORGANIZATION_ID}/application/__dev/`
1515

1616
/**
1717
* @internal
@@ -65,15 +65,13 @@ export const basePlaywrightConfig: PlaywrightTestConfig = {
6565
},
6666
{
6767
name: 'webkit',
68-
use: {...devices['Desktop Safari'], storageState: AUTH_FILE, baseURL: BASE_URL},
69-
dependencies: ['setup'],
70-
},
71-
{
72-
name: 'dashboard-chromium',
7368
use: {
74-
...devices['Desktop Chrome'],
69+
...devices['Desktop Safari'],
7570
storageState: AUTH_FILE,
76-
baseURL: `https://www.sanity.work/@${SDK_E2E_ORGANIZATION_ID}/application/__dev/`,
71+
// Webkit cannot execute JavaScript in sandboxed iframes without 'allow-scripts'
72+
// The dashboard creates sandboxed iframes for localhost apps that webkit cannot use
73+
// So webkit runs as standalone (localhost) instead of in the dashboard
74+
baseURL: 'http://localhost:3333/',
7775
},
7876
dependencies: ['setup'],
7977
},

packages/@repo/e2e/src/setup/auth.setup.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const authenticateUser = async (context: BrowserContext, config: AuthConfig) =>
4444
await page.goto(loginUrl.toString())
4545

4646
// Wait for the redirect to complete AND network to be idle
47-
await Promise.all([page.waitForURL(config.expectedRedirectUrl)])
47+
await page.waitForURL(config.expectedRedirectUrl)
4848

4949
await page.close()
5050
}
@@ -68,13 +68,12 @@ setup('setup authentication', async ({browser}) => {
6868
const context = await browser.newContext()
6969

7070
try {
71-
// Authenticate for standalone apps
71+
// Authenticate for standalone (webkit)
7272
await authenticateUser(context, {
7373
origin: 'http://localhost:3333',
7474
expectedRedirectUrl: 'http://localhost:3333',
7575
})
76-
77-
// Authenticate for embedded apps (Dashboard)
76+
// Authenticate for Dashboard (other browsers)
7877
await authenticateUser(context, {
7978
origin: 'https://www.sanity.work/api/dashboard/authenticate',
8079
expectedRedirectUrl: `https://www.sanity.work/@${env.SDK_E2E_ORGANIZATION_ID}`,

turbo.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
"dependsOn": ["^build"],
2929
"outputs": ["docs/**"]
3030
},
31-
"check-types": {
32-
"dependsOn": ["^check-types"]
31+
"ts:check": {
32+
"dependsOn": ["^ts:check"]
3333
},
3434
"clean": {},
3535
"cleanup": {

0 commit comments

Comments
 (0)