diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 00000000..99c0abfa --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,40 @@ +name: Unit Test Coverage + +on: + push: + branches: + - feature/test-chain-util + pull_request: + branches: + - feature/test-chain-util + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: '18' + + - name: Install dependencies + run: yarn install + + - name: Run tests with coverage + run: yarn run test:coverage + + - name: Generate coverage badge + uses: jaywcjlove/coverage-badges-cli@main + with: + source: coverage/coverage-summary.json + output: coverage/badges.svg + + - name: Deploy to GitHub Pages + uses: peaceiris/actions-gh-pages@v3 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: ./coverage diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 00000000..2355e34f --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,33 @@ +name: Publish +env: + CI: true +on: + push: + branches: + - feature/test-chain-util + tags: + - '!*' +jobs: + release: + name: Setup + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: '20' + registry-url: 'https://npm.pkg.github.com' + + - name: Install dependencies + run: yarn install + + - name: Publish to npm + run: | + npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN + npm publish || (echo "pnpm publish failed" && exit 1) + env: + NPM_TOKEN: ${{secrets.NPM_TOKEN}} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/test-badge.yml b/.github/workflows/test-badge.yml new file mode 100644 index 00000000..412c07e3 --- /dev/null +++ b/.github/workflows/test-badge.yml @@ -0,0 +1,108 @@ +name: Test Badge + +permissions: + contents: write + +on: + push: + branches: + - dev + - master + - feature/test-chain-util + +env: + BRANCH_NAME: 'feature/badge-json' + +jobs: + test: + name: Generate Test Badge + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 20 + cache: yarn + - run: yarn install + - run: yarn run test:browser + - name: Install xmlstarlet + run: | + sudo apt-get update + sudo apt-get install -y xmlstarlet + + - name: Extract test counts + run: | + echo "TESTS=$(xmlstarlet sel -t -v "testsuites/@tests" "jest-report.xml")" >> $GITHUB_ENV + echo "FAILURES=$(xmlstarlet sel -t -v "testsuites/@failures" "jest-report.xml")" >> $GITHUB_ENV + echo "ERRORS=$(xmlstarlet sel -t -v "testsuites/@errors" "jest-report.xml")" >> $GITHUB_ENV + + - name: Set file name + run: echo "FILENAME=$(echo ${GITHUB_REF#refs/heads/} | tr '/' '-')-test-results.json" >> $GITHUB_ENV + + - name: Prepare Content + uses: actions/github-script@v5 + with: + github-token: ${{ secrets.COMMIT_TOKEN }} + script: | + const fs = require('fs'); + const tests = "${{ env.TESTS }}"; + const failures = "${{ env.FAILURES }}"; + const errors = "${{ env.ERRORS }}"; + const success = tests - failures; + const color = errors > 0 ? "red" : (failures > 0 ? "green" : "brightgreen"); + const content = `{"schemaVersion":1,"label":"tests","message":"${tests} tests, ${success} success","color":"${color}"}`; + + fs.writeFileSync("${{ env.FILENAME }}", content); + + - name: Check if file exists + uses: actions/github-script@v5 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + let fileExists = false; + try { + const { data } = await github.rest.repos.getContent({ + owner: context.repo.owner, + repo: context.repo.repo, + path: "${{ env.FILENAME }}", + ref: "${{ env.BRANCH_NAME }}", + }); + fileExists = !!data; + } catch (error) { + if (error.status !== 404) { + throw error; + } + } + core.exportVariable('FILE_EXISTS', fileExists); + + - name: Create or update file + uses: actions/github-script@v5 + with: + github-token: ${{ secrets.COMMIT_TOKEN }} + script: | + const fs = require('fs'); + const path = require('path'); + const filePath = path.join(process.env.GITHUB_WORKSPACE, "${{ env.FILENAME }}"); + const fileContent = fs.readFileSync(filePath, 'utf8'); + + const params = { + owner: context.repo.owner, + repo: context.repo.repo, + path: "${{ env.FILENAME }}", + message: `Update ${{ env.FILENAME }}`, + content: Buffer.from(fileContent).toString('base64'), + branch: "${{ env.BRANCH_NAME }}" + }; + + if (${{ env.FILE_EXISTS }}) { + const { data } = await github.rest.repos.getContent({ + owner: context.repo.owner, + repo: context.repo.repo, + path: "${{ env.FILENAME }}", + ref: "${{ env.BRANCH_NAME }}" + }); + params.sha = data.sha; + } + + await github.rest.repos.createOrUpdateFileContents(params); diff --git a/README.md b/README.md index e1c1a545..8b3cafff 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,23 @@ # aelf-sdk.js - AELF JavaScript API -| Statements | Branches | Functions | Lines | +
+ + | Branch | Tests | Coverage | +|--------------|-----------------|----------------| +| `feature/test-chain-util` | ![Tests](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/AElfProject/aelf-web3.js/feature/badge-json/feature-test-chain-util-test-results.json) | ![Coverage](https://AElfProject.github.io/aelf-web3.js/badges.svg) | + + ## 1. Introduction diff --git a/jest.browser.config.js b/jest.browser.config.js index 6797efa9..15775a5e 100644 --- a/jest.browser.config.js +++ b/jest.browser.config.js @@ -123,7 +123,16 @@ module.exports = { // projects: null, // Use this configuration option to add custom reporters to Jest - // reporters: [], + reporters: [ + 'default', + [ + 'jest-junit', + { + outputDirectory: '.', + outputName: 'jest-report.xml' + } + ] + ], // Automatically reset mock state between every test // resetMocks: false, diff --git a/package.json b/package.json index 0ef8b07f..90d69bb0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aelf-sdk", - "version": "3.4.13-beta.1", + "version": "3.4.13-beta.2", "description": "aelf-sdk js library", "main": "dist/aelf.cjs.js", "browser": "dist/aelf.umd.js", @@ -122,6 +122,7 @@ "jest-environment-jsdom": "^29.5.0", "jest-environment-jsdom-fifteen": "^1.0.2", "jest-github-reporter": "^1.1.1", + "jest-junit": "^16.0.0", "lint-staged": "^13.2.1", "rimraf": "^5.0.0", "size-limit": "^8.1.2", diff --git a/yarn.lock b/yarn.lock index a41ed19f..4fd93f51 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6293,6 +6293,16 @@ jest-haste-map@^29.7.0: optionalDependencies: fsevents "^2.3.2" +jest-junit@^16.0.0: + version "16.0.0" + resolved "https://registry.yarnpkg.com/jest-junit/-/jest-junit-16.0.0.tgz#d838e8c561cf9fdd7eb54f63020777eee4136785" + integrity sha512-A94mmw6NfJab4Fg/BlvVOUXzXgF0XIH6EmTgJ5NDPp4xoKq0Kr7sErb+4Xs9nZvu58pJojz5RFGpqnZYJTrRfQ== + dependencies: + mkdirp "^1.0.4" + strip-ansi "^6.0.1" + uuid "^8.3.2" + xml "^1.0.1" + jest-leak-detector@^29.7.0: version "29.7.0" resolved "https://registry.npmjs.org/jest-leak-detector/-/jest-leak-detector-29.7.0.tgz#5b7ec0dadfdfec0ca383dc9aa016d36b5ea4c728" @@ -7340,6 +7350,11 @@ mkdirp@^0.5.1: dependencies: minimist "^1.2.6" +mkdirp@^1.0.4: + version "1.0.4" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e" + integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== + modify-values@^1.0.0: version "1.0.1" resolved "https://registry.npmjs.org/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022" @@ -9684,6 +9699,11 @@ uuid@^3.3.2: resolved "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee" integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A== +uuid@^8.3.2: + version "8.3.2" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" + integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== + v8-to-istanbul@^9.0.1: version "9.3.0" resolved "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-9.3.0.tgz#b9572abfa62bd556c16d75fdebc1a411d5ff3175" @@ -10057,6 +10077,11 @@ xml-name-validator@^4.0.0: resolved "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-4.0.0.tgz#79a006e2e63149a8600f15430f0a4725d1524835" integrity sha512-ICP2e+jsHvAj2E2lIHxa5tjXRlKDJo4IdvPvCXbXQGdzSfmSpNVyIKMvoZHjDY9DP0zV17iI85o90vRFXNccRw== +xml@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/xml/-/xml-1.0.1.tgz#78ba72020029c5bc87b8a81a3cfcd74b4a2fc1e5" + integrity sha512-huCv9IH9Tcf95zuYCsQraZtWnJvBtLVE0QHMOs8bWyZAFZNDcYjsPq1nEx8jKA9y+Beo9v+7OBPRisQTjinQMw== + xmlchars@^2.1.1, xmlchars@^2.2.0: version "2.2.0" resolved "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz#060fe1bcb7f9c76fe2a17db86a9bc3ab894210cb"