Skip to content

Commit

Permalink
fix: remove usages of disposeAfter
Browse files Browse the repository at this point in the history
  • Loading branch information
barak007 committed Dec 3, 2023
1 parent af39494 commit aceb256
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 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
13 changes: 6 additions & 7 deletions packages/test-kit/src/run-environment.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
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) {
afterEach(`engine shutdown for ${options.featureName}`, function () {
this.timeout(autoDisposeTimeout);
return runningFeature.engine.shutdown();
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/test-kit/src/with-local-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function withLocalFixture(suiteOptions: IWithLocalFixtureOptions) {
const { fixturePath = suiteOptions.fixturePath, runOptions = suiteOptions.runOptions } = testOptions;
if (runOptions && runOptions.projectPath) {
throw new Error(
`runOptions["projectPath"] shouldn't be provided. It will get overriden by returned projectPath.`,
`runOptions["projectPath"] shouldn't be provided. It will get overridden by returned projectPath.`,
);
}

Expand Down

0 comments on commit aceb256

Please sign in to comment.