Skip to content

Commit

Permalink
Add tests for new network tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
eternauta1337 committed Feb 26, 2024
1 parent 7beaf44 commit caf1d22
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/ethernaut-network/src/tasks/active.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require('../scopes/net')
.setAction(async () => {
try {
const networks = storage.readNetworks()
output.resultBox(`Active network is ${networks.activeNetwork}`)
output.resultBox(`The active network is "${networks.activeNetwork}"`)
} catch (err) {
return output.errorBox(err)
}
Expand Down
25 changes: 25 additions & 0 deletions packages/ethernaut-network/test/tasks/active.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const assert = require('assert')
const { Terminal } = require('common/src/terminal')
const storage = require('../../src/internal/storage')

describe('active', function () {
let activeNetwork
const terminal = new Terminal()

before('get the active network', async function () {
activeNetwork = storage.readNetworks().activeNetwork
})

describe('when calling active task', function () {
before('call task', async function () {
await terminal.run('npx hardhat net active')
})

it('prints the active network', async function () {
assert.ok(
terminal.output.includes(`The active network is "${activeNetwork}"`),
terminal.output,
)
})
})
})
7 changes: 5 additions & 2 deletions packages/ethernaut-network/test/tasks/add.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ const storage = require('ethernaut-network/src/internal/storage')
describe('add', function () {
const terminal = new Terminal()

before('remove test networks', async function () {
const removeTestNetworks = function () {
const networks = storage.readNetworks()
if ('test__1' in networks) delete networks.test__1
if ('test__2' in networks) delete networks.test__2
storage.storeNetworks(networks)
})
}

before('remove test networks', removeTestNetworks)
after('remove test networks', removeTestNetworks)

describe('when all params are specified', function () {
before('run add', async function () {
Expand Down
7 changes: 7 additions & 0 deletions packages/ethernaut-network/test/tasks/remove.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ describe('remove', function () {
storage.storeNetworks(networks)
})

after('remove test networks', async function () {
const networks = storage.readNetworks()
if ('test__3' in networks) delete networks.test__3
if ('test__4' in networks) delete networks.test__4
storage.storeNetworks(networks)
})

describe('when all params are specified', function () {
before('run remove', async function () {
await terminal.run('npx hardhat net remove --alias test__3')
Expand Down
44 changes: 44 additions & 0 deletions packages/ethernaut-network/test/tasks/set.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const assert = require('assert')
const { Terminal } = require('common/src/terminal')
const storage = require('../../src/internal/storage')

describe('set', function () {
const terminal = new Terminal()
let activeNetwork

before('read the active network', async function () {
activeNetwork = storage.readNetworks().activeNetwork
})

after('restore the active network', async function () {
const networks = storage.readNetworks()
networks.activeNetwork = activeNetwork
storage.storeNetworks(networks)
})

before('inject a dummy network', async function () {
const networks = storage.readNetworks()
networks.test__1 = { url: 'http://localhost:8545' }
storage.storeNetworks(networks)
})

after('remove dummy network', async function () {
const networks = storage.readNetworks()
if ('test__1' in networks) delete networks.test__1
storage.storeNetworks(networks)
})

describe('when calling set', function () {
before('call it', async function () {
await terminal.run('npx hardhat net set --alias test__1', 1000)
})

it('sets the active network', async function () {
const networks = storage.readNetworks()
assert.equal(networks.activeNetwork, 'test__1')
})

// TODO: Test actually interacting with the set network
// (Only tested manually so far)
})
})

0 comments on commit caf1d22

Please sign in to comment.