Skip to content

Commit

Permalink
fix: remove usage of global disposeAfter from withFeature and getRunn…
Browse files Browse the repository at this point in the history
…ingFeature (#2176)
  • Loading branch information
barak007 authored Dec 3, 2023
1 parent e877837 commit 85b19d8
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 111 deletions.
21 changes: 12 additions & 9 deletions packages/electron-commons/test/electron-node.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from 'node:path';
import type { TopLevelConfig } from '@wixc3/engine-core';
import testFeature, { serverEnv } from '@fixture/disconnecting-env/dist/disconnecting-env.feature.js';
import { setupRunningNodeEnv } from '../test-kit/setup-running-node-env.js';
import { disposeAfter } from '@wixc3/testing';
import { Disposables } from '@wixc3/patterns';

const { expect } = chai;
chai.use(chaiAsPromised);
Expand All @@ -28,6 +28,8 @@ const setupRunningEnv = ({ featuresConfig, stdio }: SetupRunningFeatureOptions)
});

describe('onDisconnectHandler for node environment initializer', () => {
const disposables = new Disposables();
afterEach(() => disposables.dispose());
describe('without own uncaughtException handling', () => {
it('should catch on dispose of env', async () => {
const { dispose, exitPromise } = await setupRunningEnv({
Expand All @@ -42,7 +44,8 @@ describe('onDisconnectHandler for node environment initializer', () => {
const { exitPromise, dispose } = await setupRunningEnv({
featuresConfig: [testFeature.use({ errorsConfig: { throwError: 'exit' } })],
});
disposeAfter(dispose, {

disposables.add(dispose, {
timeout,
name: `env ${testFeature.id}`,
});
Expand All @@ -53,7 +56,8 @@ describe('onDisconnectHandler for node environment initializer', () => {
const { exitPromise, dispose } = await setupRunningEnv({
featuresConfig: [testFeature.use({ errorsConfig: { throwError: 'exception' } })],
});
disposeAfter(dispose, {

disposables.add(dispose, {
timeout,
name: `env ${testFeature.id}`,
});
Expand All @@ -64,7 +68,7 @@ describe('onDisconnectHandler for node environment initializer', () => {
const { exitPromise, dispose } = await setupRunningEnv({
featuresConfig: [testFeature.use({ errorsConfig: { throwError: 'promise-reject' } })],
});
disposeAfter(dispose, {
disposables.add(dispose, {
timeout,
name: `env ${testFeature.id}`,
});
Expand All @@ -76,7 +80,7 @@ describe('onDisconnectHandler for node environment initializer', () => {
featuresConfig: [testFeature.use({ errorsConfig: { throwError: 'exception' } })],
stdio: 'pipe',
});
disposeAfter(dispose, {
disposables.add(dispose, {
timeout,
name: `env ${testFeature.id}`,
});
Expand All @@ -100,7 +104,7 @@ describe('onDisconnectHandler for node environment initializer', () => {
const { exitPromise, dispose } = await setupRunningEnv({
featuresConfig: [testFeature.use({ errorsConfig: { throwError: 'exit', handleUncaught } })],
});
disposeAfter(dispose, {
disposables.add(dispose, {
timeout,
name: `env ${testFeature.id}`,
});
Expand All @@ -110,18 +114,17 @@ describe('onDisconnectHandler for node environment initializer', () => {
const { exitPromise, dispose } = await setupRunningEnv({
featuresConfig: [testFeature.use({ errorsConfig: { throwError: 'exception', handleUncaught } })],
});
disposeAfter(dispose, {
disposables.add(dispose, {
timeout,
name: `env ${testFeature.id}`,
});

await expect(exitPromise).to.eventually.deep.eq({ exitCode: 1 });
});
it('should catch on env unhandled promise rejection', async () => {
const { exitPromise, dispose } = await setupRunningEnv({
featuresConfig: [testFeature.use({ errorsConfig: { throwError: 'promise-reject', handleUncaught } })],
});
disposeAfter(dispose, {
disposables.add(dispose, {
timeout,
name: `env ${testFeature.id}`,
});
Expand Down
11 changes: 7 additions & 4 deletions packages/electron-commons/test/worker-thread.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import workerThreadFeature, {
serverEnv,
type WorkerService,
} from '@fixture/worker-thread/dist/worker-thread.feature.js';
import { disposeAfter } from '@wixc3/testing';
import { Disposables } from '@wixc3/patterns';
import { expect } from 'chai';
import { setupRunningNodeEnv } from '../test-kit/setup-running-node-env.js';

Expand All @@ -24,9 +24,12 @@ const setupRunningEnv = (featureId: string) =>
const timeout = 3000;

describe('workerthread environment type', () => {
const disposables = new Disposables();
afterEach(() => disposables.dispose());
it('initializes worker, calls API and disposes', async () => {
const { dispose, communication } = await setupRunningEnv(workerThreadFeature.id);
disposeAfter(dispose, {

disposables.add(dispose, {
timeout,
name: `worker thread ${workerThreadFeature.id}`,
});
Expand All @@ -42,7 +45,7 @@ describe('workerthread environment type', () => {

it('initializes multiple workers, calls API and disposes', async () => {
const { dispose, communication } = await setupRunningEnv(`${workerThreadFeature.id}/${multiFeature.id}`);
disposeAfter(dispose, {
disposables.add(dispose, {
timeout,
name: `worker thread ${workerThreadFeature.id}/${multiFeature.id}`,
});
Expand All @@ -60,7 +63,7 @@ describe('workerthread environment type', () => {
const { dispose, communication } = await setupRunningEnv(
`${workerThreadFeature.id}/${contextualMultiPreloadFeature.id}`,
);
disposeAfter(dispose, {
disposables.add(dispose, {
timeout,
name: `worker thread ${workerThreadFeature.id}/${contextualMultiPreloadFeature.id}`,
});
Expand Down
21 changes: 13 additions & 8 deletions packages/test-kit/src/run-environment.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import { RuntimeEngine, type AnyEnvironment, type FeatureClass, type Running } from '@wixc3/engine-core';
import { getRunningFeature as originalGetRunningFeature, type RunningFeatureOptions } from '@wixc3/engine-scripts';
import { disposeAfter } from '@wixc3/testing';

/**
* get a running feature with no browser environment
* @param disposeAfterTestTimeout if false, will not dispose the engine after the test
* @param autoDisposeTimeout if false, will not dispose the engine after the test
*/
export async function getRunningFeature<F extends FeatureClass, ENV extends AnyEnvironment>(
options: RunningFeatureOptions<F, ENV>,
disposeAfterTestTimeout: false | number = 10_000,
autoDisposeTimeout: false | number = 10_000,
): Promise<{
runningApi: Running<F, ENV>;
engine: RuntimeEngine;
/**@deprecated use engine.shutdown */
dispose: () => Promise<void>;
}> {
const runningFeature = await originalGetRunningFeature(options);
if (disposeAfterTestTimeout) {
disposeAfter(runningFeature.engine.shutdown, {
name: `engine shutdown for ${options.featureName}`,
timeout: disposeAfterTestTimeout,
});
if (autoDisposeTimeout) {
if (typeof afterEach !== 'undefined') {
afterEach(`engine shutdown for ${options.featureName}`, function () {
this.timeout(autoDisposeTimeout);
return runningFeature.engine.shutdown();
});
} else {
throw new Error(
`autoDisposeTimeout is set but the environment you are running does not have global "afterEach", set it to false to avoid auto-dispose.`,
);
}
}

return runningFeature;
Expand Down
Loading

0 comments on commit 85b19d8

Please sign in to comment.