-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathindex.ts
270 lines (204 loc) · 8.65 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import type { HardhatRuntimeEnvironment } from "hardhat/types/hre";
import assert from "node:assert/strict";
import path from "node:path";
import { before, beforeEach, describe, it } from "node:test";
import { useFixtureProject } from "@nomicfoundation/hardhat-test-utils";
import {
exists,
readUtf8File,
remove,
} from "@nomicfoundation/hardhat-utils/fs";
import { createHardhatRuntimeEnvironment } from "hardhat/hre";
// Read the contract factory from the generated types
async function readContractFactory(contractName: string) {
const potentialPaths = [
`${process.cwd()}/types/ethers-contracts/factories/${contractName}__factory.ts`,
`${process.cwd()}/types/ethers-contracts/factories/${contractName}.sol/${contractName}__factory.ts`,
];
for (const potentialPath of potentialPaths) {
if (await exists(potentialPath)) {
return readUtf8File(potentialPath);
}
}
return undefined;
}
// Check the contract is typed in hardhat.d.ts
function isContractTyped(typeFileContents: string, contractName: string) {
const lookupStrings = [
`getContractFactory(name: '${contractName}'`,
`getContractAt(name: '${contractName}'`,
`deployContract(name: '${contractName}'`,
];
for (const lookupString of lookupStrings) {
if (!typeFileContents.includes(lookupString)) {
return false;
}
}
return true;
}
describe("hardhat-typechain", () => {
describe("check that types are generated correctly", () => {
const projectFolder = "generate-types";
let hre: HardhatRuntimeEnvironment;
let hardhatConfig: any;
useFixtureProject(projectFolder);
beforeEach(async () => {
await remove(`${process.cwd()}/types`);
hardhatConfig = await import(
// eslint-disable-next-line import/no-relative-packages -- allow for fixture projects
`./fixture-projects/${projectFolder}/hardhat.config.js`
);
hre = await createHardhatRuntimeEnvironment(hardhatConfig.default);
assert.equal(await exists(`${process.cwd()}/types`), false);
await hre.tasks.getTask("clean").run();
});
it("should generate the types for the `hardhat.d.ts` file", async () => {
await hre.tasks.getTask("compile").run();
// Check that the types are generated with the expected addition of the "/index.js" extensions
// and the v3 modules
const content = await readUtf8File(
path.join(process.cwd(), "types", "ethers-contracts", "hardhat.d.ts"),
);
// The overload target the v3 hardhat ethers package
assert.equal(
content.includes(
`declare module "@nomicfoundation/hardhat-ethers/types" {`,
),
true,
);
// The import should be from the v3 hardhat ethers package
assert.equal(
content.includes(`from "@nomicfoundation/hardhat-ethers/types";`),
true,
);
// A relative import should have the ".js" extension
assert.equal(
content.includes(`import * as Contracts from "./index.js"`),
true,
);
// The import from a npm package should have ".js" extensions
assert.equal(content.includes(`import { ethers } from 'ethers'`), true);
for (const contractName of ["A", "B"]) {
assert.notEqual(await readContractFactory(contractName), undefined);
assert.equal(isContractTyped(content, contractName), true);
}
});
it("should generated types for the contracts and add the support for the `attach` method", async () => {
await hre.tasks.getTask("compile").run();
const content = await readContractFactory("A");
if (content === undefined) {
throw new Error("Factory for A.sol not found");
}
// The "Addressable" type should be imported
assert.equal(
content.includes(`import type { Addressable } from "ethers";`),
true,
);
// The "attach" method should be added to the factory
assert.equal(
content.includes(`override attach(address: string | Addressable): A {`),
true,
);
});
it("doesnt lose types when compiling a subset of the contracts", async () => {
// First: compile only A.sol. Only A should be typed
await hre.tasks.getTask("compile").run({ files: ["contracts/A.sol"] });
assert.notEqual(await readContractFactory("A"), undefined);
assert.equal(await readContractFactory("B"), undefined);
let content = await readUtf8File(
path.join(process.cwd(), "types", "ethers-contracts", "hardhat.d.ts"),
);
assert.equal(isContractTyped(content, "A"), true);
assert.equal(isContractTyped(content, "B"), false);
// Second: compile only B.sol. Both A and B should be typed
await hre.tasks.getTask("compile").run({ files: ["contracts/B.sol"] });
assert.notEqual(await readContractFactory("A"), undefined);
assert.notEqual(await readContractFactory("B"), undefined);
content = await readUtf8File(
path.join(process.cwd(), "types", "ethers-contracts", "hardhat.d.ts"),
);
assert.equal(isContractTyped(content, "A"), true);
assert.equal(isContractTyped(content, "B"), true);
});
});
describe("typechain should not generate types during compilation", () => {
describe("when the configuration property dontOverrideCompile is set to true", () => {
const projectFolder = "skip-type-generation";
useFixtureProject(projectFolder);
it("should not generate the types", async () => {
const hardhatConfig = await import(
// eslint-disable-next-line import/no-relative-packages -- allow for fixture projects
`./fixture-projects/${projectFolder}/hardhat.config.js`
);
const hre = await createHardhatRuntimeEnvironment(
hardhatConfig.default,
);
assert.equal(await exists(`${process.cwd()}/types`), false);
await hre.tasks.getTask("clean").run();
await hre.tasks.getTask("compile").run();
assert.equal(await exists(`${process.cwd()}/types`), false);
});
});
describe("when the flag --no-typechain is passed", () => {
const projectFolder = "generate-types";
useFixtureProject(projectFolder);
before(async () => {
await remove(`${process.cwd()}/types`);
});
it("should not generate the types", async () => {
const hardhatConfig = await import(
// eslint-disable-next-line import/no-relative-packages -- allow for fixture projects
`./fixture-projects/${projectFolder}/hardhat.config.js`
);
const hre = await createHardhatRuntimeEnvironment(
hardhatConfig.default,
{
noTypechain: true,
},
);
assert.equal(await exists(`${process.cwd()}/types`), false);
await hre.tasks.getTask("clean").run();
await hre.tasks.getTask("compile").run();
assert.equal(await exists(`${process.cwd()}/types`), false);
});
});
});
describe("nothing to generate, no contract is compiled", () => {
const projectFolder = "nothing-to-generate";
useFixtureProject(projectFolder);
before(async () => {
await remove(`${process.cwd()}/types`);
});
it("should not generate the types", async () => {
const hardhatConfig = await import(
// eslint-disable-next-line import/no-relative-packages -- allow for fixture projects
`./fixture-projects/${projectFolder}/hardhat.config.js`
);
const hre = await createHardhatRuntimeEnvironment(hardhatConfig.default);
assert.equal(await exists(`${process.cwd()}/types`), false);
await hre.tasks.getTask("clean").run();
await hre.tasks.getTask("compile").run();
assert.equal(await exists(`${process.cwd()}/types`), false);
});
});
describe("generate types from contracts in a custom output directory", () => {
const projectFolder = "custom-out-dir";
useFixtureProject(projectFolder);
before(async () => {
await remove(`${process.cwd()}/custom-types`);
});
it("should generate the types", async () => {
const hardhatConfig = await import(
// eslint-disable-next-line import/no-relative-packages -- allow for fixture projects
`./fixture-projects/${projectFolder}/hardhat.config.js`
);
const hre = await createHardhatRuntimeEnvironment(hardhatConfig.default);
assert.equal(await exists(`${process.cwd()}/types`), false);
assert.equal(await exists(`${process.cwd()}/custom-types`), false);
await hre.tasks.getTask("clean").run();
await hre.tasks.getTask("compile").run();
assert.equal(await exists(`${process.cwd()}/custom-types`), true);
assert.equal(await exists(`${process.cwd()}/types`), false);
});
});
});