-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add example test of mocking hre artifacts
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
Showing
2 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
173 changes: 173 additions & 0 deletions
173
v-next/hardhat-test-utils/src/create-mock-hardhat-runtime-environment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
61
v-next/hardhat/test/internal/example-mock-plugin-using-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |