Skip to content

Commit

Permalink
feat: add logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanosdev committed Apr 21, 2024
1 parent cfe0927 commit 71d109b
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 67 deletions.
1 change: 1 addition & 0 deletions docs/docs/guides/02-using-jest.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const config: Config = {
testEnvironment: 'node',
globalSetup: '<rootDir>/global-setup.ts',
globalTeardown: '<rootDir>/global-teardown.ts',
testTimeout: 30_000,
};

export default config;
Expand Down
1 change: 1 addition & 0 deletions docs/docs/guides/03-using-vitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export default defineConfig({
test: {
root: 'examples/counter/tests',
globalSetup: './global-setup.ts',
testTimeout: 30_000,
},
});
```
Expand Down
2 changes: 2 additions & 0 deletions examples/clock/src/main.mo
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Time "mo:base/Time";
import { now } = "mo:base/Time";
import { setTimer; recurringTimer } = "mo:base/Timer";
import Debug "mo:base/Debug";

actor Clock {
private type Time = Time.Time;
private stable var time : Time = 0;

public query func get() : async Time {
Debug.print("Getting current time...");
return time / 1_000_000;
};

Expand Down
5 changes: 4 additions & 1 deletion examples/clock/tests/global-setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { PocketIcServer } from '@hadronous/pic';

module.exports = async function (): Promise<void> {
const pic = await PocketIcServer.start();
const pic = await PocketIcServer.start({
pipeStdout: false,
pipeStderr: true,
});
const url = pic.getUrl();

process.env.PIC_URL = url;
Expand Down
1 change: 1 addition & 0 deletions examples/clock/tests/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const config: Config = {
testEnvironment: 'node',
globalSetup: '<rootDir>/global-setup.ts',
globalTeardown: '<rootDir>/global-teardown.ts',
testTimeout: 30_000,
};

export default config;
1 change: 1 addition & 0 deletions examples/counter/tests/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export default defineConfig({
test: {
root: 'examples/counter/tests',
globalSetup: './global-setup.ts',
testTimeout: 30_000,
},
});
1 change: 1 addition & 0 deletions examples/multicanister/tests/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const config: Config = {
testEnvironment: 'node',
globalSetup: '<rootDir>/global-setup.ts',
globalTeardown: '<rootDir>/global-teardown.ts',
testTimeout: 30_000,
};

export default config;
1 change: 1 addition & 0 deletions examples/nns_proxy/tests/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const config: Config = {
testEnvironment: 'node',
globalSetup: '<rootDir>/global-setup.ts',
globalTeardown: '<rootDir>/global-teardown.ts',
testTimeout: 30_000,
};

export default config;
1 change: 1 addition & 0 deletions examples/todo/tests/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const config: Config = {
testEnvironment: 'node',
globalSetup: '<rootDir>/global-setup.ts',
globalTeardown: '<rootDir>/global-teardown.ts',
testTimeout: 30_000,
};

export default config;
16 changes: 12 additions & 4 deletions packages/pic/src/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@ import { Ed25519KeyIdentity } from '@dfinity/identity';
*
* @example
* ```ts
* import { PocketIc, createIdentity } from '@hadronous/pic';
* import { PocketIc, PocketIcServer, createIdentity } from '@hadronous/pic';
* import { AnonymousIdentity } from '@dfinity/agent';
* import { _SERVICE, idlFactory } from '../declarations';
*
* const wasmPath = resolve('..', '..', 'canister.wasm');
*
* const pic = await PocketIc.create();
* const picServer = await PocketIcServer.create();
* const pic = await PocketIc.create(picServer.getUrl());
* const fixture = await pic.setupCanister<_SERVICE>(idlFactory, wasmPath);
* const { actor } = fixture;
*
* const bob = createIdentity('SuperSecretSeedPhraseForBob');
* actor.setIdentity(bob);
*
* await pic.tearDown();
* await picServer.stop();
* ```
*/
export function createIdentity(seedPhrase: string): Identity {
Expand Down Expand Up @@ -67,18 +71,22 @@ function generateMnemonic(): string {
*
* @example
* ```ts
* import { PocketIc, generateRandomIdentity } from '@hadronous/pic';
* import { PocketIc, PocketIcServer, generateRandomIdentity } from '@hadronous/pic';
* import { AnonymousIdentity } from '@dfinity/agent';
* import { _SERVICE, idlFactory } from '../declarations';
*
* const wasmPath = resolve('..', '..', 'canister.wasm');
*
* const pic = await PocketIc.create();
* const picServer = await PocketIcServer.create();
* const pic = await PocketIc.create(picServer.getUrl());
* const fixture = await pic.setupCanister<_SERVICE>(idlFactory, wasmPath);
* const { actor } = fixture;
*
* const bob = generateRandomIdentity();
* actor.setIdentity(bob);
*
* await pic.tearDown();
* await picServer.stop();
* ```
*/
export function generateRandomIdentity(): Identity {
Expand Down
1 change: 1 addition & 0 deletions packages/pic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export type {
UpgradeCanisterOptions,
} from './pocket-ic-types';
export { PocketIcServer } from './pocket-ic-server';
export { StartServerOptions as ServerStartOptions } from './pocket-ic-server-types';
14 changes: 14 additions & 0 deletions packages/pic/src/pocket-ic-server-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Options for starting a PocketIC server.
*/
export interface StartServerOptions {
/**
* Whether to pipe the server's stdout to the parent process's stdout.
*/
pipeStdout?: boolean;

/**
* Whether to pipe the server's stderr to the parent process's stderr.
*/
pipeStderr?: boolean;
}
61 changes: 56 additions & 5 deletions packages/pic/src/pocket-ic-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,34 @@ import {
isArm,
isDarwin,
} from './util';

import { StartServerOptions } from './pocket-ic-server-types';

/**
* This class represents the main PocketIC server.
* It is responsible for maintaining the lifecycle of the server process.
* See {@link PocketIc} for details on the client to use with this server.
*
* @category API
*
* @example
* ```ts
* import { PocketIc, PocketIcServer } from '@hadronous/pic';
* import { _SERVICE, idlFactory } from '../declarations';
*
* const wasmPath = resolve('..', '..', 'canister.wasm');
*
* const picServer = await PocketIcServer.create();
* const pic = await PocketIc.create(picServer.getUrl());
*
* const fixture = await pic.setupCanister<_SERVICE>({ idlFactory, wasmPath });
* const { actor } = fixture;
*
* // perform tests...
*
* await pic.tearDown();
* await picServer.stop();
* ```
*/
export class PocketIcServer {
private readonly url: string;

Expand All @@ -26,7 +53,15 @@ export class PocketIcServer {
this.url = `http://127.0.0.1:${portNumber}`;
}

