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

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.18",
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"
@@ -15,6 +16,7 @@
15
16
  "dependencies": {
16
17
  "@playwright/test": "^1.25.0",
17
18
  "get-port": "^6.1.2",
19
+ "minimist": "^1.2.6",
18
20
  "read-pkg-up": "^9.1.0"
19
21
  }
20
22
  }
package/src/all.js CHANGED
@@ -1,36 +1,108 @@
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
+ import parseArgv from 'minimist'
6
7
 
8
+ /**
9
+ * @param {string} name
10
+ */
11
+ const isTestFile = (name) => {
12
+ return !name.startsWith('_')
13
+ }
14
+
15
+ /**
16
+ * @param {string} root
17
+ */
7
18
  const getTestFiles = async (root) => {
8
- return readdirSync(root).map((x) => join(root, x))
19
+ return readdirSync(root)
20
+ .filter(isTestFile)
21
+ .map((x) => join(root, x))
22
+ }
23
+
24
+ /**
25
+ * @param {string} absolutePath
26
+ * @param {number} port
27
+ */
28
+ const getUrlFromTestFile = (absolutePath, port) => {
29
+ const baseName = basename(absolutePath)
30
+ const htmlFileName = baseName.replace('.js', '.html')
31
+ return `http://localhost:${port}/tests/${htmlFileName}`
32
+ }
33
+
34
+ /**
35
+ *
36
+ * @param {import('@playwright/test').Page} page
37
+ * @param {string} url
38
+ */
39
+ const executeSingleTest = async (page, url) => {
40
+ await page.goto(url)
41
+ const testOverlay = page.locator('#TestOverlay')
42
+ await expect(testOverlay).toBeVisible({ timeout: 25_000 })
43
+ const text = await testOverlay.textContent()
44
+ const state = await testOverlay.getAttribute('data-state')
45
+ switch (state) {
46
+ case 'pass':
47
+ return {
48
+ status: 'pass',
49
+ }
50
+ case 'skip':
51
+ return {
52
+ status: 'skip',
53
+ }
54
+ case 'fail':
55
+ return {
56
+ status: 'fail',
57
+ error: `${text}`,
58
+ }
59
+ default:
60
+ throw new Error(`unexpected test state: ${state}`)
61
+ }
62
+ }
63
+
64
+ /**
65
+ *
66
+ * @param {any} options
67
+ */
68
+ const getEnv = (options) => {
69
+ const env = Object.create(null)
70
+ if (options['only-extension']) {
71
+ env['ONLY_EXTENSION'] = options['only-extension']
72
+ }
73
+ if (options['test-path']) {
74
+ env['TEST_PATH'] = options['test-path']
75
+ }
76
+ return env
9
77
  }
10
78
 
11
79
  const main = async () => {
12
80
  try {
81
+ const argv = process.argv.slice(2)
82
+ const options = parseArgv(argv)
13
83
  let skipped = 0
14
84
  let passed = 0
85
+ const env = getEnv(options)
15
86
  const start = performance.now()
16
- state.runImmediately = false
17
- await startAll()
18
- console.info('SETUP COMPLETE')
87
+ const { page, port } = await startAll(env)
19
88
  const root = await getRoot()
20
- state.root = root
21
89
  const testFiles = await getTestFiles(join(root, 'src'))
22
90
  console.log({ testFiles })
23
91
  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)
92
+ const url = getUrlFromTestFile(testFile, port)
93
+ const name = basename(testFile)
94
+ const result = await executeSingleTest(page, url)
95
+ switch (result.status) {
96
+ case 'pass':
97
+ console.info(`test passed ${name}`)
32
98
  passed++
33
- }
99
+ break
100
+ case 'skip':
101
+ console.info(`test skipped ${name}`)
102
+ skipped++
103
+ break
104
+ case 'fail':
105
+ throw new Error(`Test Failed ${name}: ${result.error}`)
34
106
  }
35
107
  }
36
108
  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,29 +111,17 @@ 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
- export const startAll = async () => {
114
+ export const startAll = async (env) => {
156
115
  const port = await getPort()
157
116
  await launchServer({
158
117
  port,
159
- env: {},
118
+ env,
160
119
  folder: '',
161
120
  })
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>