Skip to content

Commit

Permalink
🚧 Speed up tests (LayerZero-Labs#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Jan 5, 2024
1 parent 597a7b4 commit 4c1108e
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 19 additions & 1 deletion .github/workflows/reusable-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 38 additions & 10 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,46 +52,74 @@ 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 <a id="running-tests--ci-mode"></a>

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 <a id="running-tests--local-mode"></a>

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 <a id="running-tests--refining-tested-packages"></a>

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 <a id="running-tests--adjusting-docker-commands"></a>

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

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
Expand Down
14 changes: 14 additions & 0 deletions bin/env
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion docker-compose.templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@ 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.
#
# The workaround is to provide an outside volume to work
# 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"
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion packages/io-devtools/src/filesystem/filesystem.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/devtools-evm-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 1 addition & 3 deletions tests/ua-devtools-evm-hardhat-test/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/ua-devtools-evm-hardhat-test/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
preset: 'ts-jest',
cache: false,
testEnvironment: 'node',
testTimeout: 150000,
testTimeout: 300_000,
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
Expand Down
5 changes: 3 additions & 2 deletions tests/ua-devtools-evm-hardhat-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -165,7 +168,8 @@ describe('oapp/options', () => {
expect(packetSentEvents).toHaveLength(1)
expect(packetSentEvents[0]!.args.options.toLowerCase() === options.toHex().toLowerCase())
}
)
),
{ numRuns: 20 }
)
})

Expand All @@ -189,7 +193,8 @@ describe('oapp/options', () => {
expect(packetSentEvents).toHaveLength(1)
expect(packetSentEvents[0]!.args.options.toLowerCase() === options.toHex().toLowerCase())
}
)
),
{ numRuns: 20 }
)
})

Expand All @@ -208,7 +213,8 @@ describe('oapp/options', () => {
expect(packetSentEvents).toHaveLength(1)
expect(packetSentEvents[0]!.args.options.toLowerCase() === options.toHex().toLowerCase())
}
)
),
{ numRuns: 20 }
)
})

Expand All @@ -225,7 +231,8 @@ describe('oapp/options', () => {
expect(packetSentEvents).toHaveLength(1)
expect(packetSentEvents[0]!.args.options.toLowerCase() === options.toHex().toLowerCase())
}
)
),
{ numRuns: 10 }
)
})

Expand Down
3 changes: 1 addition & 2 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
},
"test": {
"cache": false,
"outputs": [],
"dependsOn": ["^build"]
"outputs": []
}
},
"globalDependencies": ["tsconfig.json"]
Expand Down

0 comments on commit 4c1108e

Please sign in to comment.