Skip to content

Commit

Permalink
feat: add e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasbrugneaux committed Oct 16, 2024
1 parent 93cb53b commit 8d0290e
Show file tree
Hide file tree
Showing 20 changed files with 639 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"access": "public",
"baseBranch": "master",
"updateInternalDependencies": "patch",
"ignore": [],
"ignore": [
"@celo/e2e"
],
"snapshot": {
"useCalculatedVersion": true,
"prereleaseTemplate": "{tag}-{commit}"
Expand Down
48 changes: 48 additions & 0 deletions .github/actions/third-party-tests/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Third party libraries testing workflow
description: "Tests one library at a time"
inputs:
library:
required: true
version:
default: 'latest'
description: Version to test again or 'latest'
required: false
runs:
using: "composite"
steps:
- uses: actions/setup-node@v4
with:
node-version: '18'
- name: 'enable corepack for yarn'
run: sudo corepack enable yarn
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'yarn'
- name: Restore node cache
uses: actions/cache@v4
id: cache_node
with:
# We need to cache all the artifacts generated by yarn install+build
# Update this list also in .github/actions/sync-workspace/action.yml with exactly the same list
path: |
./.yarn/cache
./.yarn/install-state.gz
node_modules
packages/**/node_modules
key: node-${{ runner.os }}-${{ runner.arch }}-${{ env.NODE_MODULE_CACHE_VERSION }}-${{ hashFiles('yarn.lock') }}
restore-keys: |
node-${{ runner.os }}-${{ runner.arch }}-${{ env.NODE_MODULE_CACHE_VERSION }}-
- name: Install yarn dependencies
run: git config --global url."https://".insteadOf ssh:// && yarn install
if: steps.cache_node.outputs.cache-hit != 'true'
- name: Run yarn postinstall if cache hitted
run: yarn run postinstall
if: steps.cache_node.outputs.cache-hit == 'true'

- run: |
cd packages/e2e
yarn
yarn add ${{ github.event.input.library }}@${{ github.env.input.version }}
yarn run test:${{ github.event.input.library }}
24 changes: 24 additions & 0 deletions .github/workflows/third-party-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Third party libraries testing workflow
on:
workflow_dispatch:
inputs:
library:
type: choice
options:
- viem
- web3
- ethers
version:
type: string
default: 'latest'
description: Version to test again or 'latest'
workflow_run:

jobs:
third-party-test-workflow:
runs-on: ['self-hosted', 'org', '8-cpu']
steps:
- uses: celo-org/developer-tooling/.github/actions/third-party-tests/action.yaml
with:
library: ${{ github.event.inputs.library }}
version: ${{ github.event.inputs.version }}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"reset-rn": "watchman watch-del-all; rm -rf $TMPDIR/metro-cache-*; rm -rf $TMPDIR/haste-map-*; rm -rf $TMPDIR/metro-symbolicate*",
"reset-yarn": "yarn cache clean",
"test": "yarn test-pure && yarn test-needing-ganache",
"test-pure": "yarn workspaces foreach -piv --all --exclude celo --exclude \"@celo/{celocli,contractkit,transactions-uri}\" run test",
"test-pure": "yarn workspaces foreach -piv --all --exclude celo --exclude \"@celo/{celocli,contractkit,transactions-uri,e2e}\" run test",
"test-needing-ganache": "yarn workspace @celo/contractkit test && yarn workspace @celo/celocli test && yarn workspace @celo/transactions-uri test",
"build": "yarn workspaces foreach -piv --all --topological-dev run build",
"clean": "yarn workspaces foreach -piv --all run clean",
Expand Down
1 change: 1 addition & 0 deletions packages/e2e/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEST_ACCOUNT="0x123..."
3 changes: 3 additions & 0 deletions packages/e2e/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: '../../.eslintrc.js',
}
4 changes: 4 additions & 0 deletions packages/e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lib/
tmp/
.tmp/
.env
16 changes: 16 additions & 0 deletions packages/e2e/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/.devchain/
/.devchain.tar.gz
/coverage/
/node_modules/
/src/
/tmp/
/.tmp/

/tsconfig.*
/jest.config.*
*.tgz

/src

/lib/**/*.test.*
/lib/test-utils
Empty file added packages/e2e/CHANGELOG.md
Empty file.
3 changes: 3 additions & 0 deletions packages/e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @celo/e2e

