Skip to content

Commit

Permalink
Update test environment (#973)
Browse files Browse the repository at this point in the history
* feat: custom test environment

* feat: custom test environment

* fix: adding TextDecoder to the custom env

* feat: replacing ws with isomorphic-ws

* feat: adding tests in browser env

* fix: fix failing tests

* fix: fix failing tests

* feat: adding browser tests in GitHub workflow

* feat: set custom test environment for browser

* chore: update sonar properties

* chore: set browser test environment config

* revert: hardhat plugin browser tests

* docs: updating readme

---------

Co-authored-by: Fabio Rigamonti <[email protected]>
Co-authored-by: Fabio Rigamonti <[email protected]>
  • Loading branch information
3 people authored Jun 12, 2024
1 parent 7115e99 commit 0179eb7
Show file tree
Hide file tree
Showing 24 changed files with 436 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/on-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ jobs:
unit-integration-test:
uses: ./.github/workflows/unit-integration-test.yml
secrets: inherit
unit-integration-test-browser:
uses: ./.github/workflows/unit-integration-test-browser.yml
secrets: inherit
sonar-scan:
uses: ./.github/workflows/sonar.yml
secrets: inherit
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/on-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ jobs:
uses: ./.github/workflows/unit-integration-test.yml
secrets: inherit

unit-integration-test-browser:
uses: ./.github/workflows/unit-integration-test-browser.yml
secrets: inherit

sonar-scan:
uses: ./.github/workflows/sonar.yml
secrets: inherit
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/unit-integration-test-browser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Run unit tests

on:
workflow_call:

jobs:
build:
name: Build & Lint
runs-on: ubuntu-latest
strategy:
matrix:
node: [18, lts/*, latest]
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}

- name: Install & Patch packages
run: yarn install

- name: Preliminary Stop of Thor solo node
id: preliminary-stop-solo
run: yarn stop-thor-solo

- name: Start Thor solo node
id: start-solo
run: yarn start-thor-solo

- name: Test
id: unit-test
run: yarn test:browser

- name: Stop Thor solo node
id: stop-solo
run: yarn stop-thor-solo
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ There are two ways to run tests:
- For a more straightforward approach, use `yarn test:solo`.
- This command handles the thor-Solo node's start and stop processes for you.


#### Running tests in a browser-like environment

The SDK fully support execution in a browser environment. To run the tests in a browser-like environment, you can use the `yarn test:browser` command. This command requires a local thor-solo node to be running.
Alternatively, you can run the tests with thor-solo by using the `yarn test:browser:solo` command. This command will start thor-solo, run the tests, and stop thor-solo at the end.



```bash


### Custom thor-solo Data Starting Point

For advanced testing scenarios, you may require a custom data starting point with thor-solo. This involves creating a custom snapshot of thor's LevelDB.
Expand Down
23 changes: 23 additions & 0 deletions customEnv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { TestEnvironment } = require('jest-environment-jsdom');

/**
* Jest creates a new VM context for each test and doesn't add in all
* the node globals.
* We rectify this here.
*/
class CustomizedTestEnvironment extends TestEnvironment {
constructor(config, context) {
super(config, context);
}

async setup() {
await super.setup();

// These seem to be needed for @noble/hashes
this.global.TextEncoder = globalThis.TextEncoder;
this.global.TextDecoder = globalThis.TextDecoder;
this.global.Uint8Array = globalThis.Uint8Array;
}
}

module.exports = CustomizedTestEnvironment;
2 changes: 1 addition & 1 deletion docs/examples/subscriptions/block-subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { subscriptions } from '@vechain/sdk-network';
import WebSocket from 'ws';
import WebSocket from 'isomorphic-ws';

// The URL of the node to request the subscription from.
const testnetUrl = 'https://testnet.vechain.org';
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/subscriptions/event-subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { subscriptions } from '@vechain/sdk-network';
import WebSocket from 'ws';
import WebSocket from 'isomorphic-ws';

// The URL of the node to request the subscription from.
const testnetUrl = 'https://testnet.vechain.org';
Expand Down
4 changes: 2 additions & 2 deletions docs/subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Subscribe to specific contract events through the `subscriptions.getEventSubscri

```typescript { name=event-subscriptions, category=example }
import { subscriptions } from '@vechain/sdk-network';
import WebSocket from 'ws';
import WebSocket from 'isomorphic-ws';

// The URL of the node to request the subscription from.
const testnetUrl = 'https://testnet.vechain.org';
Expand Down Expand Up @@ -88,7 +88,7 @@ Subscribe to new blocks as they are added to the blockchain through the `subscri

```typescript { name=block-subscriptions, category=example }
import { subscriptions } from '@vechain/sdk-network';
import WebSocket from 'ws';
import WebSocket from 'isomorphic-ws';

// The URL of the node to request the subscription from.
const testnetUrl = 'https://testnet.vechain.org';
Expand Down
1 change: 1 addition & 0 deletions docs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true,
},
"ts-node": {
"compilerOptions": {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
"test:examples": "turbo test:examples",
"test:examples:solo": "(yarn start-thor-solo && yarn test:examples && yarn stop-thor-solo) || yarn stop-thor-solo",
"test": "turbo test --force && yarn merge-coverage",
"test:browser": "turbo test:browser --force && yarn merge-coverage",
"test:solo": "(yarn start-thor-solo && yarn test && yarn stop-thor-solo) || yarn stop-thor-solo",
"test:browser:solo": "(yarn start-thor-solo && yarn test:browser && yarn stop-thor-solo) || yarn stop-thor-solo",
"merge-coverage": "ts-node scripts/merge-coverage.ts",
"check-tests-naming": "ts-node scripts/check-tests-naming.ts"
},
Expand All @@ -61,6 +63,7 @@
"husky": "^9.0.11",
"jest": "^29.7.0",
"jest-docblock": "^29",
"jest-environment-jsdom": "^29.7.0",
"jest-junit": "^16.0.0",
"jest-runner": "^29.7.0",
"jest-runner-groups": "^2.2.0",
Expand Down
9 changes: 9 additions & 0 deletions packages/core/jest.config.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: '../../customEnv.js',
coverageReporters: ['html', 'lcov', 'json'],
runner: 'groups',
reporters: ['default', 'jest-junit'],
workerThreads: true
};
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"format": "prettier --write src/**/*.ts tests/**/*.ts",
"test:unit": "rm -rf ./coverageUnit && jest --coverage --coverageDirectory=coverageUnit --group=unit",
"test:integration": "rm -rf ./coverageIntegration && jest --coverage --coverageDirectory=coverageIntegration --group=integration",
"test": "rm -rf ./coverage && jest --coverage --coverageDirectory=coverage --group=integration --group=unit"
"test": "rm -rf ./coverage && jest --coverage --coverageDirectory=coverage --group=integration --group=unit",
"test:browser": "rm -rf ./coverage && jest --coverage --coverageDirectory=coverage --group=integration --group=unit --config ./jest.config.browser.js"
},
"dependencies": {
"@ethereumjs/rlp": "^5.0.2",
Expand Down
8 changes: 8 additions & 0 deletions packages/errors/jest.config.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: '../../customEnv.js',
coverageReporters: ['html', 'lcov', 'json'],
runner: 'groups',
reporters: ['default', 'jest-junit']
};
5 changes: 3 additions & 2 deletions packages/errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"test:unit": "rm -rf ./coverageUnit && jest --coverage --coverageDirectory=coverageUnit --group=unit",
"test:integration": "rm -rf ./coverageIntegration && jest --coverage --coverageDirectory=coverageIntegration --group=integration",
"test:integration:solo": "(yarn start-thor-solo && yarn test:integration && yarn stop-thor-solo) || yarn stop-thor-solo",
"test": "rm -rf ./coverage && jest --coverage --coverageDirectory=coverage --group=integration --group=unit",
"test:solo": "(yarn start-thor-solo && yarn test && yarn stop-thor-solo) || yarn stop-thor-solo"
"test:browser": "rm -rf ./coverage && jest --coverage --coverageDirectory=coverage --group=integration --group=unit --config ./jest.config.browser.js",
"test:solo": "(yarn start-thor-solo && yarn test && yarn stop-thor-solo) || yarn stop-thor-solo",
"test": "rm -rf ./coverage && jest --coverage --coverageDirectory=coverage --group=integration --group=unit"
}
}
2 changes: 1 addition & 1 deletion packages/ethers-adapter/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testEnvironment: '../../customEnv.js',
coverageReporters: ['html', 'lcov', 'json'],
runner: 'groups',
reporters: ['default', 'jest-junit'],
Expand Down
9 changes: 9 additions & 0 deletions packages/logging/jest.config.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: '../../customEnv.js',
coverageReporters: ['html', 'lcov', 'json'],
runner: 'groups',
reporters: ['default', 'jest-junit'],
workerThreads: true
};
1 change: 1 addition & 0 deletions packages/logging/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"format": "prettier --write src/**/*.ts tests/**/*.ts",
"test:unit": "rm -rf ./coverageUnit && jest --coverage --coverageDirectory=coverageUnit --group=unit",
"test:integration": "rm -rf ./coverageIntegration && jest --coverage --coverageDirectory=coverageIntegration --group=integration",
"test:browser": "rm -rf ./coverage && jest --coverage --coverageDirectory=coverage --group=integration --group=unit --config ./jest.config.browser.js",
"test": "rm -rf ./coverage && jest --coverage --coverageDirectory=coverage --group=integration --group=unit"
},
"dependencies": {
Expand Down
15 changes: 15 additions & 0 deletions packages/network/jest.config.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: '../../customEnv.js',
testPathIgnorePatterns: [
'subscriptions',
'tests/utils/poll/event/event-poll.unit.test.ts',
'tests/utils/http/http-client.solo.test.ts',
'http-client.testnet.test.ts'
],
coverageReporters: ['html', 'lcov', 'json'],
runner: 'groups',
reporters: ['default', 'jest-junit'],
workerThreads: true
};
9 changes: 6 additions & 3 deletions packages/network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@
"test:unit": "rm -rf ./coverageUnit && jest --coverage --coverageDirectory=coverageUnit --group=unit",
"test:integration": "rm -rf ./coverageIntegration && jest --coverage --coverageDirectory=coverageIntegration --group=integration",
"test:integration:solo": "(yarn start-thor-solo && yarn test:integration && yarn stop-thor-solo) || yarn stop-thor-solo",
"test:browser": "rm -rf ./coverage && jest --coverage --coverageDirectory=coverage --group=integration --group=unit --config ./jest.config.browser.js",
"test": "rm -rf ./coverage && jest --coverage --coverageDirectory=coverage --group=integration --group=unit",
"test:solo": "(yarn start-thor-solo && yarn test && yarn stop-thor-solo) || yarn stop-thor-solo"
"test:solo": "(yarn start-thor-solo && yarn test && yarn stop-thor-solo) || yarn stop-thor-solo",
"test:browser:solo": "(yarn start-thor-solo && yarn test:browser && yarn stop-thor-solo) || yarn stop-thor-solo"
},
"dependencies": {
"@types/ws": "^8.5.10",
"@vechain/sdk-core": "1.0.0-beta.15",
"@vechain/sdk-errors": "1.0.0-beta.15",
"@vechain/sdk-logging": "1.0.0-beta.15",
"abitype": "^1.0.2",
"axios": "^1.7.2"
"axios": "^1.7.2",
"isomorphic-ws": "^5.0.0"
}
}
}
2 changes: 1 addition & 1 deletion packages/network/tests/utils/subscriptions/fixture.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { generateRandomValidAddress } from '../../../../core/tests/fixture';
import { vechain_sdk_core_ethers } from '@vechain/sdk-core';
import { TESTING_CONTRACT_ADDRESS } from '../../fixture';
import WebSocket from 'ws';
import WebSocket from 'isomorphic-ws';

