Skip to content

Commit

Permalink
better wait for
Browse files Browse the repository at this point in the history
  • Loading branch information
alec-chernicki committed Dec 19, 2023
1 parent d66d3e7 commit fee8201
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 48 deletions.
4 changes: 0 additions & 4 deletions packages/commonality/src/cli/commands/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ import {
} from '@commonalityco/data-project';
import chalk from 'chalk';
import waitOn from 'wait-on';
import killPort from 'kill-port';
import url from 'node:url';
import { createRequire } from 'node:module';
import c from 'picocolors';
import { resolveModule } from 'local-pkg';
import { isCI } from 'std-env';
import prompts from 'prompts';
import { ExecaChildProcess } from 'execa';

const EXIT_CODE_RESTART = 43;

const command = new Command();

Expand Down
53 changes: 9 additions & 44 deletions packages/commonality/test/integration/studio.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
import { describe, test, expect, beforeEach } from 'vitest';
import { describe, test, expect, beforeEach, vi } from 'vitest';

import fs from 'fs-extra';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { ExecaChildProcess, execa } from 'execa';
import os from 'node:os';
import { afterEach } from 'node:test';
import waitOn from 'wait-on';
import { Writable } from 'node:stream';
import killPort from 'kill-port';
import getPort from 'get-port';

const binPath = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../../bin.js',
);

const waitFor = (
fn: () => Promise<boolean> | boolean,
interval: number = 1000,
) => {
return new Promise((resolve, reject) => {
const intervalId = setInterval(async () => {
try {
const result = await fn();
if (result) {
clearInterval(intervalId);
resolve(result);
}
} catch (error) {
clearInterval(intervalId);
reject(error);
}
}, interval);
});
};

describe('studio', () => {
const temporaryDirectoryPath = process.env['RUNNER_TEMP'] || os.tmpdir();
const temporaryPath = fs.mkdtempSync(temporaryDirectoryPath);
Expand Down Expand Up @@ -82,42 +60,29 @@ describe('studio', () => {
cliProcess.stdout?.pipe(stdoutMock);
cliProcess.stderr?.pipe(stdoutMock);

console.log({ output });
await waitFor(() => {
return output.includes('📦 Starting Commonality Studio...');
await vi.waitFor(() => {
expect(output).toContain('📦 Starting Commonality Studio...');
});

expect(output).toContain('📦 Starting Commonality Studio...');

await waitFor(() => {
return output.includes(
await vi.waitFor(() => {
expect(output).toContain(
`MISSING DEPENDENCY Cannot find dependency '@commonalityco/studio'`,
);
});

expect(output).toContain(
`MISSING DEPENDENCY Cannot find dependency '@commonalityco/studio'`,
);

await waitFor(() => {
return output.includes(
await vi.waitFor(() => {
expect(output).toContain(
`Viewable at: http://127.0.0.1:${preferredPort} (press ctrl-c to quit)`,
);
});

expect(output).toContain(
`Viewable at: http://127.0.0.1:${preferredPort} (press ctrl-c to quit)`,
);

cliProcess.kill('SIGTERM', {
forceKillAfterTimeout: 2000,
});

await waitFor(() => {
return output.includes(`Successfully exited Commonality Studio`);
await vi.waitFor(() => {
expect(output).toContain(`Successfully exited Commonality Studio`);
});

expect(output).toContain(`Successfully exited Commonality Studio`);
},
{ timeout: 50_000 },
);
Expand Down

0 comments on commit fee8201

Please sign in to comment.