Skip to content

Commit

Permalink
feat: add act hook to esbuild testkit
Browse files Browse the repository at this point in the history
  • Loading branch information
barak007 committed Feb 8, 2024
1 parent f1e7e68 commit 059e308
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
34 changes: 33 additions & 1 deletion packages/esbuild/test/e2e/esbuild-testkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,41 @@ export class ESBuildTestKit {
if (!run) {
throw new Error(`could not find ${buildExport || 'run'} export in ${buildFile}`);
}
const onEnd = new Set<() => void>();
function act<T>(fn: () => T, timeout = 3000) {
return new Promise<T>((resolve, reject) => {
let results = undefined as T | Promise<T>;
const tm = setTimeout(reject, timeout);
const handler = () => {
clearTimeout(tm);
onEnd.delete(handler);
if (results instanceof Promise) {
results.then(resolve, reject);
} else {
resolve(results);
}
};
onEnd.add(handler);
results = fn();
});
}

const buildContext = await run(context, (options: BuildOptions) => ({
...options,
plugins: [...(options.plugins ?? []), ...extraPlugins],
plugins: [
...(options.plugins ?? []),
...extraPlugins,
{
name: 'build-end',
setup(build) {
build.onEnd(() => {
for (const fn of onEnd) {
fn();
}
});
},
},
],
absWorkingDir: projectDir,
loader: {
'.png': 'file',
Expand Down Expand Up @@ -126,6 +157,7 @@ export class ESBuildTestKit {
context: buildContext,
serve,
open,
act,
write(pathInCwd: string, content: string) {
writeFileSync(join(projectDir, pathInCwd), content, 'utf8');
},
Expand Down
19 changes: 11 additions & 8 deletions packages/esbuild/test/e2e/rebuild/rebuild.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,36 @@ describe('Stylable ESBuild plugin rebuild on change', function () {
afterEach(() => tk.dispose());

it('should pick up rebuild', async function () {
const { context, read, write } = await tk.build({
const { context, read, write, act } = await tk.build({
project: 'rebuild',
tmp: true,
});
const css1 = read('dist/index.js');
expect(css1, 'initial color').to.includes('color: red');
await context.watch();
await sleep(2222);
write('a.st.css', `.root{color: green}`);
await sleep(2222);
await act(() => {
write('a.st.css', `.root{color: green}`);
});
const css2 = read('dist/index.js');
expect(css2, 'color after change').to.includes('color: green');
});

it('should stay alive after error', async function () {
const { context, read, write } = await tk.build({
const { context, read, write, act } = await tk.build({
project: 'rebuild',
tmp: true,
});
const css1 = read('dist/index.js');
expect(css1, 'initial color').to.includes('color: red');
await context.watch();
await sleep(2222);
write('a.st.css', `.root{}}}}}`);
await sleep(2222);
write('a.st.css', `.root{color: green}`);
await sleep(2222);
await act(() => {
write('a.st.css', `.root{}}}}}`);
});
await act(() => {
write('a.st.css', `.root{color: green}`);
});
const css2 = read('dist/index.js');
expect(css2, 'color after change').to.includes('color: green');
});
Expand Down

0 comments on commit 059e308

Please sign in to comment.