Skip to content

Commit

Permalink
test: add example test of mocking hre artifacts
Browse files Browse the repository at this point in the history
We add a `createMockHardhatRuntimeEnvironment` that overrides the
standard artifacts built-in plugin and substitutes in a mock version
that can be leveraged in tests and development.
  • Loading branch information
kanej committed Sep 12, 2024
1 parent 8109bdd commit db90e56
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// import { HardhatError } from "@ignored/hardhat-vnext-errors";

// import { BUILTIN_GLOBAL_OPTIONS_DEFINITIONS } from "../../src/internal/builtin-global-options.js";
// import { builtinPlugins as originalBuiltinPlugins } from "../../src/internal/builtin-plugins/index.js";

// import "../../src/internal/builtin-plugins/artifacts/type-extensions.js";

// class MockArtifactsManager implements ArtifactsManager {
// readonly #artifacts: Map<string, Artifact>;

// constructor() {
// this.#artifacts = new Map();
// }

// public async readArtifact(
// contractNameOrFullyQualifiedName: string,
// ): Promise<Artifact> {
// const artifact = this.#artifacts.get(contractNameOrFullyQualifiedName);

// if (artifact === undefined) {
// throw new HardhatError(HardhatError.ERRORS.INTERNAL.ASSERTION_ERROR, {
// message:
// "Unable to find the artifact during mock readArtifact " +
// contractNameOrFullyQualifiedName,
// });
// }

// return artifact;
// }

// public artifactExists(
// _contractNameOrFullyQualifiedName: string,
// ): Promise<boolean> {
// throw new HardhatError(HardhatError.ERRORS.INTERNAL.ASSERTION_ERROR, {
// message: "Not implemented yet",
// });
// }

// public getAllFullyQualifiedNames(): Promise<string[]> {
// throw new HardhatError(HardhatError.ERRORS.INTERNAL.ASSERTION_ERROR, {
// message: "Not implemented yet",
// });
// }

// public getBuildInfo(
// _fullyQualifiedName: string,
// ): Promise<BuildInfo | undefined> {
// throw new HardhatError(HardhatError.ERRORS.INTERNAL.ASSERTION_ERROR, {
// message: "Not implemented yet",
// });
// }

// public getArtifactPaths(): Promise<string[]> {
// throw new HardhatError(HardhatError.ERRORS.INTERNAL.ASSERTION_ERROR, {
// message: "Not implemented yet",
// });
// }

// public getDebugFilePaths(): Promise<string[]> {
// throw new HardhatError(HardhatError.ERRORS.INTERNAL.ASSERTION_ERROR, {
// message: "Not implemented yet",
// });
// }

// public getBuildInfoPaths(): Promise<string[]> {
// throw new HardhatError(HardhatError.ERRORS.INTERNAL.ASSERTION_ERROR, {
// message: "Not implemented yet",
// });
// }

// public async saveArtifactAndDebugFile(
// artifact: Artifact,
// _pathToBuildInfo?: string,
// ): Promise<void> {
// this.#artifacts.set(artifact.contractName, artifact);
// }

// public saveBuildInfo(
// _solcVersion: string,
// _solcLongVersion: string,
// _input: CompilerInput,
// _output: CompilerOutput,
// ): Promise<string> {
// throw new HardhatError(HardhatError.ERRORS.INTERNAL.ASSERTION_ERROR, {
// message: "Not implemented yet",
// });
// }

// public formArtifactPathFromFullyQualifiedName(
// _fullyQualifiedName: string,
// ): string {
// throw new HardhatError(HardhatError.ERRORS.INTERNAL.ASSERTION_ERROR, {
// message: "Not implemented yet",
// });
// }

// public clearCache(): void {
// throw new HardhatError(HardhatError.ERRORS.INTERNAL.ASSERTION_ERROR, {
// message: "Not implemented yet",
// });
// }
// }

