diff --git a/tests/integration/cli/build-watch/build.test.ts b/tests/integration/cli/build-watch/build.test.ts index e6ac954ae..2b2cb2d29 100644 --- a/tests/integration/cli/build-watch/build.test.ts +++ b/tests/integration/cli/build-watch/build.test.ts @@ -1,12 +1,11 @@ -import { spawn } from 'node:child_process'; import path from 'node:path'; import { stripVTControlCharacters as stripAnsi } from 'node:util'; import { describe, expect, onTestFinished, test } from '@rstest/core'; import fse from 'fs-extra'; import { + expectBuildEnd, expectFile, - expectFileChanges, - rslibBinPath, + expectFileWithContent, runCli, } from 'test-helper'; @@ -91,8 +90,9 @@ export default defineConfig({ }, }, performance: { + buildCache: false, printFileSize: false, - } + }, }); `, ); @@ -104,21 +104,19 @@ export default defineConfig({ const distFooFile = path.join(__dirname, 'dist/esm/foo.js'); const distFoo2File = path.join(__dirname, 'dist/esm/foo2.js'); - const child = spawn( - 'node', - [rslibBinPath, 'build', '--watch', '-c', tempConfigFile], - { - cwd: __dirname, - stdio: 'pipe', - shell: true, - }, - ); - await expectFile(distIndexFile); + const { child } = runCli(`build --watch -c ${tempConfigFile}`, { + cwd: __dirname, + }); + await expectFileWithContent(distIndexFile, 'index'); fse.outputFileSync(srcFooFile, `export const foo = 'foo';`); + await expectBuildEnd(child); + await expectFileWithContent(distFooFile, `'foo'`); + fse.outputFileSync(srcFoo2File, `export const foo2 = 'foo2';`); - await expectFile(distFooFile); - await expectFile(distFoo2File); + await expectBuildEnd(child); + await expectFileWithContent(distFoo2File, 'foo2'); + const content1 = await fse.readFile(distFooFile, 'utf-8'); expect(content1!).toMatchInlineSnapshot(` "const foo = 'foo'; @@ -137,12 +135,13 @@ export default defineConfig({ fse.removeSync(srcIndexFile); // change - fse.outputFileSync(srcFooFile, `export const foo = 'foo1';`); - await expectFileChanges(distFooFile, content1, 'foo1'); + fse.outputFileSync(srcFooFile, `export const foo1 = 'foo1';`); + await expectBuildEnd(child); + await expectFileWithContent(distFooFile, 'foo1'); const content3 = await fse.readFile(distFooFile, 'utf-8'); expect(content3!).toMatchInlineSnapshot(` - "const foo = 'foo1'; - export { foo }; + "const foo1 = 'foo1'; + export { foo1 }; " `); diff --git a/tests/scripts/helper.ts b/tests/scripts/helper.ts index bed692b27..a496d0eca 100644 --- a/tests/scripts/helper.ts +++ b/tests/scripts/helper.ts @@ -1,6 +1,8 @@ +import type { ChildProcess } from 'node:child_process'; import fs from 'node:fs'; import { platform } from 'node:os'; import { join } from 'node:path'; +import { stripVTControlCharacters as stripAnsi } from 'node:util'; import { expect } from '@playwright/test'; import fse from 'fs-extra'; import { convertPathToPattern, type GlobOptions, glob } from 'tinyglobby'; @@ -83,19 +85,38 @@ export const expectFile = (dir: string) => expectPoll(() => fs.existsSync(dir)).toBeTruthy(); /** - * Expect a file to be changed and contain newContent + * Expect a file to exist and include specified content */ -export const expectFileChanges = async ( - file: string, - oldContent: string, - newContent: string, -) => { - await expectPoll(() => { +export const expectFileWithContent = ( + filePath: string, + expectedContent: string, +) => + expectPoll(() => { try { - const content = fse.readFileSync(file, 'utf-8'); - return content !== oldContent && content.includes(newContent); + if (!fs.existsSync(filePath)) { + return false; + } + const content = fs.readFileSync(filePath, 'utf-8'); + return content.includes(expectedContent); } catch { return false; } }).toBeTruthy(); -}; + +/** + * Expect log output from child process + */ +export const expectLog = (child: ChildProcess, log: string) => + new Promise((resolve) => { + const listener = (chunk: Buffer) => { + if (stripAnsi(chunk.toString()).includes(log)) { + resolve(); + } + }; + + child.stdout?.on('data', listener); + child.stderr?.on('data', listener); + }); + +export const expectBuildEnd = (child: ChildProcess) => + expectLog(child, 'built in');