diff --git a/test/index.ts b/test/index.ts index 32b3235de0..aa4f67b16b 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,13 +1,14 @@ import * as dns from 'node:dns'; dns.setDefaultResultOrder('ipv4first'); -import { ActorSubclass, HttpAgent, Identity } from '@dfinity/agent'; +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'; @@ -114,14 +115,37 @@ export function defaultPropTestParams(): fc.Parameters { type GetCanisterActorOptions = { identity?: Identity; - agent?: HttpAgent; + agent?: Agent; + parentDir?: string; }; export async function getCanisterActor( canisterName: string, - _options: GetCanisterActorOptions = {} + options: GetCanisterActorOptions = {} ): Promise> { - throw new Error('This prop tests uses getCanisterActor and is good to go'); + 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(): { diff --git a/test/property/get_actor.ts b/test/property/get_actor.ts index 9b6bc00735..c81bd5b822 100644 --- a/test/property/get_actor.ts +++ b/test/property/get_actor.ts @@ -1,8 +1,8 @@ -import { Agent, HttpAgent } from '@dfinity/agent'; +import { Agent } from '@dfinity/agent'; -import { getCanisterId } from '../../dfx'; +import { getCanisterActor } from '../index'; -export function getActor(parentDir: string, agent?: Agent): any { +export async function getActor(parentDir: string, agent?: Agent): Promise { const resolvedPathIndex = require.resolve( `${parentDir}/dfx_generated/canister/index.js` ); @@ -13,15 +13,8 @@ export function getActor(parentDir: string, agent?: Agent): any { delete require.cache[resolvedPathIndex]; delete require.cache[resolvedPathDid]; - // eslint-disable-next-line @typescript-eslint/no-require-imports - const { createActor } = require(`${parentDir}/dfx_generated/canister`); - - return createActor(getCanisterId('canister'), { - agent: - agent ?? - new HttpAgent({ - host: 'http://127.0.0.1:8000', - verifyQuerySignatures: false // TODO Major issue: https://forum.dfinity.org/t/agent-js-0-20-0-is-released-replica-signed-query-edition/24743/16?u=lastmjs - }) + return getCanisterActor('canister', { + parentDir, + agent }); }