mirror of
https://github.com/wharfkit/token.git
synced 2026-07-21 17:03:28 +00:00
chore: getting unit tests running properly
This commit is contained in:
+20
-16
@@ -1,19 +1,23 @@
|
||||
name: Tests
|
||||
on: push
|
||||
jobs:
|
||||
test:
|
||||
name: Node 14
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: '14'
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
- name: Install dependencies
|
||||
run: make node_modules
|
||||
- name: Run tests
|
||||
run: make ci-test
|
||||
- name: Run linter
|
||||
run: make ci-lint
|
||||
test-node-js:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
node-version: [14, 16, 18]
|
||||
name: Node.js v${{ matrix.node-version }}
|
||||
steps:
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v2
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
- name: Install dependencies
|
||||
run: make node_modules
|
||||
- name: Run checks
|
||||
run: make check
|
||||
- name: Run tests
|
||||
run: make ci-test
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
SHELL := /bin/bash
|
||||
SRC_FILES := $(shell find src -name '*.ts')
|
||||
TEST_FILES := $(wildcard test/tests/*.ts)
|
||||
BIN := ./node_modules/.bin
|
||||
MOCHA_OPTS := -u tdd -r ts-node/register -r tsconfig-paths/register --extension ts
|
||||
|
||||
lib: ${SRC_FILES} package.json tsconfig.json node_modules rollup.config.js
|
||||
@${BIN}/rollup -c && touch lib
|
||||
|
||||
.PHONY: test
|
||||
test: node_modules
|
||||
@TS_NODE_PROJECT='./test/tsconfig.json' ${BIN}/mocha \
|
||||
-u tdd -r ts-node/register --extension ts test/*.ts --grep '$(grep)'
|
||||
@TS_NODE_PROJECT='./test/tsconfig.json' MOCK_DIR='./test/data' \
|
||||
${BIN}/mocha ${MOCHA_OPTS} test/tests/*.ts --grep '$(grep)'
|
||||
|
||||
.PHONY: coverage
|
||||
coverage: node_modules
|
||||
@@ -15,14 +18,19 @@ coverage: node_modules
|
||||
${BIN}/mocha -u tdd -r ts-node/register --extension ts test/*.ts \
|
||||
-R nyan && open coverage/index.html
|
||||
|
||||
.PHONY: lint
|
||||
lint: node_modules
|
||||
@${BIN}/eslint src --ext .ts --fix
|
||||
.PHONY: check
|
||||
check: node_modules
|
||||
@${BIN}/eslint src --ext .ts --max-warnings 0 --format unix && echo "Ok"
|
||||
|
||||
.PHONY: format
|
||||
format: node_modules
|
||||
@${BIN}/eslint src test --ext .ts --fix
|
||||
|
||||
.PHONY: ci-test
|
||||
ci-test: node_modules
|
||||
@TS_NODE_PROJECT='./test/tsconfig.json' ${BIN}/nyc --reporter=text \
|
||||
${BIN}/mocha -u tdd -r ts-node/register --extension ts test/*.ts -R list
|
||||
@TS_NODE_PROJECT='./test/tsconfig.json' MOCK_DIR='./test/data' \
|
||||
${BIN}/nyc ${NYC_OPTS} --reporter=text \
|
||||
${BIN}/mocha ${MOCHA_OPTS} -R list test/tests/*.ts
|
||||
|
||||
.PHONY: ci-lint
|
||||
ci-lint: node_modules
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@wharfkit/antelope": "^0.7.3",
|
||||
"@wharfkit/mock-data": "^1.0.2",
|
||||
"bn.js": "^4.11.9",
|
||||
"tslib": "^2.1.0"
|
||||
},
|
||||
@@ -42,6 +43,7 @@
|
||||
"rollup": "^2.38.2",
|
||||
"rollup-plugin-dts": "^2.0.0",
|
||||
"ts-node": "^9.1.0",
|
||||
"tsconfig-paths": "^4.2.0",
|
||||
"typedoc": "^0.20.25",
|
||||
"typescript": "^4.1.2"
|
||||
}
|
||||
|
||||
+14
-10
@@ -1,4 +1,4 @@
|
||||
import {APIClient, Asset, NameType} from '@wharfkit/antelope'
|
||||
import {APIClient, Asset, Name, NameType} from '@wharfkit/antelope'
|
||||
|
||||
interface BalanceOptions {
|
||||
accountName: NameType
|
||||
@@ -8,25 +8,29 @@ interface BalanceOptions {
|
||||
}
|
||||
|
||||
export class Balance {
|
||||
readonly accountName: NameType
|
||||
readonly contract: NameType
|
||||
readonly symbol: Asset.SymbolType = '4,EOS'
|
||||
readonly accountName: Name
|
||||
readonly contract: Name
|
||||
readonly symbol: Asset.Symbol
|
||||
readonly apiClient: APIClient
|
||||
|
||||
constructor({accountName, contract, symbol, apiClient}: BalanceOptions) {
|
||||
this.accountName = accountName
|
||||
this.contract = contract
|
||||
this.symbol = symbol || this.symbol
|
||||
this.accountName = Name.from(accountName)
|
||||
this.contract = Name.from(contract)
|
||||
this.symbol = Asset.Symbol.from(symbol || '4,EOS')
|
||||
this.apiClient = apiClient
|
||||
}
|
||||
|
||||
get symbolCode(): Asset.SymbolCode {
|
||||
return this.symbol.code
|
||||
}
|
||||
|
||||
fetch(): Promise<Asset> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.apiClient.v1.chain
|
||||
.get_currency_balance(
|
||||
this.contract,
|
||||
String(this.contract),
|
||||
String(this.accountName),
|
||||
this.symbol && String(this.symbol)
|
||||
String(this.symbolCode)
|
||||
)
|
||||
.then((balances) => {
|
||||
const balance = (balances as any)[0]
|
||||
@@ -34,7 +38,7 @@ export class Balance {
|
||||
if (!balance) {
|
||||
reject(
|
||||
new Error(
|
||||
`No balance found for ${this.symbol} token of ${this.contract} contract.`
|
||||
`No "${this.symbol.code}" balance found for "${this.accountName}" token on "${this.contract}" contract.`
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"request": {
|
||||
"path": "https://jungle4.greymass.com/v1/chain/get_currency_balance",
|
||||
"params": {
|
||||
"method": "POST",
|
||||
"body": "{\"account\":\"teamgreymass\",\"code\":\"eosio.token\",\"symbol\":\"EOS\"}"
|
||||
}
|
||||
},
|
||||
"status": 200,
|
||||
"json": [
|
||||
"14.7187 EOS"
|
||||
],
|
||||
"text": "[\"14.7187 EOS\"]"
|
||||
}
|
||||
@@ -1,18 +1,20 @@
|
||||
import 'mocha'
|
||||
import {strict as assert} from 'assert'
|
||||
|
||||
import {APIClient, Asset} from '@wharfkit/antelope'
|
||||
import {Asset} from '@wharfkit/antelope'
|
||||
import {makeClient} from '@wharfkit/mock-data'
|
||||
|
||||
import {Balance} from '../src'
|
||||
const apiClient = makeClient('https://jungle4.greymass.com')
|
||||
|
||||
import {Balance} from '../../src'
|
||||
|
||||
suite('Balance', function () {
|
||||
this.slow(20000)
|
||||
test('fetch() returns an Asset', async function () {
|
||||
// pass fetch and url to resources directly
|
||||
const balance = new Balance({
|
||||
accountName: 'eosio',
|
||||
accountName: 'teamgreymass',
|
||||
contract: 'eosio.token',
|
||||
apiClient: new APIClient({url: 'https://eos.greymass.com'}),
|
||||
apiClient,
|
||||
})
|
||||
// perform test
|
||||
const result = await balance.fetch()
|
||||
@@ -1,51 +0,0 @@
|
||||
import fetch from 'node-fetch'
|
||||
import {join as joinPath} from 'path'
|
||||
import {promisify} from 'util'
|
||||
import {readFile as _readFile, writeFile as _writeFile} from 'fs'
|
||||
|
||||
const readFile = promisify(_readFile)
|
||||
const writeFile = promisify(_writeFile)
|
||||
|
||||
import {APIProvider, Bytes, Checksum160, FetchProvider} from '@wharfkit/antelope'
|
||||
|
||||
export class MockProvider implements APIProvider {
|
||||
recordProvider = new FetchProvider(this.api, {fetch})
|
||||
|
||||
constructor(private dir: string, private api: string = 'https://jungle3.greymass.com') {}
|
||||
|
||||
getFilename(path: string, params?: unknown) {
|
||||
const digest = Checksum160.hash(
|
||||
Bytes.from(this.api + path + (params ? JSON.stringify(params) : ''), 'utf8')
|
||||
).hexString
|
||||
return joinPath(this.dir, digest + '.json')
|
||||
}
|
||||
|
||||
async getExisting(filename: string) {
|
||||
try {
|
||||
const data = await readFile(filename)
|
||||
return JSON.parse(data.toString('utf8'))
|
||||
} catch (error) {
|
||||
if (error.code !== 'ENOENT') {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async call(path: string, params?: unknown) {
|
||||
const filename = this.getFilename(path, params)
|
||||
if (process.env['MOCK_RECORD'] !== 'overwrite') {
|
||||
const existing = await this.getExisting(filename)
|
||||
if (existing) {
|
||||
return existing
|
||||
}
|
||||
}
|
||||
if (process.env['MOCK_RECORD']) {
|
||||
const response = await this.recordProvider.call(path, params)
|
||||
const json = JSON.stringify(response, undefined, 4)
|
||||
await writeFile(filename, json)
|
||||
return response
|
||||
} else {
|
||||
throw new Error(`No data for ${path}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -348,6 +348,16 @@
|
||||
resolved "https://registry.yarnpkg.com/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz#aa58042711d6e3275dd37dc597e5d31e8c290a44"
|
||||
integrity sha512-sL/cEvJWAnClXw0wHk85/2L0G6Sj8UB0Ctc1TEMbKSsmpRosqhwj9gWgFRZSrBr2f9tiXISwNhCPmlfqUqyb9Q==
|
||||
|
||||
"@wharfkit/abicache@^1.1.1":
|
||||
version "1.1.1"
|
||||
resolved "https://registry.yarnpkg.com/@wharfkit/abicache/-/abicache-1.1.1.tgz#03624d03d9466e7def278786aaf074d274fc5650"
|
||||
integrity sha512-CfpHzCLxFATcfReqdJDUxEDSHIeBF3jFx0cP8RcTDyhC2jX4cd+Q/wqjw/kCni3xrBM7cIGUp9c9NZdvdTK9Cg==
|
||||
dependencies:
|
||||
"@wharfkit/antelope" "^0.7.3"
|
||||
"@wharfkit/signing-request" "^3.0.0"
|
||||
pako "^2.0.4"
|
||||
tslib "^2.1.0"
|
||||
|
||||
"@wharfkit/antelope@^0.7.3":
|
||||
version "0.7.3"
|
||||
resolved "https://registry.yarnpkg.com/@wharfkit/antelope/-/antelope-0.7.3.tgz#408f6c587f4f5990d4b55596c10be2e976798641"
|
||||
@@ -359,6 +369,51 @@
|
||||
hash.js "^1.0.0"
|
||||
tslib "^2.0.3"
|
||||
|
||||
"@wharfkit/common@^1.1.0":
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@wharfkit/common/-/common-1.1.0.tgz#1ee9dd1ba9e202002fadd20593f5f42a3e67c827"
|
||||
integrity sha512-A1Ta8zrEXkuEQcEiEexG0BVrYOxqm75qbLYU9tGNhyw4z/vQiF6rzmCOqhmWGg6nE2J2GYPvrPZPZzDmRGtG+w==
|
||||
dependencies:
|
||||
tslib "^2.1.0"
|
||||
|
||||
"@wharfkit/mock-data@^1.0.2":
|
||||
version "1.0.2"
|
||||
resolved "https://registry.yarnpkg.com/@wharfkit/mock-data/-/mock-data-1.0.2.tgz#81d6327c76032b40e5acf209d507cf6ca2a3ae9f"
|
||||
integrity sha512-Mbf/rZX2dqj5r+h+6NcRsDfRdHZ5OWEk0oIZ6iarXEBV65jmODoLdZlS906m9ndC1bi1ewCm/276JDimIqtLkQ==
|
||||
dependencies:
|
||||
"@wharfkit/antelope" "^0.7.3"
|
||||
"@wharfkit/session" "^1.0.0"
|
||||
"@wharfkit/wallet-plugin-privatekey" "^1.0.0"
|
||||
node-fetch "^2.6.1"
|
||||
tslib "^2.1.0"
|
||||
|
||||
"@wharfkit/session@^1.0.0":
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/@wharfkit/session/-/session-1.0.0.tgz#09c60d01a6185ab2e451d34462d84de2d7013220"
|
||||
integrity sha512-wXmvOVBZ1Rp/9HPUzGPaD/vpGXv2FCNgl8JRLopKgKPHkkEX/u4untshHR8AwSc0ZitjOlv6ubR2h9/UW8h6ug==
|
||||
dependencies:
|
||||
"@wharfkit/abicache" "^1.1.1"
|
||||
"@wharfkit/antelope" "^0.7.3"
|
||||
"@wharfkit/common" "^1.1.0"
|
||||
"@wharfkit/signing-request" "^3.0.0"
|
||||
pako "^2.0.4"
|
||||
tslib "^2.1.0"
|
||||
|
||||
"@wharfkit/signing-request@^3.0.0":
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/@wharfkit/signing-request/-/signing-request-3.0.0.tgz#3464cdc76c0690ab241d805116101e3ed63ba846"
|
||||
integrity sha512-+9UaznhYlZSgdZGG/LF5GkH7P9dCVh+b32cR1PYHKG6KuOsPlLqfv1DuWkqsxQyi3kGT1fXG1i8sl39ItgwLzg==
|
||||
dependencies:
|
||||
"@wharfkit/antelope" "^0.7.3"
|
||||
tslib "^2.0.3"
|
||||
|
||||
"@wharfkit/wallet-plugin-privatekey@^1.0.0":
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/@wharfkit/wallet-plugin-privatekey/-/wallet-plugin-privatekey-1.0.0.tgz#2600cce1117ce9391c8078649e05ceaf93780f1d"
|
||||
integrity sha512-V+/7T/cwoHM8fDaM3MZ1DFKrX2+NddBkkWJ8BIFfmEZnGR1W8Qr77t+piOP0/6UM2etmuZh98XLwZS33vORQ0A==
|
||||
dependencies:
|
||||
tslib "^2.1.0"
|
||||
|
||||
acorn-jsx@^5.3.1:
|
||||
version "5.3.1"
|
||||
resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-5.3.1.tgz#fc8661e11b7ac1539c47dbfea2e72b3af34d267b"
|
||||
@@ -1628,6 +1683,11 @@ json5@^2.1.2:
|
||||
dependencies:
|
||||
minimist "^1.2.5"
|
||||
|
||||
json5@^2.2.2:
|
||||
version "2.2.3"
|
||||
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
|
||||
integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==
|
||||
|
||||
jsonfile@^4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
|
||||
@@ -1761,6 +1821,11 @@ minimist@^1.2.5:
|
||||
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
|
||||
integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==
|
||||
|
||||
minimist@^1.2.6:
|
||||
version "1.2.8"
|
||||
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
|
||||
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==
|
||||
|
||||
mocha@^8.2.1:
|
||||
version "8.3.0"
|
||||
resolved "https://registry.yarnpkg.com/mocha/-/mocha-8.3.0.tgz#a83a7432d382ae1ca29686062d7fdc2c36f63fe5"
|
||||
@@ -1986,6 +2051,11 @@ package-hash@^4.0.0:
|
||||
lodash.flattendeep "^4.4.0"
|
||||
release-zalgo "^1.0.0"
|
||||
|
||||
pako@^2.0.4:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/pako/-/pako-2.1.0.tgz#266cc37f98c7d883545d11335c00fbd4062c9a86"
|
||||
integrity sha512-w+eufiZ1WuJYgPXbV/PO3NCMEc3xqylkKHzp8bxp1uW4qaSNQUkwmLLEc3kKsfz8lpV1F8Ht3U1Cm+9Srog2ug==
|
||||
|
||||
parent-module@^1.0.0:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/parent-module/-/parent-module-1.0.1.tgz#691d2709e78c79fae3a156622452d00762caaaa2"
|
||||
@@ -2386,6 +2456,11 @@ strip-ansi@^6.0.0:
|
||||
dependencies:
|
||||
ansi-regex "^5.0.0"
|
||||
|
||||
strip-bom@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3"
|
||||
integrity sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==
|
||||
|
||||
strip-bom@^4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-4.0.0.tgz#9c3505c1db45bcedca3d9cf7a16f5c5aa3901878"
|
||||
@@ -2484,6 +2559,15 @@ ts-node@^9.1.0:
|
||||
source-map-support "^0.5.17"
|
||||
yn "3.1.1"
|
||||
|
||||
tsconfig-paths@^4.2.0:
|
||||
version "4.2.0"
|
||||
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-4.2.0.tgz#ef78e19039133446d244beac0fd6a1632e2d107c"
|
||||
integrity sha512-NoZ4roiN7LnbKn9QqE1amc9DJfzvZXxF4xDavcOWt1BPkdx+m+0gJuPM+S0vCe7zTJMYUP0R8pO2XMr+Y8oLIg==
|
||||
dependencies:
|
||||
json5 "^2.2.2"
|
||||
minimist "^1.2.6"
|
||||
strip-bom "^3.0.0"
|
||||
|
||||
tslib@^1.8.1:
|
||||
version "1.14.1"
|
||||
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
|
||||
|
||||
Reference in New Issue
Block a user