Skip to content

Commit

Permalink
fix: correctly close ws connection in web3-eth-contract integration t…
Browse files Browse the repository at this point in the history
…ests (#7338)
  • Loading branch information
krzysu authored Nov 11, 2024
1 parent 098ee6d commit 1724f35
Show file tree
Hide file tree
Showing 22 changed files with 487 additions and 347 deletions.
10 changes: 5 additions & 5 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"lerna": "4.0.0",
"npmClient": "yarn",
"useWorkspaces": true,
"version": "independent",
"packages": ["packages/*", "tools/*"]
"lerna": "4.0.0",
"npmClient": "yarn",
"useWorkspaces": true,
"version": "independent",
"packages": ["packages/*", "tools/*"]
}
4 changes: 2 additions & 2 deletions packages/web3-eth-contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
"format": "prettier --write '**/*'",
"test": "jest --config=./test/unit/jest.config.js",
"test:coverage:unit": "jest --config=./test/unit/jest.config.js --coverage=true --coverage-reporters=text",
"test:coverage:integration": "jest --config=./test/integration/jest.config.js --runInBand --forceExit --coverage=true --coverage-reporters=text",
"test:coverage:integration": "jest --config=./test/integration/jest.config.js --runInBand --coverage=true --coverage-reporters=text",
"test:ci": "jest --coverage=true --coverage-reporters=json --verbose",
"test:watch": "npm test -- --watch",
"test:unit": "jest --config=./test/unit/jest.config.js",
"test:integration": "jest --config=./test/integration/jest.config.js --runInBand --forceExit",
"test:integration": "jest --config=./test/integration/jest.config.js --runInBand",
"test:e2e:electron": "npx cypress run --headless --browser electron --env grep='ignore',invert=true",
"test:e2e:chrome": "npx cypress run --headless --browser chrome --env grep='ignore',invert=true",
"test:e2e:firefox": "npx cypress run --headless --browser firefox --env grep='ignore',invert=true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ import {
describeIf,
getSystemTestBackend,
BACKEND,
closeOpenConnection,
} from '../fixtures/system_test_utils';

describe('contract', () => {
describeIf(getSystemTestBackend() === BACKEND.GETH)('createAccessList', () => {
let contract: Contract<typeof GreeterAbi>;
let deployedContract: Contract<typeof GreeterAbi>;
let deployOptions: Record<string, unknown>;
let sendOptions: Record<string, unknown>;
let acc: { address: string; privateKey: string };

beforeEach(async () => {
beforeAll(async () => {
contract = new Contract(GreeterAbi, undefined, {
provider: getSystemTestProvider(),
});
Expand All @@ -46,10 +48,16 @@ describe('contract', () => {
sendOptions = { from: acc.address, gas: '1000000' };
});

it('create access list for setter', async () => {
const deployedContract = await contract.deploy(deployOptions).send(sendOptions);
afterAll(async () => {
await closeOpenConnection(contract);
});

beforeEach(async () => {
deployedContract = await contract.deploy(deployOptions).send(sendOptions);
deployedContract.defaultAccount = acc.address;
});

it('create access list for setter', async () => {
const receipt = await deployedContract.methods
.setGreeting('New Greeting')
.send({ gas: '1000000' });
Expand All @@ -75,9 +83,6 @@ describe('contract', () => {
});

it('create access list for getter', async () => {
const deployedContract = await contract.deploy(deployOptions).send(sendOptions);
deployedContract.defaultAccount = acc.address;

const receipt = await deployedContract.methods
.setGreeting('New Greeting')
.send({ gas: '1000000' });
Expand Down
13 changes: 11 additions & 2 deletions packages/web3-eth-contract/test/integration/contract_clone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
import { Contract } from '../../src';
import { GreeterBytecode, GreeterAbi } from '../shared_fixtures/build/Greeter';
import { getSystemTestProvider, createTempAccount } from '../fixtures/system_test_utils';
import {
getSystemTestProvider,
createTempAccount,
closeOpenConnection,
} from '../fixtures/system_test_utils';

describe('contract', () => {
describe('clone', () => {
let contract: Contract<typeof GreeterAbi>;
let deployOptions: Record<string, unknown>;
let sendOptions: Record<string, unknown>;

beforeAll(async () => {
contract = new Contract(GreeterAbi, undefined, {
provider: getSystemTestProvider(),
Expand All @@ -34,7 +39,11 @@ describe('contract', () => {
arguments: ['My Greeting'],
};

sendOptions = { from: acc.address, gas: '1000000' };
sendOptions = { from: acc.address };
});

afterAll(async () => {
await closeOpenConnection(contract);
});

it('should clone the contract but with same address', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import { Web3Context } from 'web3-core';

import { Contract } from '../../src';
import { GreeterBytecode, GreeterAbi } from '../shared_fixtures/build/Greeter';
import { getSystemTestProvider, createTempAccount } from '../fixtures/system_test_utils';
import {
getSystemTestProvider,
createTempAccount,
closeOpenConnection,
} from '../fixtures/system_test_utils';

describe('contract', () => {
describe('defaults', () => {
Expand All @@ -43,6 +47,10 @@ describe('contract', () => {
sendOptions = { from: acc.address, gas: '1000000' };
});

afterEach(async () => {
await closeOpenConnection(contract);
});

it('should use "defaultAccount" on "instance" level instead of "from"', async () => {
const deployedContract = await contract.deploy(deployOptions).send(sendOptions);
// eslint-disable-next-line prefer-destructuring
Expand All @@ -63,23 +71,27 @@ describe('contract', () => {
});

it('should set syncWithContext from init options', async () => {
contract = new Contract(GreeterAbi, {
const testContract = new Contract(GreeterAbi, {
provider: getSystemTestProvider(),
syncWithContext: true,
});

contract = await contract.deploy(deployOptions).send(sendOptions);
const deployedContract = await testContract.deploy(deployOptions).send(sendOptions);

expect(contract.syncWithContext).toBeTruthy();
expect(deployedContract.syncWithContext).toBeTruthy();

await closeOpenConnection(testContract);
});

it('should subscribe to provided context upon instantiation', () => {
it('should subscribe to provided context upon instantiation', async () => {
const web3Context = new Web3Context('http://127.0.0.1:8545');
const _contract = new Contract([], { syncWithContext: true }, web3Context);
expect(_contract.defaultBlock).toBe('latest');

web3Context.defaultBlock = 'earliest';
expect(_contract.defaultBlock).toBe('earliest');

await closeOpenConnection(_contract);
});

describe('defaultBlock', () => {
Expand Down
Loading

0 comments on commit 1724f35

Please sign in to comment.