diff --git a/.env b/.env new file mode 100644 index 000000000..b137845cf --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +# Used by testing - both the CI & local mode need this variable to setup & connect to funded accounts +MNEMONIC="test test test test test test test test test test test junk" \ No newline at end of file diff --git a/.github/workflows/reusable-test.yaml b/.github/workflows/reusable-test.yaml index 546ec30a6..3ce616ca1 100644 --- a/.github/workflows/reusable-test.yaml +++ b/.github/workflows/reusable-test.yaml @@ -42,8 +42,26 @@ jobs: - name: Build run: pnpm build + test: + name: Test + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v3 + + - name: Setup environment + uses: ./.github/workflows/actions/setup-environment + + - name: Install dependencies + uses: ./.github/workflows/actions/install-dependencies + with: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + + - name: Setup build cache + uses: ./.github/workflows/actions/setup-build-cache + - name: Test - run: pnpm test + run: pnpm test:ci # Since we're running the E2E tests in docker, we'll need to reinstall # the node modules. This is a temporary solution to bridge us between # non-containerized and containerized development diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 99d02ce66..1af6b9946 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -52,38 +52,66 @@ pnpm dev --filter=create-lz-oapp... ### Running tests -The tests are by default executed in a containerized environment that sets up two `hardhat` nodes accessible from within the environment: +There are two options when it comes to running tests: + +- [CI mode](#running-tests--ci-mode) +- [Local mode](#running-tests--local-mode) + +#### CI mode + +In the CI mode, the tests are ran inside a docker container based on the provided `Dockerfile`. You can run the tests in the CI mode by running: + +```bash +pnpm test:ci +``` + +In the CI mode, the containers are rebuilt before every test run. [See below for how to refine the packages](#running-tests--refining-tested-packages) for which the tests will be run. [See below for how to adjust the `docker run` command behavior](#running-tests--adjusting-docker-commands). + +In the CI mode the environment sets up two `hardhat` nodes only accessible from within the environment: - `http://network-britney:8545` - `http://network-vengaboys:8545` -You can run the whole test suite within this environment by running: +Their URLs are exposed to the containers under `NETWORK_URL_BRITNEY` and `NETWORK_URL_VENGABOYS` environment variables. The accounts on these networks are funded based on the `MNEMONIC` environment variable that's also exposed to the containers. + +#### Local mode + +In the local mode, the tests are executed on the developer machine. You can run the tests in the local mode by running: ```bash -pnpm test +pnpm test:local ``` -#### Refining tested packages +In the local mode the environment sets up two `hardhat` nodes accessible from the host machine: + +- `http://localhost:10001` +- `http://localhost:10002` + +The accounts on these networks are funded based on the `MNEMONIC` environment variable that is set based on the `.env` file. + +#### Refining tested packages To only run a specific test suite, you can define `DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS` environment variable before running the tests. This variable will be passed to the underlying `turbo` command and can contain any arguments that this command understands, for example: ```bash # To only run tests for @layerzerolabs/ua-devtools-evm-hardhat-test package -DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS=--filter=ua-devtools-evm-hardhat-test pnpm test +DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS=--filter=ua-devtools-evm-hardhat-test pnpm test:ci ``` -#### Rebuilding containers +#### Adjusting docker commands + +The test commands use `docker compose up` and `docker compose run` commands to setup necessary services. If you need to change the behavior of these commands by passing some CLI arguments, you can do so using the `DOCKER_COMPOSE_ARGS` environment variable. -`docker compose` will by default reuse images for containers it has already built. If by any chance you are seeing code changes not being reflected in your test runs, you can force docker to rebuild the images by defining `DOCKER_COMPOSE_RUN_TESTS_ARGS` environment variable. This variable will be passed to the underlying `docker compose run` command and can contain any arguments that this command understands, for example: +`docker compose` will by default reuse images for containers it has already built. If by any chance you are seeing code changes not being reflected in your local mode test runs, you can force docker to rebuild the images by defining `DOCKER_COMPOSE_ARGS` environment variable. This variable will be passed to the underlying `docker compose run` command and can contain any arguments that this command understands, for example: ```bash -DOCKER_COMPOSE_RUN_TESTS_ARGS=--build pnpm test +DOCKER_COMPOSE_ARGS=--build pnpm test:local ``` You also combine the environment variables: ```bash -DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS=--filter=ua-devtools-evm-hardhat-test DOCKER_COMPOSE_RUN_TESTS_ARGS=--build pnpm test +DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS=--filter=ua-devtools-evm-hardhat-test DOCKER_COMPOSE_ARGS=--build pnpm test:local ``` #### Container logs @@ -91,7 +119,7 @@ DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS=--filter=ua-devtools-evm-hardhat-test DOCKER To monitor the container logs you'll need to run: ```bash -docker compose logs -f +pnpm logs ``` This allows you to monitor logs coming from e.g. the `hardhat` nodes diff --git a/bin/env b/bin/env new file mode 100755 index 000000000..bb9d92801 --- /dev/null +++ b/bin/env @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +# Helper for expanding env files to a local shell +# +# Usage: +# +# . bin/env +# . bin/env .env.local +# +# (The "." will execute this inside the current shell instead of creating a child one) + +set -a +source ${1:=.env} +set +a \ No newline at end of file diff --git a/docker-compose.templates.yaml b/docker-compose.templates.yaml index c853434d7..e5636e7f4 100644 --- a/docker-compose.templates.yaml +++ b/docker-compose.templates.yaml @@ -32,7 +32,7 @@ services: # We'll provide a single testing MNEMONIC for the project so that the test EVM nodes # account are in sync with the hardhat accounts environment: - - MNEMONIC=test test test test test test test test test test test junk + - MNEMONIC=${MNEMONIC} logging: driver: local options: diff --git a/docker-compose.yaml b/docker-compose.yaml index f8f456c11..117ae906d 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -55,8 +55,6 @@ services: - NETWORK_URL_VENGABOYS=http://network-vengaboys:8545 volumes: - ./node_modules/.cache/turbo:/app/node_modules/.cache/turbo - - ./packages:/app/packages - - ./tests:/app/tests # Hardhat has an issue with caching compilers inside a docker container, # failing with EACCES -13 error, pointing to a permissions issue with the cache folder. # @@ -64,4 +62,4 @@ services: # as a cache directory for hardhat so that the permissions # don't clash - ./.cache/hardhat:/root/.cache/hardhat-nodejs - command: "pnpm turbo test $DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS" + command: "pnpm test" diff --git a/package.json b/package.json index dcaef163e..e9e748b15 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,14 @@ "clean": "$npm_execpath turbo run clean", "dev": "$npm_execpath turbo run dev", "lint": "$npm_execpath turbo run lint", + "logs": "docker compose logs -f", "prepare": "husky install", - "start": "docker compose -f docker-compose.yaml -f docker-compose.local.yaml up network-britney network-vengaboys", + "start": "docker compose -f docker-compose.yaml -f docker-compose.local.yaml up network-britney network-vengaboys --wait $DOCKER_COMPOSE_ARGS", "stop": "docker compose down", - "test": "docker compose run --rm $DOCKER_COMPOSE_RUN_TESTS_ARGS tests" + "pretest": "$npm_execpath build", + "test": "$npm_execpath turbo run test $DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS", + "test:ci": "docker compose run --build --rm $DOCKER_COMPOSE_ARGS tests", + "test:local": ". bin/env && $npm_execpath start && $npm_execpath test" }, "lint-staged": { "**/*": "pnpm prettier --write --ignore-unknown" diff --git a/packages/io-devtools/src/filesystem/filesystem.ts b/packages/io-devtools/src/filesystem/filesystem.ts index e7888badc..b65d81a9e 100644 --- a/packages/io-devtools/src/filesystem/filesystem.ts +++ b/packages/io-devtools/src/filesystem/filesystem.ts @@ -1,5 +1,5 @@ +import { createModuleLogger } from '@/stdio/logger' import { accessSync, constants, lstatSync } from 'fs' -import { createModuleLogger } from '..' export const isDirectory = (path: string): boolean => { try { diff --git a/tests/devtools-evm-test/package.json b/tests/devtools-evm-test/package.json index 6fe03bd66..76a956b36 100644 --- a/tests/devtools-evm-test/package.json +++ b/tests/devtools-evm-test/package.json @@ -11,8 +11,8 @@ "license": "MIT", "scripts": { "clean": "rm -rf artifacts cache", + "compile": "$npm_execpath hardhat compile", "lint": "$npm_execpath eslint '**/*.{js,ts,json}'", - "pretest": "$npm_execpath hardhat compile", "test": "jest" }, "devDependencies": { diff --git a/tests/ua-devtools-evm-hardhat-test/hardhat.config.ts b/tests/ua-devtools-evm-hardhat-test/hardhat.config.ts index 1d95bb380..a93ec5446 100644 --- a/tests/ua-devtools-evm-hardhat-test/hardhat.config.ts +++ b/tests/ua-devtools-evm-hardhat-test/hardhat.config.ts @@ -7,9 +7,7 @@ import type { HardhatUserConfig } from 'hardhat/types' // using your own keyboard (using exposed networks) import './tasks' -// We will default the mnemonic to the value we use in docker compose -// for easy exposed network workflow -const MNEMONIC = process.env.MNEMONIC ?? 'test test test test test test test test test test test junk' +const MNEMONIC = process.env.MNEMONIC ?? '' /** * This is a dummy hardhat config that enables us to test diff --git a/tests/ua-devtools-evm-hardhat-test/jest.config.js b/tests/ua-devtools-evm-hardhat-test/jest.config.js index ff17d9c22..9b43c323b 100644 --- a/tests/ua-devtools-evm-hardhat-test/jest.config.js +++ b/tests/ua-devtools-evm-hardhat-test/jest.config.js @@ -3,7 +3,7 @@ module.exports = { preset: 'ts-jest', cache: false, testEnvironment: 'node', - testTimeout: 150000, + testTimeout: 300_000, moduleNameMapper: { '^@/(.*)$': '/src/$1', }, diff --git a/tests/ua-devtools-evm-hardhat-test/package.json b/tests/ua-devtools-evm-hardhat-test/package.json index 35f67a33d..560101c80 100644 --- a/tests/ua-devtools-evm-hardhat-test/package.json +++ b/tests/ua-devtools-evm-hardhat-test/package.json @@ -10,9 +10,10 @@ }, "license": "MIT", "scripts": { + "clean": "rm -rf artifacts cache deployments", + "compile": "$npm_execpath hardhat compile", "lint": "$npm_execpath eslint '**/*.{js,ts,json}'", - "pretest": "$npm_execpath hardhat compile", - "test": "jest --runInBand --forceExit --verbose" + "test": "jest --runInBand --forceExit" }, "devDependencies": { "@babel/core": "^7.23.7", diff --git a/tests/ua-devtools-evm-hardhat-test/test/omnicounter/options.test.ts b/tests/ua-devtools-evm-hardhat-test/test/omnicounter/options.test.ts index 6094c7241..a2ac8ae61 100644 --- a/tests/ua-devtools-evm-hardhat-test/test/omnicounter/options.test.ts +++ b/tests/ua-devtools-evm-hardhat-test/test/omnicounter/options.test.ts @@ -89,9 +89,12 @@ describe('oapp/options', () => { let avaxPoint: OmniPoint let contractFactory: OmniContractFactoryHardhat - beforeEach(async () => { + beforeAll(async () => { await deployOmniCounterFixture() await setupDefaultEndpoint() + }) + + beforeEach(async () => { contractFactory = createConnectedContractFactory() const sdkFactory = createOmniCounterFactory(contractFactory) const signerFactory = createSignerFactory() @@ -165,7 +168,8 @@ describe('oapp/options', () => { expect(packetSentEvents).toHaveLength(1) expect(packetSentEvents[0]!.args.options.toLowerCase() === options.toHex().toLowerCase()) } - ) + ), + { numRuns: 20 } ) }) @@ -189,7 +193,8 @@ describe('oapp/options', () => { expect(packetSentEvents).toHaveLength(1) expect(packetSentEvents[0]!.args.options.toLowerCase() === options.toHex().toLowerCase()) } - ) + ), + { numRuns: 20 } ) }) @@ -208,7 +213,8 @@ describe('oapp/options', () => { expect(packetSentEvents).toHaveLength(1) expect(packetSentEvents[0]!.args.options.toLowerCase() === options.toHex().toLowerCase()) } - ) + ), + { numRuns: 20 } ) }) @@ -225,7 +231,8 @@ describe('oapp/options', () => { expect(packetSentEvents).toHaveLength(1) expect(packetSentEvents[0]!.args.options.toLowerCase() === options.toHex().toLowerCase()) } - ) + ), + { numRuns: 10 } ) }) diff --git a/turbo.json b/turbo.json index 2d576df3f..f40fe6134 100644 --- a/turbo.json +++ b/turbo.json @@ -29,8 +29,7 @@ }, "test": { "cache": false, - "outputs": [], - "dependsOn": ["^build"] + "outputs": [] } }, "globalDependencies": ["tsconfig.json"]