/**
* random address for `from` parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
TESTING_CONTRACT_ADDRESS,
THOR_SOLO_ACCOUNTS_BASE_WALLET
} from '../../fixture';
import WebSocket from 'ws';
import WebSocket from 'isomorphic-ws';
import {
addressUtils,
clauseBuilder,
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ sonar.test.inclusions=packages/**/tests/**/*
sonar.javascript.lcov.reportPaths=coverage/**/lcov.info
sonar.coverage.exclusions= \
packages/**/index.ts, \
packages/**/*config.js
packages/**/jest*.js,
sonar.cpd.exclusions=packages/core/**/const/abi.ts
4 changes: 4 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"outputs": ["coverage/**"],
"dependsOn": ["^build"]
},
"test:browser": {
"outputs": ["coverage/**"],
"dependsOn": ["^build"]
},
"test:integration":{
"outputs": ["coverage/**"],
"dependsOn": ["^build"]
Expand Down
Loading

1 comment on commit 0179eb7

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 100%
100% (3408/3408) 100% (794/794) 100% (705/705)
Title Tests Skipped Failures Errors Time
core 498 0 💤 0 ❌ 0 🔥 1m 5s ⏱️
network 658 0 💤 0 ❌ 0 🔥 3m 50s ⏱️
errors 48 0 💤 0 ❌ 0 🔥 11.209s ⏱️

Please sign in to comment.