Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed May 27, 2021
1 parent c9c88b1 commit 64f5a1a
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"eslint": "^7.27.0",
"jest": "^27.0.1",
"rimraf": "^3.0.2",
"ts-jest": "^27.0.0",
"ts-jest": "^27.0.1",
"typescript": "^4.3.2"
},
"resolutions": {
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/Commander.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Application, IRoute} from "@bunt/app";
import {Action, ContextArg, Disposable, Heartbeat, IDisposable, IRunnable} from "@bunt/unit";
import {Action, ContextArg, Heartbeat, IDisposable, IRunnable} from "@bunt/unit";
import {CommandContext} from "./Context/CommandContext";
import {RequestCommand} from "./Request";

Expand All @@ -11,12 +11,12 @@ export class Commander<C extends CommandContext> implements IRunnable, IDisposab
}

public static async execute<C extends CommandContext>(
context: ContextArg<C>, routes: IRoute<Action<C, any, IRunnable>>[] = []): Promise<Commander<C>> {
context: ContextArg<C>, routes: IRoute<Action<C, any, IRunnable>>[] = []): Promise<IRunnable | undefined> {
const command = new this<C>(await Application.factory<C>(context, routes));
return command.handle();
}

public async handle(): Promise<Commander<C>> {
public async handle(): Promise<IRunnable | undefined> {
const {context} = this.#application;
const request = new RequestCommand(context.program.args);

Expand Down
21 changes: 13 additions & 8 deletions packages/cli/test/src/Command.test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
import {Resolver, RouteRule} from "@bunt/app";
import {Fields, Nullable, Text} from "@bunt/input";
import {dispose, Heartbeat, isRunnable} from "@bunt/unit";
import {dispose, Heartbeat, isDisposable, isRunnable} from "@bunt/unit";
import {ok} from "assert";
import {Commander} from "../../src";
import {BaseTestCommand} from "./action/BaseTestCommand";
import {command} from "./command";
import {BaseContext} from "./context/BaseContext";
import {BaseTestCommand} from "./app/Action/BaseTestCommand";
import {command} from "./app/command";
import {BaseContext} from "./app/Context/BaseContext";

describe("Command", () => {
test("should return runnable", async () => {
const baseTestCommand = command(BaseTestCommand, new RouteRule(
const testCommand = command(BaseTestCommand, new RouteRule(
"test",
new Fields({name: new Nullable(Text)}),
new Resolver({name: ({context}) => context.args.getOption("name")}),
));

const context = new BaseContext(["test", "--name=test"]);
const result = await Commander.execute(context, [baseTestCommand]);
const result = await Commander.execute(context, [testCommand]);

expect(isRunnable(result)).toBe(true);
ok(isRunnable(result));

expect(isDisposable(result)).toBe(true);
ok(isDisposable(result));

await dispose(result);
await expect(Heartbeat.watch(result)).resolves.toBeUndefined();
expect(result.getHeartbeat().beats).toBe(false);
});

test("should return void", async () => {
const baseTestCommand = command(BaseTestCommand, new RouteRule(
const testCommand = command(BaseTestCommand, new RouteRule(
"test",
new Fields({name: new Nullable(Text)}),
new Resolver({name: ({context}) => context.args.getOption("name")}),
));

const context = new BaseContext(["test"]);
const result = await Commander.execute(context, [baseTestCommand]);
const result = await Commander.execute(context, [testCommand]);
expect(result).toBeUndefined();
});
});
28 changes: 28 additions & 0 deletions packages/cli/test/src/RunnableCommand.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {dispose, isDisposable, isRunnable} from "@bunt/unit";
import {AsyncState} from "@bunt/util";
import {ok} from "assert";
import {Commander} from "../../src";
import {RunnableTestCommand} from "./app/Action/RunnableTestCommand";
import {command} from "./app/command";
import {BaseContext} from "./app/Context/BaseContext";

test("Runnable Command", async () => {
const testCommand = command(RunnableTestCommand, "test");
const result = await Commander.execute(new BaseContext(["test"]), [testCommand]);
expect(isRunnable(result)).toBe(true);
ok(isRunnable(result));

const heartbeat = result.getHeartbeat();
const pending = heartbeat.watch();

expect(heartbeat.beats).toBe(true);
expect(pending.then).not.toBeUndefined();
expect(AsyncState.has(pending)).toBe(true);
expect(AsyncState.isReleased(pending)).toBe(false);

expect(isDisposable(result)).toBe(true);
ok(isDisposable(result));

await dispose(result);
await expect(heartbeat.beats).toBe(false);
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Disposable, Heartbeat, IDisposable, IRunnable} from "@bunt/unit";
import {Command} from "../../../src";
import {BaseContext} from "../context/BaseContext";
import {Heartbeat, IDisposable, IRunnable} from "@bunt/unit";
import {Command} from "../../../../src";
import {BaseContext} from "../Context/BaseContext";

export class BaseTestCommand extends Command<BaseContext, { name?: string }> {
public run(): void | Result {
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/test/src/app/Action/RunnableTestCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {IRunnable} from "@bunt/unit";
import {Command} from "../../../../src";
import {BaseContext} from "../Context/BaseContext";
import {RunnableTest} from "../RunnableTest";

export class RunnableTestCommand extends Command<BaseContext> {
public run(): IRunnable {
return new RunnableTest();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ApplyContext} from "@bunt/unit";
import {CommandContext} from "../../../src";
import {CommandContext} from "../../../../src";

export class BaseContext extends CommandContext {
}
Expand Down
12 changes: 12 additions & 0 deletions packages/cli/test/src/app/RunnableTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Heartbeat, IDisposable, IRunnable} from "@bunt/unit";

export class RunnableTest implements IRunnable, IDisposable {
public async dispose(): Promise<void> {
return;
}

public getHeartbeat(): Heartbeat {
return Heartbeat.create(this);
}

}
File renamed without changes.
10 changes: 6 additions & 4 deletions packages/unit/src/Runtime/Runtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {isNull, isUndefined, Logger, logger, Promisify, SingleRef} from "@bunt/util";
import {run} from "jest";
import {Disposable, dispose} from "../Dispose";
import {Heartbeat} from "./Heartbeat";
import {DisposableFn, IDisposable} from "./interfaces";
Expand Down Expand Up @@ -64,8 +63,11 @@ export class Runtime implements IDisposable {

public async dispose(): Promise<void> {
this.logger.info("dispose");
await Disposable.disposeAll();
process.exit(0);
process.nextTick(async () => {
this.logger.info("dispose all");
await Disposable.disposeAll()
.finally(() => process.exit(0));
});
}

private async watch(chain: Promise<unknown>[]): Promise<void> {
Expand All @@ -82,7 +84,7 @@ export class Runtime implements IDisposable {
finish();
}

dispose(this);
await dispose(this);
}

private async handle(result: unknown): Promise<void> {
Expand Down
8 changes: 7 additions & 1 deletion packages/util/src/Async/AsyncState.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {isDefined} from "../is";

export interface IAsyncStateMap<T> {
resolve: (value: T | PromiseLike<T>) => void;
reject: (error?: Error) => void;
Expand Down Expand Up @@ -43,6 +45,10 @@ export class AsyncState {
}

public static isReleased<T>(pending: Promise<T> | undefined): boolean {
return !pending || (registry.get(pending)?.done ?? true);
return isDefined(pending) && (registry.get(pending)?.done ?? true);
}

public static has<T>(pending: Promise<T> | undefined): boolean {
return isDefined(pending) && registry.has(pending);
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6992,10 +6992,10 @@ trim-off-newlines@^1.0.0:
resolved "https://registry.yarnpkg.com/trim-off-newlines/-/trim-off-newlines-1.0.1.tgz#9f9ba9d9efa8764c387698bcbfeb2c848f11adb3"
integrity sha1-n5up2e+odkw4dpi8v+sshI8RrbM=

ts-jest@^27.0.0:
version "27.0.0"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-27.0.0.tgz#b94dbad8f39276b583edc7712e6b3c29e16c8863"
integrity sha512-YhuEjDZz9ZjxKbUlgT3XtJb9lyditEjctlo1nLcn983my3Xz4BE3c2ogHhonmGlAdUUiGlz/Dq2KOMXmf1WHfA==
ts-jest@^27.0.1:
version "27.0.1"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-27.0.1.tgz#3cd174561c4bb311e0187a5837543fb858c60d16"
integrity sha512-03qAt77QjhxyM5Bt2KrrT1WbdumiwLz989sD3IUznSp3GIFQrx76kQqSMLF7ynnxrF3/1ipzABnHxMlU8PD4Vw==
dependencies:
bs-logger "0.x"
buffer-from "1.x"
Expand Down

0 comments on commit 64f5a1a

Please sign in to comment.