public static async start(): Promise<PocketIcServer> {
/**
* Start a new PocketIC server.
*
* @param options Options for starting the server.
* @returns An instance of the PocketIC server.
*/
public static async start(
options: StartServerOptions = {},
): Promise<PocketIcServer> {
const binPath = this.getBinPath();
await this.assertBinExists(binPath);

Expand All @@ -35,9 +70,15 @@ export class PocketIcServer {
const portFilePath = tmpFile(`${picFilePrefix}.port`);
const readyFilePath = tmpFile(`${picFilePrefix}.ready`);

const serverProcess = spawn(binPath, ['--pid', pid.toString()], {
stdio: 'ignore',
});
const serverProcess = spawn(binPath, ['--pid', pid.toString()]);

if (options.pipeStdout) {
serverProcess.stdout.pipe(process.stdout);
}

if (options.pipeStderr) {
serverProcess.stderr.pipe(process.stderr);
}

serverProcess.on('error', error => {
if (isArm() && isDarwin()) {
Expand All @@ -61,10 +102,20 @@ export class PocketIcServer {
});
}

/**
* Get the URL of the server.
*
* @returns The URL of the server.
*/
public getUrl(): string {
return this.url;
}

/**
* Stop the server.
*
* @returns A promise that resolves when the server has stopped.
*/
public async stop(): Promise<void> {
return new Promise((resolve, reject) => {
this.serverProcess.on('exit', () => {
Expand Down
Loading

0 comments on commit 71d109b

Please sign in to comment.