-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ci: add tests * chore: remove vitest imports docs: update contributing guide * chore: update node adapter test message * chore: update contributing guide * chore: use `existsSync` and `readFileSync` in test * chore: rollback changes in package.json files + fix lint * chore: add `preview-node` script to example * chore: update pnpm to 8.4.0 * ci: add client tests (#50) * ci: add client tests * chore: use `Promise.race` when awaiting sw registration * docs: add installing Playwright Chromium browser hint before running tests * chore: update to pnpm 8.5.0 * chore: update browser test description * chore: remove Vitest globals (IntelliJ complains) * chrore: update vitest and lock file
- Loading branch information
Showing
13 changed files
with
561 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,3 @@ | ||
{ | ||
"extends": ["@antfu"], | ||
"overrides": [ | ||
{ | ||
"files": [ | ||
"**/*.md/*.*" | ||
], | ||
"rules": { | ||
"@typescript-eslint/no-this-alias": "off", | ||
"n/handle-callback-err": "off", | ||
"no-restricted-syntax": "off", | ||
"no-labels": "off" | ||
} | ||
} | ||
] | ||
"extends": ["@antfu"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ dist | |
dev-dist | ||
# intellij stuff | ||
.idea/ | ||
# routify | ||
.routify/ | ||
/test-results/ | ||
/playwright-report/ | ||
/playwright/.cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import AdapterNode from '@sveltejs/adapter-node'; | ||
import AdpaterStatic from '@sveltejs/adapter-static'; | ||
|
||
export const nodeAdapter = process.env.NODE_ADAPTER === 'true' | ||
|
||
export const adapter = nodeAdapter ? AdapterNode() : AdpaterStatic() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {test, expect} from '@playwright/test'; | ||
import {generateSW} from "../pwa.mjs"; | ||
|
||
test('The service worker is registered and cache storage is present', async ({ page}) => { | ||
await page.goto('/'); | ||
|
||
const swURL = await page.evaluate(async () => { | ||
const registration = await Promise.race([ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
navigator.serviceWorker.ready, | ||
new Promise((_, reject) => setTimeout(() => reject(new Error('Service worker registration failed: time out')), 10000)) | ||
]) | ||
return registration.active?.scriptURL | ||
}); | ||
const swName = generateSW ? 'sw.js' : 'prompt-sw.js' | ||
expect(swURL).toBe(`http://localhost:4173/${swName}`); | ||
|
||
const cacheContents = await page.evaluate(async () => { | ||
const cacheState: Record<string, Array<string>> = {}; | ||
for (const cacheName of await caches.keys()) { | ||
const cache = await caches.open(cacheName); | ||
cacheState[cacheName] = (await cache.keys()).map((req) => req.url); | ||
} | ||
return cacheState; | ||
}); | ||
|
||
expect(Object.keys(cacheContents).length).toEqual(1) | ||
|
||
const key = 'workbox-precache-v2-http://localhost:4173/' | ||
|
||
expect(Object.keys(cacheContents)[0]).toEqual(key) | ||
|
||
const urls = cacheContents[key].map(url => url.slice('http://localhost:4173/'.length)) | ||
|
||
/* | ||
'http://localhost:4173/about?__WB_REVISION__=38251751d310c9b683a1426c22c135a2', | ||
'http://localhost:4173/?__WB_REVISION__=073370aa3804305a787b01180cd6b8aa', | ||
'http://localhost:4173/manifest.webmanifest?__WB_REVISION__=27df2fa4f35d014b42361148a2207da3' | ||
*/ | ||
expect(urls.some(url => url.startsWith('manifest.webmanifest?__WB_REVISION__='))).toEqual(true) | ||
expect(urls.some(url => url.startsWith('?__WB_REVISION__='))).toEqual(true) | ||
expect(urls.some(url => url.startsWith('about?__WB_REVISION__='))).toEqual(true) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { defineConfig, devices } from '@playwright/test' | ||
|
||
const url = 'http://localhost:4173' | ||
|
||
import { nodeAdapter } from './adapter.mjs' | ||
|
||
/** | ||
* Read environment variables from file. | ||
* https://github.com/motdotla/dotenv | ||
*/ | ||
// require('dotenv').config(); | ||
|
||
/** | ||
* See https://playwright.dev/docs/test-configuration. | ||
*/ | ||
export default defineConfig({ | ||
testDir: './client-test', | ||
/* Folder for test artifacts such as screenshots, videos, traces, etc. */ | ||
outputDir: 'test-results/', | ||
timeout: 5 * 1000, | ||
expect: { | ||
/** | ||
* Maximum time expect() should wait for the condition to be met. | ||
* For example in `await expect(locator).toHaveText();` | ||
*/ | ||
timeout: 1000, | ||
}, | ||
/* Run tests in files in parallel */ | ||
fullyParallel: true, | ||
/* Fail the build on CI if you accidentally left test.only in the source code. */ | ||
forbidOnly: !!process.env.CI, | ||
/* Retry on CI only */ | ||
retries: 0, | ||
/* Opt out of parallel tests on CI. */ | ||
workers: process.env.CI ? 1 : undefined, | ||
/* Reporter to use. See https://playwright.dev/docs/test-reporters */ | ||
reporter: 'line', | ||
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ | ||
use: { | ||
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */ | ||
actionTimeout: 0, | ||
/* Base URL to use in actions like `await page.goto('/')`. */ | ||
baseURL: url, | ||
|
||
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ | ||
trace: 'on-first-retry', | ||
}, | ||
|
||
/* Configure projects for major browsers */ | ||
projects: [ | ||
{ | ||
name: 'chromium', | ||
use: { ...devices['Desktop Chrome'] }, | ||
}, | ||
|
||
// { | ||
// name: 'firefox', | ||
// use: { ...devices['Desktop Firefox'] }, | ||
// }, | ||
|
||
// { | ||
// name: 'webkit', | ||
// use: { ...devices['Desktop Safari'] }, | ||
// }, | ||
|
||
/* Test against mobile viewports. */ | ||
// { | ||
// name: 'Mobile Chrome', | ||
// use: { ...devices['Pixel 5'] }, | ||
// }, | ||
// { | ||
// name: 'Mobile Safari', | ||
// use: { ...devices['iPhone 12'] }, | ||
// }, | ||
|
||
/* Test against branded browsers. */ | ||
// { | ||
// name: 'Microsoft Edge', | ||
// use: { ...devices['Desktop Edge'], channel: 'msedge' }, | ||
// }, | ||
// { | ||
// name: 'Google Chrome', | ||
// use: { ..devices['Desktop Chrome'], channel: 'chrome' }, | ||
// }, | ||
], | ||
|
||
/* Run your local dev server before starting the tests */ | ||
webServer: { | ||
command: nodeAdapter ? 'pnpm run preview-node' : 'pnpm run preview', | ||
url, | ||
reuseExistingServer: !process.env.CI, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export const generateSW = true; | ||
export const generateSW = process.env.GENERATE_SW === 'true' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { existsSync, readFileSync } from 'node:fs' | ||
import { generateSW } from '../pwa.mjs' | ||
import { nodeAdapter } from '../adapter.mjs' | ||
|
||
describe(`test-build: ${nodeAdapter ? 'node' : 'static'} adapter`, () => { | ||
it(`service worker is generated: ${generateSW ? 'sw.js' : 'prompt-sw.js'}`, () => { | ||
const swName = `./build/${nodeAdapter ? 'client/': ''}${generateSW ? 'sw.js' : 'prompt-sw.js'}` | ||
expect(existsSync(swName), `${swName} doesn't exist`).toBeTruthy() | ||
const webManifest = `./build/${nodeAdapter ? 'client/': ''}manifest.webmanifest` | ||
expect(existsSync(webManifest), `${webManifest} doesn't exist`).toBeTruthy() | ||
const swContent = readFileSync(swName, 'utf-8') | ||
let match: RegExpMatchArray | null | ||
if (generateSW) { | ||
match = swContent.match(/define\(\['\.\/(workbox-\w+)'/) | ||
expect(match && match.length === 2, `workbox-***.js entry not found in ${swName}`).toBeTruthy() | ||
const workboxName = `./build/${nodeAdapter ? 'client/': ''}${match?.[1]}.js` | ||
expect(existsSync(workboxName),`${workboxName} doesn't exist`).toBeTruthy() | ||
} | ||
match = swContent.match(/"url":\s*"manifest\.webmanifest"/) | ||
expect(match && match.length === 1, 'missing manifest.webmanifest in sw precache manifest').toBeTruthy() | ||
match = swContent.match(/"url":\s*"\/"/) | ||
expect(match && match.length === 1, 'missing entry point route (/) in sw precache manifest').toBeTruthy() | ||
match = swContent.match(/"url":\s*"about"/) | ||
expect(match && match.length === 1,'missing about route (/about) in sw precache manifest').toBeTruthy() | ||
if (nodeAdapter) { | ||
match = swContent.match(/"url":\s*"server\//) | ||
expect(match === null, 'found server/ entries in sw precache manifest').toBeTruthy() | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
test: { | ||
include: ['test/*.test.ts'] | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.