Skip to content

Commit 1c809fd

Browse files
committed
test: stabilize watch hmr baseline reads
1 parent aebd420 commit 1c809fd

5 files changed

Lines changed: 121 additions & 63 deletions

File tree

packages/weapp-tailwindcss/test/watch-hmr-class-baseline.unit.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,42 @@ describe('watch-hmr class baseline', () => {
7676
expect(outputs.globalStyle).toContain('.ready')
7777
expect(session.ensureRunning).toHaveBeenCalled()
7878
})
79+
80+
it('accepts an existing empty global style output as a class mutation baseline', async () => {
81+
tempDir = await mkdtemp(path.join(os.tmpdir(), 'weapp-tw-class-empty-style-'))
82+
const outputWxml = path.join(tempDir, 'index.wxml')
83+
const outputJs = path.join(tempDir, 'index.js')
84+
const outputStyle = path.join(tempDir, 'app.wxss')
85+
86+
await Promise.all([
87+
writeFile(outputWxml, '<view class="ready"></view>', 'utf8'),
88+
writeFile(outputJs, 'export default {}', 'utf8'),
89+
writeFile(outputStyle, '', 'utf8'),
90+
])
91+
92+
const session: WatchSession = {
93+
child: {} as WatchSession['child'],
94+
ensureRunning: vi.fn(),
95+
lastCompileSuccessAt: vi.fn(() => 0),
96+
logs: vi.fn(() => ''),
97+
stop: vi.fn(async () => {}),
98+
}
99+
const watchCase = {
100+
label: 'demo/weapp-vite-tailwindcss-v4',
101+
outputWxml,
102+
outputJs,
103+
} as WatchCase
104+
105+
const outputs = await waitForClassMutationBaselineOutputs(
106+
watchCase,
107+
createOptions(),
108+
session,
109+
'script',
110+
[outputStyle],
111+
)
112+
113+
expect(outputs.wxml).toContain('ready')
114+
expect(outputs.js).toContain('export default')
115+
expect(outputs.globalStyle).toBe('')
116+
})
79117
})