TODO
4 changes: 4 additions & 0 deletions packages/e2e/eslint.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": []
}
35 changes: 35 additions & 0 deletions packages/e2e/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@celo/e2e",
"version": "0.0.1",
"description": "TODO",
"types": "./lib/index.d.ts",
"author": "cLabs",
"license": "Apache-2.0",
"homepage": "https://docs.celo.org/developer/tools",
"repository": "https://github.com/celo-org/developer-tooling/tree/master/packages/e2e",
"keywords": [],
"type": "module",
"exports": {
".": "./lib/index.js"
},
"scripts": {
"test": "yarn run vitest",
"test:viem": "yarn run vitest viem",
"test:ethers": "yarn run vitest ethers",
"test:web3": "yarn run vitest web3"
},
"dependencies": {
"@celo/abis": "^11.0.0",
"@celo/typescript": "workspace:^",
"@celo/web3-plugin-transaction-types": "^1.0.2",
"@vitest/coverage-v8": "2.1.2",
"dotenv": "^8.2.0",
"ethers": "^6.13.4",
"viem": "^2.21.27",
"vitest": "^2.1.2",
"web3": "^4.13.0"
},
"engines": {
"node": ">=18"
}
}
4 changes: 4 additions & 0 deletions packages/e2e/tests/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Hex } from 'viem'

export const TEST_PRIVATE_KEY = process.env.TEST_ACCOUNT as Hex
export { celo, celoAlfajores } from 'viem/chains'
56 changes: 56 additions & 0 deletions packages/e2e/tests/ethers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Contract, ethers, Wallet } from 'ethers'

import { describe, expect, it } from 'vitest'

import { registryABI } from '@celo/abis'
import { celoAlfajores, TEST_PRIVATE_KEY } from './common'

const provider = new ethers.JsonRpcProvider(celoAlfajores.rpcUrls.default.http[0])
const signer = new Wallet(TEST_PRIVATE_KEY, provider)

const cEURContract = new Contract(
'0x000000000000000000000000000000000000ce10',
registryABI,
provider
)
let cEUR = cEURContract.getAddressForString('StableToken')

describe('e2e ethers test suite', () => {
it('is setup correctly', async () => {
const blockNumber = await provider.getBlockNumber()
expect(blockNumber).toBeTypeOf('number')
expect(blockNumber).toBeGreaterThan(0)
})

it('can get basic account info', async () => {
const address = await signer.getAddress()
expect(address).toMatchInlineSnapshot(`"0x5FB3913Eb60d125Afbe7A6572E4F0F624dd945Ed"`)

const balance = await signer.provider?.getBalance(address, 'latest')
expect(balance).toBeTypeOf('bigint')
expect(balance).toBeGreaterThan(0n)
})

it('can call a contract', async () => {
expect(cEUR).resolves.toMatch(/^0x[A-Fa-f0-9]{40}$/)
})

it('can submit a basic tx', async () => {
const tx = await signer.sendTransaction({
value: 1n,
to: await signer.getAddress(),
})
await tx.wait()
expect(tx.hash).toMatch(/^0x[A-Fa-f0-9]{64}$/)
}, 15000)

it.skip('can submit a feeCurrency tx', async () => {
const tx = await signer.sendTransaction({
value: 1n,
to: await signer.getAddress(),
// feeCurrency: await cEUR,
})
await tx.wait()
expect(tx.hash).toMatch(/^0x[A-Fa-f0-9]{64}$/)
})
})
68 changes: 68 additions & 0 deletions packages/e2e/tests/viem.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { registryABI } from '@celo/abis'

