Skip to content

Commit 892f0da

Browse files
authored
[Tech] Refactor mock backend (#871)
* refactor mock backend * fix entrypoint.sh * update deps, rm page logs * rm store tests * fix entrypoint build * enable one download test * change to port 8081 * rm store api tests * set playwright config timeout 5 min * pretty * add comment and better error handling * rm reject if any page closes * rm mock backend from submodule merge check
1 parent 1b8014f commit 892f0da

8 files changed

+171
-65
lines changed

.github/workflows/submodules-are-merged.yml

-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,3 @@ jobs:
2929
- name: Check overlay
3030
id: overlayDiff
3131
run: cd src/backend/hyperplay-overlay && echo "OVERLAY_DIFF=$(git log origin/main...HEAD)" >> "$GITHUB_OUTPUT"
32-
- name: Check mock backend
33-
id: mockBackendDiff
34-
run: cd e2e/__mocks__/hyperplay-mock-backend && echo "MOCK_BACKEND_DIFF=$(git log origin/main...HEAD)" >> "$GITHUB_OUTPUT"

e2e/__mocks__/hyperplay-mock-backend

Submodule hyperplay-mock-backend deleted from 833953f

e2e/__specs__/common-setup.ts

+45-23
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const appNameToMock =
2727

2828
export const launchApp = async () => {
2929
process.env.CI = 'e2e'
30-
process.env.MOCK_DOWNLOAD_URL = `http://127.0.0.1:8080/download/kosium`
30+
process.env.MOCK_DOWNLOAD_URL = `http://127.0.0.1:8081/download/kosium`
3131
process.env.APP_NAME_TO_MOCK = appNameToMock
3232
if (process.env.TEST_PACKAGED === 'true') {
3333
console.log('Testing packaged build')
@@ -53,26 +53,26 @@ export const launchApp = async () => {
5353
}
5454

5555
// this pipes the main process std out to test std out
56-
electronApp
57-
.process()
58-
.stdout?.on('data', (data) => console.log(`main process stdout: ${data}`))
59-
electronApp
60-
.process()
61-
.stderr?.on('data', (error) => console.log(`main process stderr: ${error}`))
62-
63-
electronApp.on('window', async (page) => {
64-
const filename = page.url()?.split('/').pop()
65-
console.log(`Window opened: ${filename} page url ${page.url()}`)
66-
67-
// capture errors
68-
page.on('pageerror', (error) => {
69-
console.error(error)
70-
})
71-
// capture console messages
72-
page.on('console', (msg) => {
73-
console.log(msg.text())
74-
})
75-
})
56+
// electronApp
57+
// .process()
58+
// .stdout?.on('data', (data) => console.log(`main process stdout: ${data}`))
59+
// electronApp
60+
// .process()
61+
// .stderr?.on('data', (error) => console.log(`main process stderr: ${error}`))
62+
63+
// electronApp.on('window', async (page) => {
64+
// const filename = page.url()?.split('/').pop()
65+
// console.log(`Window opened: ${filename} page url ${page.url()}`)
66+
67+
// // capture errors
68+
// page.on('pageerror', (error) => {
69+
// console.error(error)
70+
// })
71+
// // capture console messages
72+
// page.on('console', (msg) => {
73+
// console.log(msg.text())
74+
// })
75+
// })
7676

7777
const hpPagePromise = new Promise<Page>((res, rej) => {
7878
async function getPageTitle(page_i: Page) {
@@ -88,6 +88,10 @@ export const launchApp = async () => {
8888
return false
8989
}
9090

91+
/**
92+
* @dev Note that this will throw the following error on the electronApp.close function call
93+
* Error during electronApp.waitForEvent(window): Error: electronApplication.waitForEvent: Target page, context or browser has been closed
94+
*/
9195
electronApp
9296
.waitForEvent('window', {
9397
predicate: async (page_i: Page) => {
@@ -97,7 +101,6 @@ export const launchApp = async () => {
97101
})
98102
.catch((err) => {
99103
console.log(`Error during electronApp.waitForEvent(window): ${err}`)
100-
rej(err)
101104
})
102105

103106
for (const windowPage of electronApp.windows()) {
@@ -115,13 +118,32 @@ export const launchApp = async () => {
115118
console.log('Electron app is ready for testing!')
116119
}
117120

121+
async function launchMockBackend() {
122+
try {
123+
const mockBackend = await import('@hyperplay/mock-backend')
124+
await mockBackend.connectedPromise
125+
} catch (err) {
126+
console.error(`Error launching mock backend for e2e test setup ${err}`)
127+
}
128+
}
129+
118130
export default function setup(timeout = 120000): void {
131+
test.beforeAll(async () => {
132+
await launchMockBackend()
133+
})
134+
119135
test.beforeEach(async () => {
120136
test.setTimeout(timeout)
121137
await launchApp()
122138
})
123139

124140
test.afterEach(async () => {
125-
await electronApp.close()
141+
try {
142+
await electronApp?.close()
143+
} catch (err) {
144+
console.error(
145+
`Error while closing electron app in after each hook ${err}`
146+
)
147+
}
126148
})
127149
}

e2e/__specs__/hpStoreApi.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ test.describe('hp store api tests', function () {
315315
await cancelDownload(true)
316316
})
317317

318-
test('hp store: download, pause, resume, cancel and do not keep files', async () => {
318+
test.skip('hp store: download, pause, resume, cancel and do not keep files', async () => {
319319
// download then pause
320320
console.log('installing')
321321
await withTimeout(installPartialTimeout, installPartial(appName), false)
@@ -327,7 +327,7 @@ test.describe('hp store api tests', function () {
327327
await cancelDownload(true)
328328
})
329329

330-
test('hp store: download, pause, cancel and do not keep files', async () => {
330+
test.skip('hp store: download, pause, cancel and do not keep files', async () => {
331331
// download then pause
332332
console.log('installing')
333333
await withTimeout(installPartialTimeout, installPartial(appName), false)

e2e/entrypoint.sh

+3-8
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,9 @@ then
4444
yarn dist:$os
4545
fi
4646
else
47-
yarn vite build
47+
yarn electron-vite build
4848
fi
4949

50-
# mock backend server
51-
cd e2e/__mocks__/hyperplay-mock-backend
52-
yarn
53-
cd ../../../
54-
5550
echo
5651
echo "########"
5752
echo "# Test #"
@@ -62,8 +57,8 @@ echo
6257
if [[ "$os" == "linux" ]]
6358
then
6459
yarn playwright test .*/api.spec.ts
65-
cd e2e/__mocks__/hyperplay-mock-backend && yarn start & sleep 5 && xvfb-run -a -e /dev/stdout -s "-screen 0 1280x960x24" yarn playwright test .*hpStoreApi.spec.ts
60+
# xvfb-run -a -e /dev/stdout -s "-screen 0 1280x960x24" yarn playwright test .*hpStoreApi.spec.ts
6661
else
6762
yarn playwright test .*/api.spec.ts
68-
cd e2e/__mocks__/hyperplay-mock-backend && yarn start & sleep 5 && yarn playwright test .*hpStoreApi.spec.ts
63+
# yarn playwright test .*hpStoreApi.spec.ts
6964
fi

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@
312312
"@babel/plugin-transform-arrow-functions": "^7.22.5",
313313
"@lavamoat/allow-scripts": "^3.0.2",
314314
"@lavamoat/preinstall-always-fail": "^2.0.0",
315-
"@playwright/test": "^1.32.1",
315+
"@playwright/test": "^1.43.1",
316316
"@testing-library/dom": "^7.31.0",
317317
"@testing-library/jest-dom": "^6.2.0",
318318
"@testing-library/react": "^14.0.0",
@@ -337,7 +337,7 @@
337337
"cross-env": "^7.0.3",
338338
"electron": "^28.0.0",
339339
"electron-builder": "^24.13.3",
340-
"electron-playwright-helpers": "^1.6.0",
340+
"electron-playwright-helpers": "^1.7.1",
341341
"electron-vite": "^2.1.0",
342342
"eslint": "^8.13.0",
343343
"eslint-config-prettier": "^8.5.0",
@@ -348,7 +348,7 @@
348348
"i18next-parser": "^6.3.0",
349349
"jest": "^29.7.0",
350350
"pkg": "^5.8.1",
351-
"playwright": "^1.32.1",
351+
"playwright": "^1.43.1",
352352
"prettier": "^2.6.2",
353353
"pretty-quick": "^3.1.3",
354354
"sass": "^1.55.0",
@@ -360,7 +360,9 @@
360360
"vite": "^5.2.8",
361361
"vite-plugin-svgr": "^2.2.2"
362362
},
363-
"optionalDependencies": {},
363+
"optionalDependencies": {
364+
"@hyperplay/mock-backend": "^0.0.1"
365+
},
364366
"packageManager": "yarn@1.22.19",
365367
"lavamoat": {
366368
"allowScripts": {

playwright.config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'playwright/test'
2+
3+
export default defineConfig({
4+
testMatch: ['*spec.ts'],
5+
/* Maximum time one test can run for. */
6+
timeout: 5 * 60 * 1000
7+
})

0 commit comments

Comments
 (0)