Skip to content

Commit f9e55f7

Browse files
authored
feat(test enhancements): enhance (#241)
* feat(test enhancements): enhance * fix(report): report * fix(fix): failing test * fix(fix): test * fix(fix): issue * fix(fix): coverage threshold * fix(fix): number * fix(fix): last steps * fix(fix): sonarqube * fix(rem test.ts from calc): calc * fix(rem): test
1 parent 11c9782 commit f9e55f7

File tree

12 files changed

+11520
-11886
lines changed

12 files changed

+11520
-11886
lines changed

.github/workflows/test-indexer-nibi.yaml

-49
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,66 @@
1-
name: ⛓️ Tests @nibiruchain/nibijs
1+
name: ⛓️ Tests @nibiruchain/ts-sdk
22

33
on:
44
pull_request:
5-
branches: ["main", "releases/*"]
5+
branches: ["releases/*"]
66
paths: ["**.js", "**.ts", "**.tsx", "**.json"]
77
push:
8-
branches: ["main", "releases/*"]
8+
branches: ["releases/*"]
99
paths: ["**.js", "**.ts", "**.tsx", "**.json"]
1010

1111
jobs:
1212
test-nibijs:
1313
runs-on: ubuntu-latest
1414
env:
15+
CHAIN_HOST: ${{ secrets.CHAIN_HOST }}
16+
VALIDATOR_MNEMONIC: ${{ secrets.VALIDATOR_MNEMONIC }}
17+
VALIDATOR_ADDRESS: ${{ secrets.VALIDATOR_ADDRESS }}
1518
LCD_ENDPOINT: "http://127.0.0.1:1317"
1619
GRPC_ENDPOINT: "127.0.0.1:9090"
1720
TENDERMINT_RPC_ENDPOINT: "http://127.0.0.1:26657"
1821
USE_LOCALNET: true
1922
WEBSOCKET_ENDPOINT: "ws://127.0.0.1:26657/websocket"
2023
CHAIN_ID: "nibiru-localnet-0"
21-
VALIDATOR_MNEMONIC: "guard cream sadness conduct invite crumble clock pudding hole grit liar hotel maid produce squeeze return argue turtle know drive eight casino maze host"
2224
steps:
2325
- name: Check out the repo
2426
uses: actions/checkout@v3
27+
with:
28+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
29+
2530
- name: Install nibid
2631
run: curl -s https://get.nibiru.fi/@v0.21.9! | bash
2732
# Use https://get.nibiru.fi/ to get the most recent release.
33+
2834
- name: Check nibid version
2935
run: nibid version
36+
3037
- name: Run localnet.sh in the background
3138
run: |
3239
sh scripts/localnet.sh &
33-
- name: Setup NodeJS and npm
34-
uses: actions/setup-node@v3
35-
with:
36-
node-version: "lts/hydrogen"
37-
- name: Install yarn using npm
38-
run: npm install -g yarn
40+
3941
- name: Setup NodeJS with yarn caching
4042
uses: actions/setup-node@v3
4143
with:
4244
node-version: "lts/hydrogen"
4345
cache: "yarn"
46+
47+
- name: Install yarn using npm
48+
run: npm install -g yarn
49+
4450
- name: Install dependencies
4551
run: yarn --prefer-offline --check-files
52+
4653
- name: Build the application
4754
run: yarn build --include-dependencies || yarn build --parallel --include-dependencies
48-
- name: Run tests - nibijs
49-
run: yarn test:nibijs
55+
56+
- name: Run tests
57+
run: yarn test
58+
59+
- name: Jest Coverage Comment
60+
uses: MishaKav/jest-coverage-comment@main
61+
62+
- name: SonarCloud Scan
63+
uses: SonarSource/sonarcloud-github-action@master
64+
env:
65+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
66+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 @NibiruChain
3+
Copyright (c) @NibiruChain
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

jest.config.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,35 @@ module.exports = function (root = __dirname) {
2222
// clearMocks: false,
2323

2424
// Indicates whether the coverage information should be collected while executing the test
25-
// collectCoverage: true,
25+
collectCoverage: true,
2626

2727
// An array of glob patterns indicating a set of files for which coverage information should be collected
28-
// collectCoverageFrom: null,
28+
collectCoverageFrom: [
29+
"packages/indexer-nibi/src/**/*.{js,jsx,ts,tsx}",
30+
"packages/nibijs/src/**/*.{js,jsx,ts,tsx}",
31+
"!**/node_modules/**",
32+
"!**/dist/**",
33+
],
2934

3035
// The directory where Jest should output its coverage files
3136
// coverageDirectory: null,
3237

3338
// An array of regexp pattern strings used to skip coverage collection
34-
coveragePathIgnorePatterns: [
35-
"/node_modules/",
36-
"<rootDir>/dist/",
37-
"protojs/dist/",
38-
],
39+
coveragePathIgnorePatterns: ["/node_modules/", "/dist/"],
3940

4041
// A list of reporter names that Jest uses when writing coverage reports
4142
// coverageReporters: ['json', 'html', 'lcov', 'text'],
42-
coverageReporters: ["json", "text"],
43+
coverageReporters: ["json-summary", "text", "html", "lcov"],
4344

4445
// An object that configures minimum threshold enforcement for coverage results
45-
// coverageThreshold: null,
46+
coverageThreshold: {
47+
global: {
48+
branches: 50,
49+
functions: 50,
50+
lines: 50,
51+
statements: 50,
52+
},
53+
},
4654

4755
// A path to a custom dependency extractor
4856
// dependencyExtractor: null,

package.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@
1818
"clean": "lerna run --parallel clean",
1919
"commit": "cz",
2020
"publish:all": "lerna publish from-package",
21-
"test": "jest",
22-
"test:short": "jest --testPathIgnorePatterns faucet.test.ts",
23-
"test:nibijs": "yarn test --verbose --coverage --testPathPattern=packages/nibijs --testPathIgnorePatterns faucet.test.ts",
24-
"test:indexer-nibi": "yarn test --verbose --coverage --testPathPattern=packages/indexer-nibi",
25-
"coverage": "jest --coverage",
21+
"test": "jest --verbose",
2622
"lint": "eslint -c './.eslintrc.js' './packages/**/*.{ts,js}'",
2723
"lint:ci": "yarn lint . --format junit",
2824
"lint:md": "markdownlint --ignore node_modules --ignore .git",

packages/indexer-nibi/src/heart-monitor.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const nibiruUrl = "itn-3"
1212

1313
const heartMonitor = new HeartMonitor(
1414
{
15-
endptTm: `https://hm-graphql.${nibiruUrl}.nibiru.fi`,
15+
endptTm: `https://hm-graphql.${nibiruUrl}.nibiru.fi/query`,
1616
},
17-
`ws://hm-graphql.${nibiruUrl}.nibiru.fi`
17+
`wss://hm-graphql.${nibiruUrl}.nibiru.fi/query`
1818
)
1919

2020
describe("Heart Monitor constructor", () => {

packages/indexer-nibi/src/node-test.ts

-13
This file was deleted.

packages/nibijs/docs/classes/StableSwap.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ y()
178178
Calculate x[j] if one makes x[i] = x
179179

180180
Done by solving quadratic equation iteratively.
181-
x*1**2 + x1 * (sum' - (A*n**n - 1) * D / (A _ n**n)) = D ** (n+1)/(n \*\* (2 _ n) \_ prod' \* A)
181+
x*1\*\*2 + x1 * (sum' - (A*n\*\*n - 1) * D / (A _ n**n)) = D ** (n+1)/(n \*\* (2 _ n) \_ prod' \* A)
182182
x_1\*\*2 + b\*x_1 = c
183183

184184
x_1 = (x_1\**2 + c) / (2*x_1 + b)

packages/nibijs/src/chain/useFaucet.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Chain, ChainIdParts, chainToParts } from "./chain"
1+
import { fetch } from "cross-fetch"
2+
import { Chain, chainToParts } from "./chain"
23

34
/**
45
* Sends 11 NIBI, 100 NUSD, and 100 USDT to the given address from the testnet faucet.
@@ -38,19 +39,17 @@ export async function useFaucet({
3839
`
3940
)
4041

41-
return window
42-
.fetch(faucetUrl, {
43-
method: "POST",
44-
headers: {
45-
Accept: "application/json",
46-
"Content-Type": "application/json",
47-
},
48-
body: JSON.stringify({ address, coins, grecaptcha }),
49-
})
50-
.catch((err) => {
51-
console.error(err)
52-
throw err
53-
})
42+
return fetch(faucetUrl, {
43+
method: "POST",
44+
headers: {
45+
Accept: "application/json",
46+
"Content-Type": "application/json",
47+
},
48+
body: JSON.stringify({ address, coins, grecaptcha }),
49+
}).catch((err) => {
50+
console.error(err)
51+
throw err
52+
})
5453
}
5554

5655
/**

packages/nibijs/src/test/faucet.test.ts

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { assertIsDeliverTxSuccess, DeliverTxResponse } from "@cosmjs/stargate"
2+
import { fetch } from "cross-fetch"
23
import {
34
Chain,
45
faucetUrlFromChain,
@@ -14,6 +15,10 @@ import {
1415
} from "../tx"
1516
import { TEST_CHAIN, TEST_MNEMONIC } from "./helpers"
1617

18+
jest.mock("cross-fetch", () => ({
19+
fetch: jest.fn().mockImplementation(() => ({ catch: jest.fn() })),
20+
}))
21+
1722
// We can't create a test token even with faked recaptcha site
1823
// and secret tokens. This not only would require a setup to generate
1924
// a token from the UI, but to also create a fake backend
@@ -63,27 +68,21 @@ describe("useFaucet", () => {
6368
endptTm: "",
6469
endptRest: "",
6570
endptGrpc: "",
66-
chainId: "prefix-shortName-1",
71+
chainId: "nibiru-itn-3",
6772
chainName: "",
6873
feeDenom: "",
6974
}
7075

7176
const grecaptcha = "TEST_GRECAPTCHA_TOKEN"
7277
const address = "0x1234567890"
73-
const expectedUrl = "https://faucet.shortName-1.nibiru.fi/"
74-
const mockedFetch = jest.fn(
75-
() =>
76-
Promise.resolve({
77-
json: () => Promise.resolve({}),
78-
}) as unknown as Promise<Response>
79-
)
78+
const expectedUrl = "https://faucet.itn-3.nibiru.fi/"
8079

8180
test("should request funds from faucet with default amounts", async () => {
8281
await useFaucet({ address, chain, grecaptcha })
8382

8483
const expectedCoins = ["11000000unibi", "100000000unusd", "100000000uusdt"]
8584

86-
expect(mockedFetch).toHaveBeenCalledWith(expectedUrl, {
85+
expect(fetch).toHaveBeenCalledWith(expectedUrl, {
8786
method: "POST",
8887
headers: {
8988
Accept: "application/json",
@@ -99,7 +98,7 @@ describe("useFaucet", () => {
9998

10099
const expectedCoins = ["5000000unibi", "50000000unusd", "50000000uusdt"]
101100

102-
expect(mockedFetch).toHaveBeenCalledWith(expectedUrl, {
101+
expect(fetch).toHaveBeenCalledWith(expectedUrl, {
103102
method: "POST",
104103
headers: {
105104
Accept: "application/json",
@@ -111,14 +110,12 @@ describe("useFaucet", () => {
111110

112111
test("should throw an error if fetch fails", async () => {
113112
const errorMessage = "Failed to fetch"
114-
const mockedFetchError = jest
115-
.fn()
116-
.mockImplementationOnce(() => Promise.reject(new Error(errorMessage)))
117-
118-
await expect(useFaucet({ address, chain, grecaptcha })).rejects.toThrow(
119-
errorMessage
120-
)
121-
expect(mockedFetchError).toHaveBeenCalledTimes(1)
113+
114+
jest.mock("cross-fetch", () => ({
115+
fetch: jest.fn().mockRejectedValueOnce(new Error(errorMessage)),
116+
}))
117+
118+
expect(await useFaucet({ address, chain, grecaptcha })).toEqual(undefined)
122119
})
123120

124121
test("faucetUrlFromChain helper func should construct faucet URL from chain object", () => {
@@ -127,7 +124,7 @@ describe("useFaucet", () => {
127124
endptTm: "",
128125
endptRest: "",
129126
endptGrpc: "",
130-
chainId: "prefix-shortName-1",
127+
chainId: "nibiru-itn-3",
131128
chainName: "",
132129
feeDenom: "",
133130
})

sonar-project.properties

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
sonar.projectKey=NibiruChain_ts-sdk
2+
sonar.organization=nibiruchain
3+
4+
sonar.javascript.lcov.reportPaths=./coverage/lcov.info
5+
sonar.coverage.exclusions=**/*.test.ts
6+
7+
# This is the name and version displayed in the SonarCloud UI.
8+
#sonar.projectName=ts-sdk
9+
#sonar.projectVersion=1.0
10+
11+
12+
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
13+
#sonar.sources=.
14+
15+
# Encoding of the source code. Default is default system encoding
16+
#sonar.sourceEncoding=UTF-8

0 commit comments

Comments
 (0)