import { createPublicClient, createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { createNonceManager, jsonRpc } from 'viem/nonce'

import { describe, expect, it } from 'vitest'

import { celoAlfajores, TEST_PRIVATE_KEY } from './common'

const publicClient = createPublicClient({
chain: celoAlfajores,
transport: http(),
})

const walletClient = createWalletClient({
account: privateKeyToAccount(TEST_PRIVATE_KEY, {
nonceManager: createNonceManager({
source: jsonRpc(),
}),
}),
transport: http(),
chain: celoAlfajores,
})

let cEUR = publicClient.readContract({
abi: registryABI,
functionName: 'getAddressForString',
address: '0x000000000000000000000000000000000000ce10',
args: ['StableTokenEUR'],
})

describe('viem e2e test suite', () => {
it('is setup correctly', async () => {
const blockNumber = await publicClient.getBlockNumber()
expect(blockNumber).toBeTypeOf('bigint')
expect(blockNumber).toBeGreaterThan(0)
})

it('can get basic account info', async () => {
const [address] = await walletClient.getAddresses()
expect(address).toMatchInlineSnapshot(`"0x5FB3913Eb60d125Afbe7A6572E4F0F624dd945Ed"`)

const balance = await publicClient.getBalance({ address, blockTag: 'latest' })
expect(balance).toBeTypeOf('bigint')
expect(balance).toBeGreaterThan(0)
})

it('can call a contract', async () => {
expect(cEUR).resolves.toMatch(/^0x[A-Fa-f0-9]{40}$/)
})

it('can submit a basic tx', async () => {
const tx = await walletClient.sendTransaction({
value: 1n,
to: await walletClient.getAddresses()[0],
})
expect(tx).toMatch(/^0x[A-Fa-f0-9]{64}$/)
})
it('can submit a feeCurrency tx', async () => {
const tx = await walletClient.sendTransaction({
value: 1n,
to: await walletClient.getAddresses()[0],
feeCurrency: await cEUR,
})
expect(tx).toMatch(/^0x[A-Fa-f0-9]{64}$/)
})
})
72 changes: 72 additions & 0 deletions packages/e2e/tests/web3.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { registryABI } from '@celo/abis'
// import { CeloTransactionTypesPlugin } from '@celo/web3-plugin-transaction-types'

import { describe, expect, it } from 'vitest'

import Web3 from 'web3'

import { TEST_PRIVATE_KEY, celoAlfajores } from './common'

const web3 = new Web3(celoAlfajores.rpcUrls.default[0])
const account = web3.eth.accounts.wallet.add(TEST_PRIVATE_KEY)
web3.setProvider(new Web3.providers.HttpProvider(celoAlfajores.rpcUrls.default.http[0]))

const cEURContract = new web3.eth.Contract(
registryABI as any,
'0x000000000000000000000000000000000000ce10'
)
let cEUR = cEURContract.methods.getAddressForString('StableToken').call()

describe('e2e web3 test suite', () => {
it('is setup correctly', async () => {
const blockNumber = await web3.eth.getBlockNumber()
expect(blockNumber).toBeTypeOf('number')
expect(blockNumber).toBeGreaterThan(0)
})

it('can get basic account info', async () => {
const { address } = account
expect(address).toMatchInlineSnapshot(`"0x5FB3913Eb60d125Afbe7A6572E4F0F624dd945Ed"`)

const balance = await web3.eth.getBalance(address, 'latest')
expect(balance).toBeTypeOf('string')
expect(balance).toMatch(/\d+/)
expect(BigInt(balance)).toBeGreaterThan(0n)
})

it('can call a contract', async () => {
expect(cEUR).resolves.toMatch(/^0x[A-Fa-f0-9]{40}$/)
})

it('can submit a basic tx', async () => {
const params = {
value: 1,
from: account.address,
to: account.address,
} as const

const tx = await web3.eth.sendTransaction({
...params,
gas: await web3.eth.estimateGas(params),
})

expect(tx.transactionHash).toMatch(/^0x[A-Fa-f0-9]{64}$/)
}, 15000)

it.skip('can submit a feeCurrency tx', async () => {
// web3.registerPlugin(new CeloTransactionTypesPlugin())
// web3.celo.link(web3)
// web3.eth.defaultAccount = account.address
// const params = {
// value: 1,
// from: account.address,
// to: account.address,
// feeCurrency: await cEUR,
// } as const
// await web3.celo.populateTransaction(params)
// const tx = await web3.eth.sendTransaction({
// ...params,
// })
// expect(tx.transactionHash).toMatch(/^0x[A-Fa-f0-9]{64}$/)
})
})
13 changes: 13 additions & 0 deletions packages/e2e/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "@celo/typescript/tsconfig.library.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
"moduleResolution": "Node16",
"module": "Node16",
"declaration": true,
"types": ["vitest/globals"]
},
"include": ["src/**/*", "types/**/*"],
"exclude": ["**/*.test.ts"]
}
Loading

0 comments on commit 8d0290e

Please sign in to comment.