diff --git a/test/get_canister_actor.ts b/test/get_canister_actor.ts new file mode 100644 index 0000000000..6fe9a12fb2 --- /dev/null +++ b/test/get_canister_actor.ts @@ -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( + canisterName: string, + options: GetCanisterActorOptions = {} +): Promise> { + 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; +} diff --git a/test/index.ts b/test/index.ts index aa4f67b16b..61bcb8ec09 100644 --- a/test/index.ts +++ b/test/index.ts @@ -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, @@ -113,41 +113,6 @@ export function defaultPropTestParams(): fc.Parameters { return seed !== undefined ? { ...baseParams, seed, path } : baseParams; } -type GetCanisterActorOptions = { - identity?: Identity; - agent?: Agent; - parentDir?: string; -}; - -export async function getCanisterActor( - canisterName: string, - options: GetCanisterActorOptions = {} -): Promise> { - 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; diff --git a/test/property/get_actor.ts b/test/property/get_actor.ts index c81bd5b822..f18d33bb61 100644 --- a/test/property/get_actor.ts +++ b/test/property/get_actor.ts @@ -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 { const resolvedPathIndex = require.resolve(