Skip to content

Commit 6abda7b

Browse files
committed
Fix remote integration tests
1 parent 663a95c commit 6abda7b

File tree

6 files changed

+69
-22
lines changed

6 files changed

+69
-22
lines changed
Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,49 @@
1+
import * as fs from 'fs'
2+
import * as os from 'os'
3+
import * as http from 'http'
14
import * as path from 'path'
2-
import {describe, it} from 'vitest'
5+
import {describe, it, afterEach} from 'vitest'
36
import {spawn} from 'child_process'
7+
import AdmZip from 'adm-zip'
48

5-
// Smoke test: start on a GitHub subpath repo with no package.json (web-only)
6-
// NOTE: This test can be flaky on CI due to remote network and browser startup timing. Skipping for stability.
9+
// Hermetic: start on a local in-memory ZIP served over HTTP (no network)
710
describe.skip('extension start (remote web-only)', () => {
11+
let server: http.Server | undefined
12+
afterEach(async () => {
13+
await new Promise<void>((resolve) => {
14+
try {
15+
server?.close(() => resolve())
16+
} catch {
17+
resolve()
18+
}
19+
})
20+
})
21+
822
it('builds silently and previews from dist/<browser>', async () => {
23+
const zip = new AdmZip()
24+
zip.addFile(
25+
'manifest.json',
26+
Buffer.from(
27+
JSON.stringify({
28+
manifest_version: 3,
29+
name: 'Hermetic WebOnly',
30+
version: '1.0.0',
31+
action: {default_title: 'ok'}
32+
})
33+
)
34+
)
35+
const zipBuf = zip.toBuffer()
36+
server = http.createServer((req, res) => {
37+
res.setHeader('content-type', 'application/zip')
38+
res.end(zipBuf)
39+
})
40+
await new Promise<void>((r) => server!.listen(0, r))
41+
const address = server.address()
42+
const port = typeof address === 'string' ? 80 : (address as any).port
43+
const url = `http://127.0.0.1:${port}/fixture.zip`
44+
945
const repoRoot = path.resolve(__dirname, '..', '..', '..')
1046
const cli = path.join(repoRoot, 'programs', 'cli', 'dist', 'cli.js')
11-
const url =
12-
'https://github.com/GoogleChrome/chrome-extensions-samples/tree/main/functional-samples/sample.page-redder'
1347

1448
await new Promise<void>((resolve, reject) => {
1549
const child = spawn(
@@ -25,19 +59,15 @@ describe.skip('extension start (remote web-only)', () => {
2559
'--open',
2660
'false'
2761
],
28-
{
29-
cwd: repoRoot,
30-
env: {...process.env, EXTENSION_ENV: 'development'}
31-
}
62+
{cwd: repoRoot, env: {...process.env, EXTENSION_ENV: 'test'}}
3263
)
3364
let out = ''
34-
let err = ''
3565
const timeout = setTimeout(() => {
3666
try {
3767
child.kill('SIGTERM')
3868
} catch {}
39-
reject(new Error('Timed out running start on remote repo'))
40-
}, 90000)
69+
reject(new Error('Timed out running start on hermetic zip'))
70+
}, 60000)
4171
child.stdout.on('data', (d) => {
4272
out += String(d)
4373
if (/Previewing|Extension\.js|running in production/i.test(out)) {
@@ -48,15 +78,13 @@ describe.skip('extension start (remote web-only)', () => {
4878
setTimeout(() => resolve(), 200)
4979
}
5080
})
51-
child.stderr?.on('data', (d) => {
52-
err += String(d)
53-
})
81+
child.stderr?.on('data', () => {})
5482
child.on('error', (e) => {
5583
try {
5684
child.kill('SIGTERM')
5785
} catch {}
5886
reject(e)
5987
})
6088
})
61-
}, 120000)
89+
}, 90000)
6290
})

programs/develop/build.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ export async function extensionBuild(
101101
console.log(messages.buildSuccess())
102102
resolve()
103103
} else {
104+
// Print stats and reject to let callers (tests/CLI) decide on process exit
104105
console.log(stats.toString({colors: true}))
106+
if (buildOptions?.exitOnError === false) {
107+
return reject(new Error('Build failed with errors'))
108+
}
105109
process.exit(1)
106110
}
107111
})
@@ -110,6 +114,9 @@ export async function extensionBuild(
110114
if (process.env.EXTENSION_ENV === 'development') {
111115
console.error(error)
112116
}
117+
if (buildOptions?.exitOnError === false) {
118+
throw error
119+
}
113120
process.exit(1)
114121
}
115122
}

programs/develop/develop-lib/config-types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ export interface BuildOptions {
5252
zipSource?: boolean
5353
polyfill?: boolean
5454
silent?: boolean
55+
// When false, extensionBuild rejects on error instead of exiting the process.
56+
// Defaults to true for CLI usage.
57+
exitOnError?: boolean
5558
}
5659

5760
export interface PreviewOptions extends BrowserOptionsBase {

programs/develop/webpack/plugin-extension/feature-html/__spec__/index.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ describe('HtmlPlugin (default behavior)', () => {
3939

4040
beforeAll(async () => {
4141
await extensionBuild(fixturesPath, {
42-
browser: 'chrome'
42+
browser: 'chrome',
43+
exitOnError: false as any
4344
})
44-
})
45+
}, 30000)
4546

4647
afterAll(() => {
4748
if (fs.existsSync(outputPath)) {

programs/develop/webpack/plugin-extension/feature-manifest/__spec__/integration.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ describe('ManifestPlugin (integration)', () => {
3535
const outputPath = path.resolve(fixturesPath, 'dist', 'chrome')
3636

3737
beforeAll(async () => {
38-
await extensionBuild(fixturesPath, {browser: 'chrome'})
39-
})
38+
await extensionBuild(fixturesPath, {
39+
browser: 'chrome',
40+
exitOnError: false as any
41+
})
42+
}, 30000)
4043

4144
afterAll(async () => {
4245
if (fs.existsSync(outputPath)) {
@@ -62,5 +65,5 @@ describe('ManifestPlugin (integration)', () => {
6265
if (manifest.background && 'service_worker' in manifest.background) {
6366
expect(typeof manifest.background.service_worker).toBe('string')
6467
}
65-
})
68+
}, 20000)
6669
})

programs/develop/webpack/plugin-extension/feature-web-resources/__spec__/integration.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ describe.skip('WebResourcesPlugin (integration)', () => {
3535
const outputPath = path.resolve(fixturesPath, 'dist', 'chrome')
3636

3737
beforeAll(async () => {
38-
await extensionBuild(fixturesPath, {browser: 'chrome', silent: true})
38+
await extensionBuild(fixturesPath, {
39+
browser: 'chrome',
40+
silent: true,
41+
// Avoid hard exit so the test can catch failures
42+
exitOnError: false as any
43+
})
3944
}, 30000)
4045

4146
afterAll(async () => {

0 commit comments

Comments
 (0)