|
| 1 | +import type { HardhatRuntimeEnvironment } from "@nomicfoundation/hardhat-core/types/hre"; |
| 2 | + |
| 3 | +import assert from "node:assert/strict"; |
| 4 | +import { before, describe, it } from "node:test"; |
| 5 | + |
| 6 | +import { createHardhatRuntimeEnvironment } from "@nomicfoundation/hardhat-core"; |
| 7 | +import { HardhatError } from "@nomicfoundation/hardhat-errors"; |
| 8 | + |
| 9 | +import { runScriptWithHardhat } from "../../../../src/internal/builtin-plugins/run/runScriptWithHardhat.js"; |
| 10 | +import { useFixtureProject } from "../../../helpers/project.js"; |
| 11 | + |
| 12 | +describe("runScriptWithHardhat", function () { |
| 13 | + let hre: HardhatRuntimeEnvironment; |
| 14 | + |
| 15 | + before(async function () { |
| 16 | + hre = await createHardhatRuntimeEnvironment({}); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should throw if script is not a string", async function () { |
| 20 | + await assert.rejects(async () => { |
| 21 | + await runScriptWithHardhat({ script: 123, noCompile: false }, hre); |
| 22 | + }, /An internal invariant was violated: Expected script to be a string/); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should throw if noCompile is not a boolean", async function () { |
| 26 | + await assert.rejects(async () => { |
| 27 | + await runScriptWithHardhat({ script: "script.js", noCompile: 123 }, hre); |
| 28 | + }, /An internal invariant was violated: Expected noCompile to be a boolean/); |
| 29 | + }); |
| 30 | + |
| 31 | + describe("javascript", function () { |
| 32 | + useFixtureProject("run-js-script"); |
| 33 | + |
| 34 | + it("should throw if script does not exist", async function () { |
| 35 | + await assert.rejects( |
| 36 | + async () => { |
| 37 | + await runScriptWithHardhat( |
| 38 | + { script: "./scripts/non-existent.js", noCompile: false }, |
| 39 | + hre, |
| 40 | + ); |
| 41 | + }, |
| 42 | + new HardhatError(HardhatError.ERRORS.BUILTIN_TASKS.RUN_FILE_NOT_FOUND, { |
| 43 | + script: "./scripts/non-existent.js", |
| 44 | + }), |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should run a script successfully", async function () { |
| 49 | + await runScriptWithHardhat( |
| 50 | + { script: "./scripts/success.js", noCompile: false }, |
| 51 | + hre, |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should throw if the script throws", async function () { |
| 56 | + await assert.rejects( |
| 57 | + async () => { |
| 58 | + await runScriptWithHardhat( |
| 59 | + { script: "./scripts/throws.js", noCompile: false }, |
| 60 | + hre, |
| 61 | + ); |
| 62 | + }, |
| 63 | + new HardhatError(HardhatError.ERRORS.BUILTIN_TASKS.RUN_SCRIPT_ERROR, { |
| 64 | + script: "./scripts/throws.js", |
| 65 | + error: "broken script", |
| 66 | + }), |
| 67 | + ); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe("typescript", function () { |
| 72 | + useFixtureProject("run-ts-script"); |
| 73 | + |
| 74 | + it("should throw if script does not exist", async function () { |
| 75 | + await assert.rejects( |
| 76 | + async () => { |
| 77 | + await runScriptWithHardhat( |
| 78 | + { script: "./scripts/non-existent.ts", noCompile: false }, |
| 79 | + hre, |
| 80 | + ); |
| 81 | + }, |
| 82 | + new HardhatError(HardhatError.ERRORS.BUILTIN_TASKS.RUN_FILE_NOT_FOUND, { |
| 83 | + script: "./scripts/non-existent.ts", |
| 84 | + }), |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should run a script successfully", async function () { |
| 89 | + await runScriptWithHardhat( |
| 90 | + { script: "./scripts/success.ts", noCompile: false }, |
| 91 | + hre, |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it("should throw if the script throws", async function () { |
| 96 | + await assert.rejects( |
| 97 | + async () => { |
| 98 | + await runScriptWithHardhat( |
| 99 | + { script: "./scripts/throws.ts", noCompile: false }, |
| 100 | + hre, |
| 101 | + ); |
| 102 | + }, |
| 103 | + new HardhatError(HardhatError.ERRORS.BUILTIN_TASKS.RUN_SCRIPT_ERROR, { |
| 104 | + script: "./scripts/throws.ts", |
| 105 | + error: "broken script", |
| 106 | + }), |
| 107 | + ); |
| 108 | + }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments