Skip to content

Commit

Permalink
move getCanisterActor into separte file to avoid jest
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Dec 19, 2024
1 parent 5d4d45a commit c991337
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
41 changes: 41 additions & 0 deletions test/get_canister_actor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { HttpAgent, Identity } from '@dfinity/agent';
import { ActorSubclass } from '@dfinity/agent';
import { Agent } from '@dfinity/agent';
import { join } from 'path';

import { getCanisterId } from '../dfx';

type GetCanisterActorOptions = {
identity?: Identity;
agent?: Agent;
parentDir?: string;
};

export async function getCanisterActor<T>(
canisterName: string,
options: GetCanisterActorOptions = {}
): Promise<ActorSubclass<T>> {
const parentDir = options.parentDir ?? join(process.cwd(), 'test');
const { createActor } = await import(
join(parentDir, 'dfx_generated', canisterName)
);

const agent =
options.agent ??
(await HttpAgent.create({
host: 'http://127.0.0.1:8000',
shouldFetchRootKey: true
}));

const actor = createActor(getCanisterId(canisterName), {
agent
});

if (typeof canisterName === 'string') {
throw new Error(
'This prop tests uses getCanisterActor and is good to go'
);
}

return actor;
}
39 changes: 2 additions & 37 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as dns from 'node:dns';
dns.setDefaultResultOrder('ipv4first');

import { ActorSubclass, Agent, HttpAgent, Identity } from '@dfinity/agent';
import { describe, expect, test } from '@jest/globals';
import * as fc from 'fast-check';
import { join } from 'path';

import { execSyncPretty } from '../src/build/stable/utils/exec_sync_pretty';
export { expect } from '@jest/globals';
import { getCanisterId } from '../dfx';
import { runBenchmarksForCanisters } from './benchmarks';
import { runFuzzTests } from './fuzz';

export type Test = () => void;

export { getCanisterActor } from './get_canister_actor';

export function runTests(
tests: Test,
canisterNames: string | string[] | undefined = undefined,
Expand Down Expand Up @@ -113,41 +113,6 @@ export function defaultPropTestParams<T = unknown>(): fc.Parameters<T> {
return seed !== undefined ? { ...baseParams, seed, path } : baseParams;
}

type GetCanisterActorOptions = {
identity?: Identity;
agent?: Agent;
parentDir?: string;
};

export async function getCanisterActor<T>(
canisterName: string,
options: GetCanisterActorOptions = {}
): Promise<ActorSubclass<T>> {
const parentDir = options.parentDir ?? join(process.cwd(), 'test');
const { createActor } = await import(
join(parentDir, 'dfx_generated', canisterName)
);

const agent =
options.agent ??
(await HttpAgent.create({
host: 'http://127.0.0.1:8000',
shouldFetchRootKey: true
}));

const actor = createActor(getCanisterId(canisterName), {
agent
});

if (typeof canisterName === 'string') {
throw new Error(
'This prop tests uses getCanisterActor and is good to go'
);
}

return actor;
}

function processEnvVars(): {
shouldRunTests: boolean;
shouldRunTypeChecks: boolean;
Expand Down
2 changes: 1 addition & 1 deletion test/property/get_actor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Agent } from '@dfinity/agent';

import { getCanisterActor } from '../index';
import { getCanisterActor } from '../get_canister_actor';

export async function getActor(parentDir: string, agent?: Agent): Promise<any> {
const resolvedPathIndex = require.resolve(
Expand Down

0 comments on commit c991337

Please sign in to comment.