From 982bd26adffb789e119f90f51722fca4f8b200a6 Mon Sep 17 00:00:00 2001 From: poi-son-ivy <146914988+poi-son-ivy@users.noreply.github.com> Date: Thu, 22 Feb 2024 12:46:15 -0800 Subject: [PATCH] CI/CD : Adding Snap Unit Test Github Action (#105) Signed-off-by: Ivy Astrix --- .github/workflows/run-jest-unit-snap.yml | 27 +++ .../packages/snap/package.json | 2 +- .../packages/snap/snap.manifest.json | 2 +- .../src/utils/__tests__/HederaUtils.spec.ts | 183 ------------------ 4 files changed, 29 insertions(+), 185 deletions(-) create mode 100644 .github/workflows/run-jest-unit-snap.yml delete mode 100644 packages/hedera-wallet-snap/packages/snap/src/utils/__tests__/HederaUtils.spec.ts diff --git a/.github/workflows/run-jest-unit-snap.yml b/.github/workflows/run-jest-unit-snap.yml new file mode 100644 index 00000000..d3191aa5 --- /dev/null +++ b/.github/workflows/run-jest-unit-snap.yml @@ -0,0 +1,27 @@ +name: Run Hedera Pulse Snap Unit Tests + +on: [push] + +jobs: + eslint: + name: Run Hedera Pulse Snap Unit Tests + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + name: Check out source code + + - uses: actions/setup-node@v2 + name: Set up Node.js + with: + node-version: '18' + cache: 'yarn' + cache-dependency-path: packages/hedera-wallet-snap/packages/snap + + - name: Install Dependencies in Subdirectory + run: yarn install + working-directory: ./packages/hedera-wallet-snap/packages/snap + + - name: Run Snap Unit Tests + run: yarn test + working-directory: ./packages/hedera-wallet-snap/packages/snap diff --git a/packages/hedera-wallet-snap/packages/snap/package.json b/packages/hedera-wallet-snap/packages/snap/package.json index bdcbd727..001b7b8b 100644 --- a/packages/hedera-wallet-snap/packages/snap/package.json +++ b/packages/hedera-wallet-snap/packages/snap/package.json @@ -44,7 +44,7 @@ "lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' --ignore-path .gitignore", "serve": "mm-snap serve", "start": "mm-snap --version && concurrently \"yarn build && yarn serve\" \"yarn watch\"", - "test": "jest --forceExit", + "test": "jest --forceExit --verbose", "test:ci": "yarn test --silent", "watch": "chokidar 'src/' -c 'yarn build'" }, diff --git a/packages/hedera-wallet-snap/packages/snap/snap.manifest.json b/packages/hedera-wallet-snap/packages/snap/snap.manifest.json index 3852321c..feec131c 100644 --- a/packages/hedera-wallet-snap/packages/snap/snap.manifest.json +++ b/packages/hedera-wallet-snap/packages/snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "git+https://github.com/hashgraph/hedera-metamask-snaps.git" }, "source": { - "shasum": "QMtY9QF0nXHx3vKJLK6NAxeLsBoZt2+JxKp6EcX3ZLI=", + "shasum": "5d6XzqGIuaMBr8L8cWZVgT6GMftl95AzrNvYs8kG4+k=", "location": { "npm": { "filePath": "dist/snap.js", diff --git a/packages/hedera-wallet-snap/packages/snap/src/utils/__tests__/HederaUtils.spec.ts b/packages/hedera-wallet-snap/packages/snap/src/utils/__tests__/HederaUtils.spec.ts deleted file mode 100644 index 898939ca..00000000 --- a/packages/hedera-wallet-snap/packages/snap/src/utils/__tests__/HederaUtils.spec.ts +++ /dev/null @@ -1,183 +0,0 @@ -import { providerErrors } from '@metamask/rpc-errors'; -import { HederaUtils } from '../HederaUtils'; - -describe('HederaUtils', () => { - describe('getMirrorNodeFlagIfExists', () => { - it('returns empty string if no mirrorNodeUrl provided', () => { - const params = {}; // Empty params - const result = HederaUtils.getMirrorNodeFlagIfExists(params); - expect(result).toBe(''); - }); - - it('returns normalized mirrorNodeUrl if provided', () => { - const params = { mirrorNodeUrl: 'http://example.com/' }; - const result = HederaUtils.getMirrorNodeFlagIfExists(params); - expect(result).toBe('http://example.com'); - }); - }); - - describe('isExternalAccountFlagSet', () => { - it('returns false if externalAccount flag is not set', () => { - const params = {}; // Empty params - const result = HederaUtils.isExternalAccountFlagSet(params); - expect(result).toBe(false); - }); - - it('returns true if externalAccount flag is set correctly', () => { - const params = { - externalAccount: { accountIdOrEvmAddress: '0.0.123', curve: 'ED25519' }, - }; - const result = HederaUtils.isExternalAccountFlagSet(params); - expect(result).toBe(true); - }); - }); - - describe('isValidServiceFee', () => { - it('throws an error if serviceFee.percentageCut is invalid', () => { - const params = { percentageCut: null, toAddress: '0.0.123' }; - expect(() => { - HederaUtils.isValidServiceFee(params); - }).toThrow(providerErrors.unsupportedMethod().message); - }); - - it('does not throw an error if serviceFee is valid', () => { - const params = { percentageCut: 10, toAddress: '0.0.123' }; - expect(() => { - HederaUtils.isValidServiceFee(params); - }).not.toThrow(); - }); - }); - - describe('isValidSignMessageRequest', () => { - it('throws an error if message parameter is missing', () => { - const params = {}; // Empty parameters - expect(() => HederaUtils.isValidSignMessageRequest(params)).toThrow( - providerErrors.unsupportedMethod().message, - ); - }); - - it('does not throw an error for valid message parameters', () => { - const params = { message: 'Test message' }; - expect(() => HederaUtils.isValidSignMessageRequest(params)).not.toThrow(); - }); - }); - describe('isValidGetAccountInfoRequest', () => { - it('throws an error if accountId is invalid', () => { - const params = { accountId: 'invalid' }; - expect(() => HederaUtils.isValidGetAccountInfoRequest(params)).toThrow( - providerErrors.unsupportedMethod().message, - ); - }); - - it('does not throw an error for valid accountId', () => { - const params = { accountId: '0.0.12345' }; - expect(() => - HederaUtils.isValidGetAccountInfoRequest(params), - ).not.toThrow(); - }); - }); - describe('isValidGetTransactionsParams', () => { - it('throws an error if transactionId is invalid', () => { - const params = { transactionId: '' }; // Invalid empty string - expect(() => HederaUtils.isValidGetTransactionsParams(params)).toThrow( - providerErrors.unsupportedMethod().message, - ); - }); - - it('does not throw an error for valid transactionId', () => { - const params = { transactionId: '0.0.12345@123456789' }; - expect(() => - HederaUtils.isValidGetTransactionsParams(params), - ).not.toThrow(); - }); - }); - describe('isValidAssociateTokensParams', () => { - it('throws an error if tokenIds array is empty', () => { - const params = { tokenIds: [] }; - expect(() => HederaUtils.isValidAssociateTokensParams(params)).toThrow( - providerErrors.unsupportedMethod().message, - ); - }); - - it('does not throw an error for valid tokenIds', () => { - const params = { tokenIds: ['0.0.123', '0.0.456'] }; - expect(() => - HederaUtils.isValidAssociateTokensParams(params), - ).not.toThrow(); - }); - }); - describe('isValidTransferCryptoParams', () => { - it('throws an error if transfers parameter is missing or empty', () => { - const emptyParams = {}; - expect(() => - HederaUtils.isValidTransferCryptoParams(emptyParams), - ).toThrow( - 'Invalid transferCrypto Params passed. "transfers" must be passed as a parameter', - ); - - const paramsWithEmptyArray = { transfers: [] }; - expect(() => - HederaUtils.isValidTransferCryptoParams(paramsWithEmptyArray), - ).toThrow( - 'Invalid transferCrypto Params passed. "transfers" must be passed as a parameter', - ); - }); - - it('does not throw an error for valid transfers parameters', () => { - const validParams = { - transfers: [{ to: '0.0.123', amount: 100, assetType: 'HBAR' }], - }; - expect(() => - HederaUtils.isValidTransferCryptoParams(validParams), - ).not.toThrow(); - }); - }); - describe('isValidStakeHbarParams', () => { - it('throws an error if neither nodeId nor accountId are provided', () => { - const invalidParams = {}; - expect(() => HederaUtils.isValidStakeHbarParams(invalidParams)).toThrow( - 'Invalid stakeHbar Params passed. Pass either "nodeId" or "accountId" as a parameter', - ); - }); - - it('does not throw an error when valid nodeId is provided', () => { - const paramsWithNodeId = { nodeId: 0 }; - expect(() => - HederaUtils.isValidStakeHbarParams(paramsWithNodeId), - ).not.toThrow(); - }); - - it('does not throw an error when valid accountId is provided', () => { - const paramsWithAccountId = { accountId: '0.0.12345' }; - expect(() => - HederaUtils.isValidStakeHbarParams(paramsWithAccountId), - ).not.toThrow(); - }); - - // Test for invalid nodeId and accountId formats - }); - describe('isValidDeleteAccountParams', () => { - it('throws an error if transferAccountId is missing or invalid', () => { - const invalidParams = {}; - expect(() => - HederaUtils.isValidDeleteAccountParams(invalidParams), - ).toThrow( - 'Invalid deleteAccount Params passed. "transferAccountId" must be passed as a parameter', - ); - - const invalidParams2 = { transferAccountId: '' }; - expect(() => - HederaUtils.isValidDeleteAccountParams(invalidParams2), - ).toThrow( - 'Invalid deleteAccount Params passed. "transferAccountId" is not a valid Account ID', - ); - }); - - it('does not throw an error for valid transferAccountId', () => { - const validParams = { transferAccountId: '0.0.12345' }; - expect(() => - HederaUtils.isValidDeleteAccountParams(validParams), - ).not.toThrow(); - }); - }); -});