@lvce-editor/test-with-playwright 0.0.14 → 0.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ import '../src/all.js'
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@lvce-editor/test-with-playwright",
3
- "version": "0.0.14",
3
+ "version": "0.0.16",
4
4
  "description": "",
5
5
  "main": "src/main.js",
6
6
  "types": "src/main.d.ts",
7
7
  "type": "module",
8
+ "bin": "bin/test-with-playwright.js",
8
9
  "repository": {
9
10
  "type": "git",
10
11
  "url": "git@github.com:lvce-editor/test-with-playwright.git"
package/src/all.js CHANGED
@@ -1,36 +1,81 @@
1
+ import { expect } from '@playwright/test'
1
2
  import { readdirSync } from 'node:fs'
2
- import { join } from 'node:path'
3
+ import { basename, join } from 'node:path'
3
4
  import { performance } from 'node:perf_hooks'
4
- import { pathToFileURL } from 'node:url'
5
5
  import { closeAll, getRoot, runTest, startAll, state } from './main.js'
6
6
 
7
+ /**
8
+ * @param {string} root
9
+ */
7
10
  const getTestFiles = async (root) => {
8
11
  return readdirSync(root).map((x) => join(root, x))
9
12
  }
10
13
 
14
+ /**
15
+ * @param {string} absolutePath
16
+ * @param {number} port
17
+ */
18
+ const getUrlFromTestFile = (absolutePath, port) => {
19
+ const baseName = basename(absolutePath)
20
+ const htmlFileName = baseName.replace('.js', '.html')
21
+ return `http://localhost:${port}/tests/${htmlFileName}`
22
+ }
23
+
24
+ /**
25
+ *
26
+ * @param {import('@playwright/test').Page} page
27
+ * @param {string} url
28
+ */
29
+ const executeSingleTest = async (page, url) => {
30
+ await page.goto(url)
31
+ const testOverlay = page.locator('#TestOverlay')
32
+ await expect(testOverlay).toBeVisible({ timeout: 25_000 })
33
+ const text = await testOverlay.textContent()
34
+ const state = await testOverlay.getAttribute('data-state')
35
+ switch (state) {
36
+ case 'pass':
37
+ return {
38
+ status: 'pass',
39
+ }
40
+ case 'skip':
41
+ return {
42
+ status: 'skip',
43
+ }
44
+ case 'fail':
45
+ return {
46
+ status: 'fail',
47
+ error: `${text}`,
48
+ }
49
+ default:
50
+ throw new Error(`unexpected test state: ${state}`)
51
+ }
52
+ }
53
+
11
54
  const main = async () => {
12
55
  try {
13
56
  let skipped = 0
14
57
  let passed = 0
15
58
  const start = performance.now()
16
- state.runImmediately = false
17
- await startAll()
59
+ const { page, port } = await startAll()
18
60
  console.info('SETUP COMPLETE')
19
61
  const root = await getRoot()
20
- state.root = root
21
62
  const testFiles = await getTestFiles(join(root, 'src'))
22
63
  console.log({ testFiles })
23
64
  for (const testFile of testFiles) {
24
- state.tests = []
25
- const testUri = pathToFileURL(testFile).toString()
26
- await import(testUri)
27
- for (const test of state.tests) {
28
- if (test.status === 'skipped') {
29
- skipped++
30
- } else {
31
- await runTest(test)
65
+ const url = getUrlFromTestFile(testFile, port)
66
+ const name = basename(testFile)
67
+ const result = await executeSingleTest(page, url)
68
+ switch (result.status) {
69
+ case 'pass':
70
+ console.info(`test passed ${name}`)
32
71
  passed++
33
- }
72
+ break
73
+ case 'skip':
74
+ console.info(`test skipped ${name}`)
75
+ skipped++
76
+ break
77
+ case 'fail':
78
+ throw new Error(`Test Failed ${name}: ${result.error}`)
34
79
  }
35
80
  }
36
81
  const end = performance.now()
package/src/main.js CHANGED
@@ -29,7 +29,6 @@ export const state = {
29
29
  /**
30
30
  * @type{any}
31
31
  */
32
- tests: [],
33
32
  port: 0,
34
33
  root: undefined,
35
34
  }
@@ -47,7 +46,7 @@ const getExtensionFolder = async () => {
47
46
  return join(root, '..', 'extension')
48
47
  }
49
48
 
50
- export const getTmpDir = (prefix='foo-') => {
49
+ export const getTmpDir = (prefix = 'foo-') => {
51
50
  return mkdtemp(join(tmpdir(), prefix))
52
51
  }
53
52
 
@@ -103,34 +102,6 @@ const startBrowser = async ({ port, headless = false }) => {
103
102
  return page
104
103
  }
105
104
 
106
- export const runWithExtension = async ({ folder = '', env = {} }) => {
107
- folder ||= await getTmpDir()
108
- if (state.page && state.childProcess) {
109
- console.info('recycle page')
110
- state.childProcess.send({
111
- jsonrpc: '2.0',
112
- method: 'Platform.setEnvironmentVariables',
113
- params: [
114
- {
115
- FOLDER: folder,
116
- ...env,
117
- },
118
- ],
119
- id: 999999999999,
120
- })
121
- await state.page.goto(`http://localhost:${state.port}`)
122
- return state.page
123
- }
124
- const port = await getPort()
125
- const server = await launchServer({ port, folder, env })
126
- const page = await startBrowser({
127
- headless: false,
128
- port,
129
- })
130
- await page.goto(`http://localhost:${port}`)
131
- return page
132
- }
133
-
134
105
  export const runTest = async ({ name, fn }) => {
135
106
  const start = performance.now()
136
107
  console.info(`[test] running ${name}`)
@@ -140,18 +111,6 @@ export const runTest = async ({ name, fn }) => {
140
111
  console.info(`[test] passed ${name} in ${duration}ms`)
141
112
  }
142
113
 
143
- export const test = async (name, fn) => {
144
- if (state.runImmediately) {
145
- await runTest({ name, fn })
146
- } else {
147
- state.tests.push({ name, fn })
148
- }
149
- }
150
-
151
- test.skip = (name, fn) => {
152
- state.tests.push({ name, fn, status: 'skipped' })
153
- }
154
-
155
114
  export const startAll = async () => {
156
115
  const port = await getPort()
157
116
  await launchServer({
@@ -162,7 +121,7 @@ export const startAll = async () => {
162
121
  state.port = port
163
122
  const headless = process.argv.includes('--headless')
164
123
  const page = await startBrowser({ port, headless })
165
- return page
124
+ return { page, port }
166
125
  }
167
126
 
168
127
  export const closeAll = async () => {
@@ -179,5 +138,3 @@ export const closeAll = async () => {
179
138
  state.browser = undefined
180
139
  }
181
140
  }
182
-
183
- export { expect } from '@playwright/test'
package/src/main.d.ts DELETED
@@ -1,18 +0,0 @@
1
- import type { Page } from '@playwright/test'
2
-
3
- export const runWithExtension: ({
4
- folder,
5
- env,
6
- }: {
7
- folder?: string
8
- env?: any
9
- }) => Promise<Page>
10
-
11
- export const test: {
12
- (name: string, fn: () => Promise<void>): void
13
- skip: (name: string, fn: () => Promise<void>) => void
14
- }
15
-
16
- export { expect } from '@playwright/test'
17
-
18
- export const getTmpDir: (prefix = 'foo-') => Promise<string>