Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions tests/integration/cli/build-watch/build.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -91,8 +90,9 @@ export default defineConfig({
},
},
performance: {
buildCache: false,
printFileSize: false,
}
},
});
`,
);
Expand All @@ -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';
Expand All @@ -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 };
"
`);

Expand Down
41 changes: 31 additions & 10 deletions tests/scripts/helper.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<void>((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');
Loading