tools/weapp-tailwindcss-scripts/src/watch-hmr-regression/mutations/class.ts

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ import {
3232
collectPluginProcessMetrics,
3333
createClassMutationScenario,
3434
expandOutputFileEntries,
35-
hasResolvedOutputFiles,
3635
readJoinedOutputFiles,
3736
resolvePreferredRound,
37+
waitForClassOutputBaseline,
3838
waitForCompileSettled,
3939
waitForMarkerState,
4040
waitForOutputFilesUpdated,
@@ -73,49 +73,7 @@ export async function waitForClassMutationBaselineOutputs(
7373
mutationKind: 'template' | 'script' | 'content',
7474
globalStyleOutputs: string[],
7575
): Promise<RoundOutputs> {
76-
let resolvedOutputs: RoundOutputs | undefined
77-
let lastReason = 'outputs are not ready'
78-
const waitStartedAt = Date.now()
79-
80-
await waitFor(
81-
async () => {
82-
const [wxml, js, globalStyle, hasGlobalStyleOutputs] = await Promise.all([
83-
readFileIfExists(watchCase.outputWxml),
84-
readFileIfExists(watchCase.outputJs),
85-
readJoinedOutputFiles(globalStyleOutputs),
86-
hasResolvedOutputFiles(globalStyleOutputs),
87-
])
88-
89-
if (wxml && js && hasGlobalStyleOutputs) {
90-
resolvedOutputs = {
91-
wxml,
92-
js,
93-
globalStyle,
94-
}
95-
return true
96-
}
97-
98-
lastReason = [
99-
wxml ? undefined : 'wxml',
100-
js ? undefined : 'js',
101-
hasGlobalStyleOutputs ? undefined : 'global style',
102-
].filter(Boolean).join(', ')
103-
return false
104-
},
105-
{
106-
timeoutMs: options.timeoutMs,
107-
pollMs: options.pollMs,
108-
message: `[${watchCase.label}] baseline outputs are missing for ${mutationKind}: ${lastReason}`,
109-
onTick: session.ensureRunning,
110-
},
111-
waitStartedAt,
112-
)
113-
114-
if (!resolvedOutputs) {
115-
throw new Error(`[${watchCase.label}] baseline outputs failed to resolve for ${mutationKind}`)
116-
}
117-
118-
return resolvedOutputs
76+
return waitForClassOutputBaseline(watchCase, options, session, mutationKind, globalStyleOutputs)
11977
}
12078

12179
function collectBgHexTruncationNeedles(classTokens: string[]) {

tools/weapp-tailwindcss-scripts/src/watch-hmr-regression/mutations/class/added-class.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
collectPluginProcessMetrics,
2121
createClassMutationScenario,
2222
readJoinedOutputFiles,
23+
waitForClassOutputBaseline,
2324
waitForCompileSettled,
2425
waitForMarkerState,
2526
waitForOutputsUpdated,
@@ -152,15 +153,17 @@ export async function runAddedClassMutation(
152153
verifyClassLiteralIn,
153154
} = options
154155

155-
const [baselineWxml, baselineJs, baselineGlobalStyle] = await Promise.all([
156-
readFileIfExists(watchCase.outputWxml),
157-
readFileIfExists(watchCase.outputJs),
158-
readJoinedOutputFiles(globalStyleOutputs),
159-
])
160-
161-
if (!baselineWxml || !baselineJs || !baselineGlobalStyle) {
162-
throw new Error(`[${watchCase.label}] missing baseline outputs for ${mutationKind} added-class mutation`)
163-
}
156+
const {
157+
wxml: baselineWxml,
158+
js: baselineJs,
159+
globalStyle: baselineGlobalStyle,
160+
} = await waitForClassOutputBaseline(
161+
watchCase,
162+
cliOptions,
163+
session,
164+
mutationKind,
165+
globalStyleOutputs,
166+
)
164167

165168
const baseScenario = createClassMutationScenario(
166169
watchCase,

tools/weapp-tailwindcss-scripts/src/watch-hmr-regression/mutations/class/comment-carrier.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import type {
88
WatchSession,
99
} from '../../types'
1010
import { formatPath } from '../../cli'
11-
import { getMtime, readFileIfExists, writeFilePreserveEol } from '../../text'
11+
import { getMtime, writeFilePreserveEol } from '../../text'
1212
import {
1313
collectPluginProcessMetrics,
1414
createClassMutationScenario,
1515
readJoinedOutputFiles,
16+
waitForClassOutputBaseline,
1617
waitForMarkerState,
1718
waitForOutputsUpdated,
1819
} from '../shared'
@@ -51,15 +52,17 @@ export async function runCommentCarrierMutation(
5152
baselineMtime,
5253
} = options
5354

54-
const [baselineWxml, baselineJs, baselineGlobalStyle] = await Promise.all([
55-
readFileIfExists(watchCase.outputWxml),
56-
readFileIfExists(watchCase.outputJs),
57-
readJoinedOutputFiles(globalStyleOutputs),
58-
])
59-
60-
if (!baselineWxml || !baselineJs || !baselineGlobalStyle) {
61-
throw new Error(`[${watchCase.label}] missing baseline outputs for script comment-carrier mutation`)
62-
}
55+
const {
56+
wxml: baselineWxml,
57+
js: baselineJs,
58+
globalStyle: baselineGlobalStyle,
59+
} = await waitForClassOutputBaseline(
60+
watchCase,
61+
cliOptions,
62+
session,
63+
'script',
64+
globalStyleOutputs,
65+
)
6366

6467
const mutateCommentCarrier = mutation.mutateCommentCarrier
6568
if (!mutateCommentCarrier) {

tools/weapp-tailwindcss-scripts/src/watch-hmr-regression/mutations/shared.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,62 @@ export async function waitForOutputsUpdated(
247247
)
248248
}
249249

250+
export async function waitForClassOutputBaseline(
251+
watchCase: WatchCase,
252+
options: CliOptions,
253+
session: WatchSession,
254+
mutationKind: 'template' | 'script' | 'content',
255+
globalStyleOutputs: string[],
256+
) {
257+
let resolvedOutputs: {
258+
wxml: string
259+
js: string
260+
globalStyle: string
261+
} | undefined
262+
let lastReason = 'outputs are not ready'
263+
const waitStartedAt = Date.now()
264+
265+
await waitFor(
266+
async () => {
267+
const [wxml, js, globalStyle, hasGlobalStyleOutputs] = await Promise.all([
268+
readFileIfExists(watchCase.outputWxml),
269+
readFileIfExists(watchCase.outputJs),
270+
readJoinedOutputFiles(globalStyleOutputs),
271+
hasResolvedOutputFiles(globalStyleOutputs),
272+
])
273+
274+
if (wxml && js && hasGlobalStyleOutputs) {
275+
resolvedOutputs = {
276+
wxml,
277+
js,
278+
globalStyle,
279+
}
280+
return true
281+
}
282+
283+
lastReason = [
284+
wxml ? undefined : 'wxml',
285+
js ? undefined : 'js',
286+
hasGlobalStyleOutputs ? undefined : 'global style',
287+
].filter(Boolean).join(', ')
288+
return false
289+
},
290+
{
291+
timeoutMs: options.timeoutMs,
292+
pollMs: options.pollMs,
293+
message: `[${watchCase.label}] baseline outputs are missing for ${mutationKind}: ${lastReason}`,
294+
onTick: session.ensureRunning,
295+
},
296+
waitStartedAt,
297+
)
298+
299+
if (!resolvedOutputs) {
300+
throw new Error(`[${watchCase.label}] baseline outputs failed to resolve for ${mutationKind}`)
301+
}
302+
303+
return resolvedOutputs
304+
}
305+
250306
export async function waitForOutputFilesUpdated(
251307
watchCase: WatchCase,
252308
files: string[],

0 commit comments

Comments
 (0)