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
9 changes: 5 additions & 4 deletions tests/integration/cli/build-watch/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ export default defineConfig({
describe('build --watch should handle add / change / unlink', async () => {
test('basic', async () => {
const tempSrcPath = path.join(__dirname, 'test-temp-src');
const tempConfigFile = path.join(__dirname, 'test-temp-rslib.config.mjs');
await fse.remove(tempSrcPath);
await fse.remove(path.join(__dirname, 'dist'));
await fse.copy(path.join(__dirname, 'src'), path.resolve(tempSrcPath));
const tempConfigFile = path.join(__dirname, 'test-temp-rslib.config.mjs');
await fse.remove(tempConfigFile);

await fse.copy(path.join(__dirname, 'src'), path.resolve(tempSrcPath));
fse.outputFileSync(
tempConfigFile,
`import { defineConfig } from '@rslib/core';
Expand Down Expand Up @@ -102,11 +103,11 @@ export default defineConfig({
shell: true,
},
);

await awaitFileExists(distIndexFile);

fse.outputFileSync(srcFooFile, `export const foo = 'foo';`);
fse.outputFileSync(srcFoo2File, `export const foo2 = 'foo2';`);
await awaitFileExists(distFooFile);
await awaitFileExists(distFoo2File);
const content1 = await fse.readFile(distFooFile, 'utf-8');
expect(content1!).toMatchInlineSnapshot(`
Expand All @@ -126,7 +127,7 @@ export default defineConfig({
fse.removeSync(srcIndexFile);

// change
const wait = await awaitFileChanges(distFooFile);
const wait = await awaitFileChanges(distFooFile, 'foo1');
fse.outputFileSync(srcFooFile, `export const foo = 'foo1';`);
await wait();
const content3 = await fse.readFile(distFooFile, 'utf-8');
Expand Down
7 changes: 5 additions & 2 deletions tests/scripts/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,16 @@ export const awaitFileExists = async (dir: string) => {
}
};

export const awaitFileChanges = async (file: string) => {
export const awaitFileChanges = async (file: string, content: string) => {
const oldContent = await fse.readFile(file, 'utf-8');
return async () => {
const result = await waitFor(
() => {
try {
return fse.readFileSync(file, 'utf-8') !== oldContent;
return (
fse.readFileSync(file, 'utf-8') !== oldContent &&
fse.readFileSync(file, 'utf-8').includes(content)
);
} catch (e) {
return false;
}
Expand Down
Loading