// const mockArtifactsPlugin: HardhatPlugin = {
// id: "artifacts",
// hookHandlers: {
// hre: async () => {
// return {
// extend: async (
// _context: HookContext,
// hre: HardhatRuntimeEnvironment,
// _next: (
// nextContext: HookContext,
// nextHre: HardhatRuntimeEnvironment,
// ) => Promise<HardhatRuntimeEnvironment>,
// ): Promise<HardhatRuntimeEnvironment> => {
// hre.artifacts = new MockArtifactsManager();

// const returnedFromNext = await _next(_context, hre);

// return returnedFromNext;
// },
// };
// },
// },
// };

// const mockedBuiltinPlugins: HardhatPlugin[] = [mockArtifactsPlugin];

// const builtinPlugins = originalBuiltinPlugins.map((plugin) => {
// const mockedPlugin = mockedBuiltinPlugins.find((mp) => mp.id === plugin.id);

// return mockedPlugin ?? plugin;
// });

// export async function createMockHardhatRuntimeEnvironment(
// config: HardhatUserConfig,
// userProvidedGlobalOptions: Partial<GlobalOptions> = {},
// projectRoot?: string,
// unsafeOptions: UnsafeHardhatRuntimeEnvironmentOptions = {},
// ): Promise<HardhatRuntimeEnvironment> {
// const resolvedProjectRoot = await resolveProjectRoot(projectRoot);

// if (unsafeOptions.resolvedPlugins === undefined) {
// const plugins = [...builtinPlugins, ...(config.plugins ?? [])];

// const resolvedPlugins = await resolvePluginList(
// resolvedProjectRoot,
// plugins,
// );

// unsafeOptions.resolvedPlugins = resolvedPlugins;
// }

// if (unsafeOptions.globalOptionDefinitions === undefined) {
// const pluginGlobalOptionDefinitions = buildGlobalOptionDefinitions(
// unsafeOptions.resolvedPlugins,
// );
// const globalOptionDefinitions = new Map([
// ...BUILTIN_GLOBAL_OPTIONS_DEFINITIONS,
// ...pluginGlobalOptionDefinitions,
// ]);

// unsafeOptions.globalOptionDefinitions = globalOptionDefinitions;
// }

// return createBaseHardhatRuntimeEnvironment(
// config,
// userProvidedGlobalOptions,
// resolvedProjectRoot,
// unsafeOptions,
// );
// }
61 changes: 61 additions & 0 deletions v-next/hardhat/test/internal/example-mock-plugin-using-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { Artifact } from "../../src/types/artifacts.js";
import type { HardhatRuntimeEnvironment } from "../../src/types/hre.js";
import type { HardhatPlugin } from "../../src/types/plugins.js";

import assert from "node:assert/strict";
import { describe, it } from "node:test";

import { task } from "../../src/config.js";
import { createHardhatRuntimeEnvironment } from "../../src/hre.js";

// import { createMockHardhatRuntimeEnvironment } from "../helpers/create-mock-hardhat-runtime-environment.js";

describe("createMockHardhatRuntimeEnvironment", () => {
it.skip("should allow plugins that leverage the artifact hre object", async () => {
// arrange

const myPlugin: HardhatPlugin = {
id: "my-plugin",
tasks: [
task("hello-artifact-using-world", "Tests artifact loading")
.setAction(async ({}, hre: HardhatRuntimeEnvironment) => {
return hre.artifacts.readArtifact("MyContract");
})
.build(),
],
};

// TODO: bring back the mock version
const mockHre = await createHardhatRuntimeEnvironment(
{
plugins: [myPlugin],
},
{},
undefined,
{},
);

const exampleArtifact: Artifact = {
_format: "hh-sol-artifact-1",
contractName: "MyContract",
sourceName: "source.sol",
abi: [],
bytecode: "0x",
linkReferences: {},
deployedBytecode: "0x",
deployedLinkReferences: {},
};

await mockHre.artifacts.saveArtifactAndDebugFile(exampleArtifact);

// act
const helloArtifactUsingWorld = mockHre.tasks.getTask(
"hello-artifact-using-world",
);

const result = await helloArtifactUsingWorld.run({});

// Assert
assert.equal(result, exampleArtifact);
});
});

0 comments on commit db90e56

Please sign in to comment.