Skip to content

Commit 4380b82

Browse files
authored
test: reduce optimize-deps playground flaky fail (#15205)
1 parent 7fd7c6c commit 4380b82

File tree

2 files changed

+114
-53
lines changed

2 files changed

+114
-53
lines changed

playground/optimize-deps/__tests__/optimize-deps.spec.ts

Lines changed: 94 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, test } from 'vitest'
22
import {
33
browserErrors,
44
browserLogs,
5+
expectWithRetry,
56
getColor,
67
isBuild,
78
isServe,
@@ -12,93 +13,123 @@ import {
1213
} from '~utils'
1314

1415
test('default + named imports from cjs dep (react)', async () => {
15-
expect(await page.textContent('.cjs button')).toBe('count is 0')
16+
await expectWithRetry(() => page.textContent('.cjs button')).toBe(
17+
'count is 0',
18+
)
1619
await page.click('.cjs button')
17-
expect(await page.textContent('.cjs button')).toBe('count is 1')
20+
await expectWithRetry(() => page.textContent('.cjs button')).toBe(
21+
'count is 1',
22+
)
1823
})
1924

2025
test('named imports from webpacked cjs (phoenix)', async () => {
21-
expect(await page.textContent('.cjs-phoenix')).toBe('ok')
26+
await expectWithRetry(() => page.textContent('.cjs-phoenix')).toBe('ok')
2227
})
2328

2429
test('default import from webpacked cjs (clipboard)', async () => {
25-
expect(await page.textContent('.cjs-clipboard')).toBe('ok')
30+
await expectWithRetry(() => page.textContent('.cjs-clipboard')).toBe('ok')
2631
})
2732

2833
test('dynamic imports from cjs dep (react)', async () => {
29-
expect(await page.textContent('.cjs-dynamic button')).toBe('count is 0')
34+
await expectWithRetry(() => page.textContent('.cjs-dynamic button')).toBe(
35+
'count is 0',
36+
)
3037
await page.click('.cjs-dynamic button')
31-
expect(await page.textContent('.cjs-dynamic button')).toBe('count is 1')
38+
await expectWithRetry(() => page.textContent('.cjs-dynamic button')).toBe(
39+
'count is 1',
40+
)
3241
})
3342

3443
test('dynamic named imports from webpacked cjs (phoenix)', async () => {
35-
expect(await page.textContent('.cjs-dynamic-phoenix')).toBe('ok')
44+
await expectWithRetry(() => page.textContent('.cjs-dynamic-phoenix')).toBe(
45+
'ok',
46+
)
3647
})
3748

3849
test('dynamic default import from webpacked cjs (clipboard)', async () => {
39-
expect(await page.textContent('.cjs-dynamic-clipboard')).toBe('ok')
50+
await expectWithRetry(() => page.textContent('.cjs-dynamic-clipboard')).toBe(
51+
'ok',
52+
)
4053
})
4154

4255
test('dynamic default import from cjs (cjs-dynamic-dep-cjs-compiled-from-esm)', async () => {
43-
expect(await page.textContent('.cjs-dynamic-dep-cjs-compiled-from-esm')).toBe(
44-
'ok',
45-
)
56+
await expectWithRetry(() =>
57+
page.textContent('.cjs-dynamic-dep-cjs-compiled-from-esm'),
58+
).toBe('ok')
4659
})
4760

4861
test('dynamic default import from cjs (cjs-dynamic-dep-cjs-compiled-from-cjs)', async () => {
49-
expect(await page.textContent('.cjs-dynamic-dep-cjs-compiled-from-cjs')).toBe(
50-
'ok',
51-
)
62+
await expectWithRetry(() =>
63+
page.textContent('.cjs-dynamic-dep-cjs-compiled-from-cjs'),
64+
).toBe('ok')
5265
})
5366

5467
test('dedupe', async () => {
55-
expect(await page.textContent('.dedupe button')).toBe('count is 0')
68+
await expectWithRetry(() => page.textContent('.dedupe button')).toBe(
69+
'count is 0',
70+
)
5671
await page.click('.dedupe button')
57-
expect(await page.textContent('.dedupe button')).toBe('count is 1')
72+
await expectWithRetry(() => page.textContent('.dedupe button')).toBe(
73+
'count is 1',
74+
)
5875
})
5976

6077
test('cjs browser field (axios)', async () => {
61-
expect(await page.textContent('.cjs-browser-field')).toBe('pong')
78+
await expectWithRetry(() => page.textContent('.cjs-browser-field')).toBe(
79+
'pong',
80+
)
6281
})
6382

6483
test('cjs browser field bare', async () => {
65-
expect(await page.textContent('.cjs-browser-field-bare')).toBe('pong')
84+
await expectWithRetry(() => page.textContent('.cjs-browser-field-bare')).toBe(
85+
'pong',
86+
)
6687
})
6788

6889
test('dep from linked dep (lodash-es)', async () => {
69-
expect(await page.textContent('.deps-linked')).toBe('fooBarBaz')
90+
await expectWithRetry(() => page.textContent('.deps-linked')).toBe(
91+
'fooBarBaz',
92+
)
7093
})
7194

7295
test('forced include', async () => {
73-
expect(await page.textContent('.force-include')).toMatch(`[success]`)
96+
await expectWithRetry(() => page.textContent('.force-include')).toMatch(
97+
`[success]`,
98+
)
7499
})
75100

76101
test('import * from optimized dep', async () => {
77-
expect(await page.textContent('.import-star')).toMatch(`[success]`)
102+
await expectWithRetry(() => page.textContent('.import-star')).toMatch(
103+
`[success]`,
104+
)
78105
})
79106

80107
test('import from dep with process.env.NODE_ENV', async () => {
81-
expect(await page.textContent('.node-env')).toMatch(isBuild ? 'prod' : 'dev')
108+
await expectWithRetry(() => page.textContent('.node-env')).toMatch(
109+
isBuild ? 'prod' : 'dev',
110+
)
82111
})
83112

84113
test('import from dep with .notjs files', async () => {
85-
expect(await page.textContent('.not-js')).toMatch(`[success]`)
114+
await expectWithRetry(() => page.textContent('.not-js')).toMatch(`[success]`)
86115
})
87116

88117
test('Import from dependency which uses relative path which needs to be resolved by main field', async () => {
89-
expect(await page.textContent('.relative-to-main')).toMatch(`[success]`)
118+
await expectWithRetry(() => page.textContent('.relative-to-main')).toMatch(
119+
`[success]`,
120+
)
90121
})
91122

92123
test('dep with dynamic import', async () => {
93-
expect(await page.textContent('.dep-with-dynamic-import')).toMatch(
94-
`[success]`,
95-
)
124+
await expectWithRetry(() =>
125+
page.textContent('.dep-with-dynamic-import'),
126+
).toMatch(`[success]`)
96127
})
97128

98129
test('dep with optional peer dep', async () => {
99-
expect(await page.textContent('.dep-with-optional-peer-dep')).toMatch(
100-
`[success]`,
101-
)
130+
await expectWithRetry(() =>
131+
page.textContent('.dep-with-optional-peer-dep'),
132+
).toMatch(`[success]`)
102133
if (isServe) {
103134
expect(browserErrors.map((error) => error.message)).toEqual(
104135
expect.arrayContaining([
@@ -109,8 +140,8 @@ test('dep with optional peer dep', async () => {
109140
})
110141

111142
test('dep with optional peer dep submodule', async () => {
112-
expect(
113-
await page.textContent('.dep-with-optional-peer-dep-submodule'),
143+
expectWithRetry(() =>
144+
page.textContent('.dep-with-optional-peer-dep-submodule'),
114145
).toMatch(`[success]`)
115146
if (isServe) {
116147
expect(browserErrors.map((error) => error.message)).toEqual(
@@ -122,61 +153,69 @@ test('dep with optional peer dep submodule', async () => {
122153
})
123154

124155
test('dep with css import', async () => {
125-
expect(await getColor('.dep-linked-include')).toBe('red')
156+
await expectWithRetry(() => getColor('.dep-linked-include')).toBe('red')
126157
})
127158

128159
test('CJS dep with css import', async () => {
129-
expect(await getColor('.cjs-with-assets')).toBe('blue')
160+
await expectWithRetry(() => getColor('.cjs-with-assets')).toBe('blue')
130161
})
131162

132163
test('externalize known non-js files in optimize included dep', async () => {
133-
expect(await page.textContent('.externalize-known-non-js')).toMatch(
134-
`[success]`,
135-
)
164+
await expectWithRetry(() =>
165+
page.textContent('.externalize-known-non-js'),
166+
).toMatch(`[success]`)
136167
})
137168

138169
test('vue + vuex', async () => {
139-
expect(await page.textContent('.vue')).toMatch(`[success]`)
170+
await expectWithRetry(() => page.textContent('.vue')).toMatch(`[success]`)
140171
})
141172

142173
// When we use the Rollup CommonJS plugin instead of esbuild prebundling,
143174
// the esbuild plugins won't apply to dependencies
144175
test('esbuild-plugin', async () => {
145-
expect(await page.textContent('.esbuild-plugin')).toMatch(
176+
await expectWithRetry(() => page.textContent('.esbuild-plugin')).toMatch(
146177
`Hello from an esbuild plugin`,
147178
)
148179
})
149180

150181
test('import from hidden dir', async () => {
151-
expect(await page.textContent('.hidden-dir')).toBe('hello!')
182+
await expectWithRetry(() => page.textContent('.hidden-dir')).toBe('hello!')
152183
})
153184

154185
test('import optimize-excluded package that imports optimized-included package', async () => {
155-
expect(await page.textContent('.nested-include')).toBe('nested-include')
186+
await expectWithRetry(() => page.textContent('.nested-include')).toBe(
187+
'nested-include',
188+
)
156189
})
157190

158191
test('import aliased package with colon', async () => {
159-
expect(await page.textContent('.url')).toBe('vitejs.dev')
192+
await expectWithRetry(() => page.textContent('.url')).toBe('vitejs.dev')
160193
})
161194

162195
test('import aliased package using absolute path', async () => {
163-
expect(await page.textContent('.alias-using-absolute-path')).toBe(
164-
'From dep-alias-using-absolute-path',
165-
)
196+
await expectWithRetry(() =>
197+
page.textContent('.alias-using-absolute-path'),
198+
).toBe('From dep-alias-using-absolute-path')
166199
})
167200

168201
test('variable names are reused in different scripts', async () => {
169-
expect(await page.textContent('.reused-variable-names')).toBe('reused')
202+
await expectWithRetry(() => page.textContent('.reused-variable-names')).toBe(
203+
'reused',
204+
)
170205
})
171206

172207
test('flatten id should generate correctly', async () => {
173-
expect(await page.textContent('.clonedeep-slash')).toBe('clonedeep-slash')
174-
expect(await page.textContent('.clonedeep-dot')).toBe('clonedeep-dot')
208+
await expectWithRetry(() => page.textContent('.clonedeep-slash')).toBe(
209+
'clonedeep-slash',
210+
)
211+
await expectWithRetry(() => page.textContent('.clonedeep-dot')).toBe(
212+
'clonedeep-dot',
213+
)
175214
})
176215

177216
test('non optimized module is not duplicated', async () => {
178-
expect(
179-
await page.textContent('.non-optimized-module-is-not-duplicated'),
217+
await expectWithRetry(() =>
218+
page.textContent('.non-optimized-module-is-not-duplicated'),
180219
).toBe('from-absolute-path, from-relative-path')
181220
})
182221

@@ -227,8 +266,8 @@ test('pre bundle css require', async () => {
227266
)
228267
}
229268

230-
expect(await getColor('.css-require')).toBe('red')
231-
expect(await getColor('.css-module-require')).toBe('red')
269+
await expectWithRetry(() => getColor('.css-require')).toBe('red')
270+
await expectWithRetry(() => getColor('.css-module-require')).toBe('red')
232271
})
233272

234273
test.runIf(isBuild)('no missing deps during build', async () => {
@@ -276,5 +315,7 @@ describe.runIf(isServe)('optimizeDeps config', () => {
276315
})
277316

278317
test('long file name should work', async () => {
279-
expect(await page.textContent('.long-file-name')).toMatch(`hello world`)
318+
await expectWithRetry(() => page.textContent('.long-file-name')).toMatch(
319+
`hello world`,
320+
)
280321
})

playground/test-utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
import type { DepOptimizationMetadata, Manifest } from 'vite'
1313
import { normalizePath } from 'vite'
1414
import { fromComment } from 'convert-source-map'
15+
import type { Assertion } from 'vitest'
1516
import { expect } from 'vitest'
1617
import type { ExecaChildProcess } from 'execa'
1718
import { isBuild, isWindows, page, testDir } from './vitestSetup'
@@ -232,6 +233,25 @@ export async function withRetry(
232233
await func()
233234
}
234235

236+
export const expectWithRetry = <T>(getActual: () => Promise<T>) => {
237+
type A = Assertion<T>
238+
return new Proxy(
239+
{},
240+
{
241+
get(_target, key) {
242+
return async (...args) => {
243+
await withRetry(
244+
async () => expect(await getActual())[key](...args),
245+
true,
246+
)
247+
}
248+
},
249+
},
250+
) as {
251+
[K in keyof A]: (...params: Parameters<A[K]>) => Promise<ReturnType<A[K]>>
252+
}
253+
}
254+
235255
type UntilBrowserLogAfterCallback = (logs: string[]) => PromiseLike<void> | void
236256

237257
export async function untilBrowserLogAfter(

0 commit comments

Comments
 (0)