Skip to content

Commit

Permalink
update prop tests to use getCanisterActor
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Dec 19, 2024
1 parent 96f8232 commit 5d4d45a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
32 changes: 28 additions & 4 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -114,14 +115,37 @@ export function defaultPropTestParams<T = unknown>(): fc.Parameters<T> {

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

export async function getCanisterActor<T>(
canisterName: string,
_options: GetCanisterActorOptions = {}
options: GetCanisterActorOptions = {}
): Promise<ActorSubclass<T>> {
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(): {
Expand Down
19 changes: 6 additions & 13 deletions test/property/get_actor.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
const resolvedPathIndex = require.resolve(
`${parentDir}/dfx_generated/canister/index.js`
);
Expand All @@ -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
});
}

0 comments on commit 5d4d45a

Please sign in to comment.