Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
contract-tests: fix detetNetwork test
Browse files Browse the repository at this point in the history
- guard this.interfactAdapter in  implementation
- await  in tests as per documentation
- remove 3rd positional parameter, message, to  so expected and got comparison is shown on failures
  • Loading branch information
cds-amal committed Feb 16, 2022
1 parent 4e7159a commit 8838bb7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
50 changes: 22 additions & 28 deletions packages/contract-tests/test/contract/constructorMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,55 +32,51 @@ describe("TruffleContract.new()", () => {
});

describe("TruffleContract.at()", () => {
it("throws if passed an invalid address", () => {
it("throws if passed an invalid address", async () => {
const freshTruffleContract = TruffleContract();
assert.rejects(
await assert.rejects(
async () => {
await freshTruffleContract.at();
},
{
name: "Error",
message: /(Invalid address passed)/
},
"should have thrown!"
}
);

assert.rejects(
await assert.rejects(
async () => {
await freshTruffleContract.at(12345);
},
{
name: "Error",
message: /(Invalid address passed)/
},
"should have thrown!"
}
);

assert.rejects(
await assert.rejects(
async () => {
await freshTruffleContract.at("0x000323332");
},
{
name: "Error",
message: /(Invalid address passed)/
},
"should have thrown!"
}
);
});
});

describe("TruffleContract.deployed()", () => {
it("throws if called before setting a provider", () => {
it("throws if called before setting a provider", async () => {
const freshTruffleContract = TruffleContract();
assert.rejects(
await assert.rejects(
async () => {
await freshTruffleContract.deployed();
},
{
name: "Error",
message: /(Please call setProvider\(\) first)/
},
"should have thrown!"
}
);
});

Expand All @@ -95,15 +91,14 @@ describe("TruffleContract.deployed()", () => {
await freshTruffleContract.detectNetwork();
freshTruffleContract.networks[freshTruffleContract.network_id] =
"fakeNetworkRecord";
assert.rejects(
await assert.rejects(
async () => {
await freshTruffleContract.deployed();
},
{
name: "Error",
message: /(Contract).*(not).*(deployed to detected network)/
},
"should have thrown!"
}
);
});
});
Expand All @@ -123,30 +118,30 @@ describe("TruffleContract.isDeployed()", () => {
});
});

//TODO: fails on node16 due to provider being undefined and truffle
//tries to access undefined.method...
describe("TruffleContract.detectNetwork()", () => {
it("throws when provider not present", () => {
it("throws when provider not present", async () => {
const freshTruffleContract = TruffleContract();
assert.rejects(
await assert.rejects(
async () => await freshTruffleContract.detectNetwork(),
{
name: "Error",
message: /(Provider not set or invalid)/
},
"should have thrown!"
}
);
});
});

describe("TruffleContract.detectNetwork()", () => {
it("throws when network not set and provider not present", () => {
it("throws when network not set and provider not present", async () => {
const freshTruffleContract = TruffleContract();
assert.rejects(
await assert.rejects(
async () => await freshTruffleContract.detectNetwork(),
{
name: "Error",
message: /(Provider not set or invalid)/
},
"should have thrown!"
}
);
});

Expand All @@ -155,13 +150,12 @@ describe("TruffleContract.detectNetwork()", () => {
freshTruffleContract.network_id = 1234;
freshTruffleContract.networks[freshTruffleContract.network_id] =
"dummyNetwork";
assert.rejects(
await assert.rejects(
async () => await freshTruffleContract.detectNetwork(),
{
name: "Error",
message: /(Provider not set or invalid)/
},
"should have thrown!"
}
);
});
});
Expand Down
4 changes: 4 additions & 0 deletions packages/contract/lib/contract/constructorMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ module.exports = Contract => ({
},

async detectNetwork() {
// guard interfaceAdapter!
if (!Boolean(this.interfaceAdapter)) {
throw new Error("Provider not set or invalid");
}
// if artifacts already have a network_id and network configuration synced,
// use that network and use latest block gasLimit
if (this.network_id && this.networks[this.network_id] != null) {
Expand Down

0 comments on commit 8838bb7

Please sign in to comment.