Repository: defi-wonderland/smock
Pull request #66 (2021-09-03 19:27:19)

fix: correctly export assertion types
**Description**
Types were not being correctly exported when using matchers. Very minor fix but makes the matchers actually useful when using TypeScript.



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8ec602b..fff4fde 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,29 @@
 
 All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
 
+### [2.0.3](https://github.com/defi-wonderland/smock/compare/v2.0.0-rc.2...v2.0.3) (2021-09-07)
+
+
+### Features
+
+* add issue and PR templates ([#28](https://github.com/defi-wonderland/smock/issues/28)) ([fdcf8f3](https://github.com/defi-wonderland/smock/commit/fdcf8f3375fbd3a94c8a17f8e28b5b57193e4f34))
+* add the logo ([#39](https://github.com/defi-wonderland/smock/issues/39)) ([31deb0f](https://github.com/defi-wonderland/smock/commit/31deb0fcffb943e236605137ab479979862fd5a5))
+* converting readme to md for npm publish ([#41](https://github.com/defi-wonderland/smock/issues/41)) ([b364980](https://github.com/defi-wonderland/smock/commit/b364980ad3b011827dfec9836816a6281d3d081b))
+* mocks can be deployed with libs ([#68](https://github.com/defi-wonderland/smock/issues/68)) ([620f5a3](https://github.com/defi-wonderland/smock/commit/620f5a3bcc4b18e88e5770d254f3783e5be6d659))
+* propagate types when using connect function ([#61](https://github.com/defi-wonderland/smock/issues/61)) ([d263e43](https://github.com/defi-wonderland/smock/commit/d263e4393811c004fa991ddf95bc257ede428a2e))
+* whenCalledWith ([#59](https://github.com/defi-wonderland/smock/issues/59)) ([4e607fb](https://github.com/defi-wonderland/smock/commit/4e607fb8da50fe1e609e1308d25f3c7fd2c3e53c))
+* workflow for canary releases ([#38](https://github.com/defi-wonderland/smock/issues/38)) ([9c1c211](https://github.com/defi-wonderland/smock/commit/9c1c211a1a94a418ca57684bb51e2433f7c7eb04))
+
+
+### Bug Fixes
+
+* have canary release at 0.0.0-sha ([#42](https://github.com/defi-wonderland/smock/issues/42)) ([339d4ea](https://github.com/defi-wonderland/smock/commit/339d4ea1a6384d0d5248a5444b310aa34b5427c5))
+* no commit within canary release ([#40](https://github.com/defi-wonderland/smock/issues/40)) ([cb0b698](https://github.com/defi-wonderland/smock/commit/cb0b69856408d8be6fe481bccdaa6c8e91d98966))
+* reinit sandbox is case of an evm reset ([#57](https://github.com/defi-wonderland/smock/issues/57)) ([165e44d](https://github.com/defi-wonderland/smock/commit/165e44dba52e6a0e63e8149441af4953a114ab93))
+* renamed d.ts to ts ([#52](https://github.com/defi-wonderland/smock/issues/52)) ([53fecbf](https://github.com/defi-wonderland/smock/commit/53fecbfeb050d9e8688238e52b77d1ff8987ff9a))
+* tag release as canary ([#43](https://github.com/defi-wonderland/smock/issues/43)) ([55eb963](https://github.com/defi-wonderland/smock/commit/55eb963f9699f50b3a7f101e3ee0f581ca980b6c))
+* update package.json contrib field ([#35](https://github.com/defi-wonderland/smock/issues/35)) ([8a06b96](https://github.com/defi-wonderland/smock/commit/8a06b96cc49175334165bea0152d11162f6918fa)), closes [#34](https://github.com/defi-wonderland/smock/issues/34)
+
 ### [2.0.1](https://github.com/defi-wonderland/smock/compare/v2.0.0-rc.2...v2.0.1) (2021-08-10)
 
 
diff --git a/package.json b/package.json
index 7ca5daf..4ec3aa1 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "@defi-wonderland/smock",
-  "version": "2.0.2",
+  "version": "2.0.3",
   "description": "The Solidity mocking library",
   "keywords": [
     "ethereum",
diff --git a/src/chai-plugin/assertion.ts b/src/chai-plugin/types.ts
similarity index 94%
rename from src/chai-plugin/assertion.ts
rename to src/chai-plugin/types.ts
index 55d903e..4556d64 100644
--- a/src/chai-plugin/assertion.ts
+++ b/src/chai-plugin/types.ts
@@ -57,7 +57,3 @@ declare global {
     }
   }
 }
-
-declare const sinonChai: Chai.ChaiPlugin;
-declare namespace sinonChai {}
-export = sinonChai;
diff --git a/src/factories/smock-contract.ts b/src/factories/smock-contract.ts
index 651a1c9..54d6c09 100644
--- a/src/factories/smock-contract.ts
+++ b/src/factories/smock-contract.ts
@@ -1,4 +1,5 @@
 import Message from '@nomiclabs/ethereumjs-vm/dist/evm/message';
+import { FactoryOptions } from '@nomiclabs/hardhat-ethers/types';
 import { BaseContract, ContractFactory, ethers } from 'ethers';
 import { Interface } from 'ethers/lib/utils';
 import { ethers as hardhatEthers } from 'hardhat';
@@ -33,9 +34,10 @@ export async function createFakeContract<Contract extends BaseContract>(
 
 export async function createMockContractFactory<T extends ContractFactory>(
   vm: ObservableVM,
-  contractName: string
+  contractName: string,
+  signerOrOptions?: ethers.Signer | FactoryOptions
 ): Promise<MockContractFactory<T>> {
-  const factory = (await hardhatEthers.getContractFactory(contractName)) as unknown as MockContractFactory<T>;
+  const factory = (await hardhatEthers.getContractFactory(contractName, signerOrOptions)) as unknown as MockContractFactory<T>;
 
   const realDeploy = factory.deploy;
   factory.deploy = async (...args: Parameters<T['deploy']>) => {
diff --git a/src/index.ts b/src/index.ts
index 74a6037..053fdd2 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,6 +1,8 @@
-import { BaseContract, ContractFactory } from 'ethers';
+import { FactoryOptions } from '@nomiclabs/hardhat-ethers/types';
+import { BaseContract, ContractFactory, ethers } from 'ethers';
 import hre from 'hardhat';
 import { matchers } from './chai-plugin/matchers';
+import './chai-plugin/types';
 import { Sandbox } from './sandbox';
 import { FakeContract, FakeContractOptions, FakeContractSpec, MockContractFactory } from './types';
 import { getHardhatBaseProvider } from './utils';
@@ -12,10 +14,13 @@ async function fake<T extends BaseContract>(spec: FakeContractSpec, opts: FakeCo
   return await sandbox.fake(spec, opts);
 }
 
-async function mock<T extends ContractFactory>(contractName: string): Promise<MockContractFactory<T>> {
+async function mock<T extends ContractFactory>(
+  contractName: string,
+  signerOrOptions?: ethers.Signer | FactoryOptions
+): Promise<MockContractFactory<T>> {
   if (!sandbox) await init();
 
-  return await sandbox.mock(contractName);
+  return await sandbox.mock(contractName, signerOrOptions);
 }
 
 async function init() {
diff --git a/src/sandbox.ts b/src/sandbox.ts
index d96f43a..6f1a0e9 100644
--- a/src/sandbox.ts
+++ b/src/sandbox.ts
@@ -1,5 +1,6 @@
 import VM from '@nomiclabs/ethereumjs-vm';
-import { BaseContract, ContractFactory } from 'ethers';
+import { FactoryOptions } from '@nomiclabs/hardhat-ethers/types';
+import { BaseContract, ContractFactory, ethers } from 'ethers';
 import hre from 'hardhat';
 import { ethersInterfaceFromSpec } from './factories/ethers-interface';
 import { createFakeContract, createMockContractFactory } from './factories/smock-contract';
@@ -44,8 +45,11 @@ export class Sandbox {
     );
   }
 
-  async mock<T extends ContractFactory>(contractName: string): Promise<MockContractFactory<T>> {
-    return createMockContractFactory(this.vm, contractName);
+  async mock<T extends ContractFactory>(
+    contractName: string,
+    signerOrOptions?: ethers.Signer | FactoryOptions
+  ): Promise<MockContractFactory<T>> {
+    return createMockContractFactory(this.vm, contractName, signerOrOptions);
   }
 
   static async create(): Promise<Sandbox> {


Test patch:
diff --git a/test/contracts/mock/Librarian.sol b/test/contracts/mock/Librarian.sol
new file mode 100644
index 0000000..e9f3fda
--- /dev/null
+++ b/test/contracts/mock/Librarian.sol
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: MIT
+pragma solidity ^0.8.4;
+
+import './TestLibrary.sol';
+
+contract Librarian {
+  function getLibValue() external pure returns (uint256) {
+    return TestLibrary.getSomeValue();
+  }
+}
diff --git a/test/contracts/mock/TestLibrary.sol b/test/contracts/mock/TestLibrary.sol
new file mode 100644
index 0000000..7052cd4
--- /dev/null
+++ b/test/contracts/mock/TestLibrary.sol
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: MIT
+pragma solidity ^0.8.4;
+
+library TestLibrary {
+  function getSomeValue() external pure returns (uint256) {
+    return 10;
+  }
+}
diff --git a/test/unit/mock/initialization.spec.ts b/test/unit/mock/initialization.spec.ts
new file mode 100644
index 0000000..d600942
--- /dev/null
+++ b/test/unit/mock/initialization.spec.ts
@@ -0,0 +1,39 @@
+import { BigNumber } from '@ethersproject/bignumber';
+import { smock } from '@src';
+import { Librarian__factory, TestLibrary__factory } from '@typechained';
+import chai, { expect } from 'chai';
+import { ethers } from 'hardhat';
+
+chai.use(smock.matchers);
+
+describe('Mock: Initialization', () => {
+  it('should be able to use libraries', async () => {
+    const testLibrary = await (await ethers.getContractFactory('TestLibrary')).deploy();
+    const librarian = await (
+      await smock.mock<Librarian__factory>('Librarian', {
+        libraries: {
+          TestLibrary: testLibrary.address,
+        },
+      })
+    ).deploy();
+
+    expect(await librarian.getLibValue()).to.equal(10);
+  });
+
+  // TODO: make it work
+  it.skip('should be able to use mocked libraries', async () => {
+    const testLibrary = await (await smock.mock<TestLibrary__factory>('TestLibrary')).deploy();
+    const librarian = await (
+      await smock.mock<Librarian__factory>('Librarian', {
+        libraries: {
+          TestLibrary: testLibrary.address,
+        },
+      })
+    ).deploy();
+
+    const mockValue = BigNumber.from(123);
+    testLibrary.getSomeValue.returns(mockValue);
+
+    expect(await librarian.getLibValue()).to.equal(mockValue);
+  });
+});