Skip to content

Commit

Permalink
chore: update the unit test configurations (uncefact#113)
Browse files Browse the repository at this point in the history
<!--
  For Work In Progress Pull Requests, please use the Draft PR feature,
see https://github.blog/2019-02-14-introducing-draft-pull-requests/ for
further details.
  
  For a timely review/response, please avoid force-pushing additional
  commits if your PR already received reviews or comments.
  
Before submitting a Pull Request, please ensure you've done the
following:
- 📖 Read the [Contributing
Guide](https://github.com/uncefact/project-vckit/blob/main/CONTRIBUTING.md).
- 📖 Read the [Code of
Conduct](https://github.com/uncefact/project-vckit/blob/main/CODE_OF_CONDUCT.md).
  - 👷‍♀️ Create small PRs. In most cases, this will be possible.
  - ✅ Provide tests for your changes.
- 📝 Use descriptive commit messages following [conventional
commits](https://www.conventionalcommits.org/en/v1.0.0/).
- 📗 Update any related documentation and include any relevant
screenshots.
-->

## What type of PR is this? (check all applicable)

- [ ] 🍕 Feature
- [ ] 🐛 Bug Fix
- [ ] 📝 Documentation Update
- [ ] 🎨 Style
- [ ] 🧑‍💻 Code Refactor
- [ ] 🔥 Performance Improvements
- [x] ✅ Test
- [ ] 🤖 Build
- [ ] 🔁 CI
- [ ] 📦 Chore (Release)
- [ ] ⏩ Revert

## Description

<!-- 
Please do not leave this blank 
This PR [adds/removes/fixes/replaces] the [feature/bug/etc]. 
-->

Update the configuration for the unit test to make it can run at the
root. the vckit-oa-renderers package will run the test script separately
with another and include that script to the main test script `pnpm test`
to run at the root.

## Related Tickets & Documents
<!-- 
Please use this format link issue numbers: Fixes uncefact#123

https://docs.github.com/en/free-pro-team@latest/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
-->

## Mobile & Desktop Screenshots/Recordings

<!-- Visual changes require screenshots -->


## Added tests?

- [x] 👍 yes
- [ ] 🙅 no, because they aren't needed
- [ ] 🙋 no, because I need help

## Added to documentation?

- [ ] 📜 README.md
- [ ] 📓 [vc-kit doc site](https://uncefact.github.io/vckit/)
- [ ] 📕 storybook
- [ ] 🙅 no documentation needed

## [optional] Are there any post-deployment tasks we need to perform?


<!-- note: PRs with deleted sections will be marked invalid -->

---------

Signed-off-by: Nam Hoang <hoangxuannam160493@gmail.com>
  • Loading branch information
namhoang1604 authored Jun 27, 2023
1 parent e019a5b commit 0c3c3dd
Show file tree
Hide file tree
Showing 6 changed files with 8,170 additions and 20,612 deletions.
13 changes: 10 additions & 3 deletions jest.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { defaults } from 'jest-config';

export default {
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'jsonld'],
rootDir: './',
moduleFileExtensions: [...defaults.moduleFileExtensions, 'mts'],
collectCoverage: false,
collectCoverageFrom: [
'packages/**/src/**/*.ts',
Expand All @@ -13,10 +16,14 @@ export default {
coverageProvider: 'v8',
coverageDirectory: './coverage',
extensionsToTreatAsEsm: ['.ts'],
testMatch: ['**/__tests__/**/*.test.*'],
testMatch: ['**/__tests__/**/*.test.ts'],
testPathIgnorePatterns: [
'<rootDir>/node_modules/',
'<rootDir>/packages/credential-ld/',
'<rootDir>/packages/vckit-oa-renderers/',
],
testEnvironment: 'node',
automock: false,
setupFiles: ['./setupJest.js'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
"build:js": "pnpm -r --stream build",
"build:api": "pnpm --package=@vckit/cli -c dlx \"pnpm -r --stream extract-api\"",
"build:schema": "pnpm --package=@vckit/cli -c dlx \"pnpm -r --stream generate-plugin-schema\"",
"test:ci": "pnpm test -- --coverage=true",
"test": "cross-env NODE_OPTIONS=\"--experimental-vm-modules --experimental-import-meta-resolve\" jest",
"test:watch": "pnpm test --watch --verbose",
"test": "pnpm test:packages && pnpm test:oa-renderer",
"test:packages-ci": "pnpm test:packages --coverage=true",
"test:packages": "cross-env NODE_OPTIONS=\"--experimental-vm-modules --experimental-import-meta-resolve\" jest",
"test:packages-watch": "pnpm test:packages --watch --verbose",
"test:oa-renderer": "cd packages/vckit-oa-renderers && pnpm test",
"test:oa-renderer-ci": "pnpm test:oa-renderer -- --coverage=true",
"watch": "pnpm -r exec --if-present 'pnpm run watch'",
"vckit": "cross-env NODE_NO_WARNINGS=1 ./packages/cli/bin/vckit.js",
"prettier": "prettier --write \"{packages,docs,__tests__,!build}/**/*.{ts,js,json,md,yml}\"",
Expand Down Expand Up @@ -43,6 +46,7 @@
"express": "^4.18.2",
"ganache": "7.7.3",
"jest": "29.3.1",
"jest-config": "29.5.0",
"jest-environment-jsdom": "29.3.1",
"jest-environment-node": "29.3.1",
"json-schema": "0.4.0",
Expand Down
46 changes: 18 additions & 28 deletions packages/credential-oa/__tests__/action-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ import {
IssuerAgentContext,
} from '@vckit/core-types';

import { CredentialPlugin } from '../src';
import { INVALID_RAW_OA_CREDENTIAL, RAW_OA_CREDENTIAL } from './mocks/constants';
import { CredentialIssuerOA } from '../src/action-handler';
import {
INVALID_RAW_OA_CREDENTIAL,
RAW_OA_CREDENTIAL,
} from './mocks/constants';

describe('CredentialPlugin', () => {
describe('CredentialIssuerOA', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('createVerifiableCredentialOA', () => {
it('should create a verifiable credential with valid inputs', async () => {
const credentialPlugin = new CredentialPlugin();
const credentialPlugin = new CredentialIssuerOA();
const args: ICreateVerifiableCredentialArgs = {
credential: RAW_OA_CREDENTIAL,
proofFormat: 'OpenAttestationMerkleProofSignature2018',
};

const signature = '0x0987';
const did = 'did:ethr:0xabc123';

const issuerAgentContext = {
agent: {
didManagerGet: jest.fn().mockReturnValue({
Expand All @@ -40,12 +40,10 @@ describe('CredentialPlugin', () => {
keyManagerSign: jest.fn().mockReturnValue(signature),
},
} as unknown as IssuerAgentContext;

const result = await credentialPlugin.createVerifiableCredentialOA(
args,
issuerAgentContext
);

expect(result).toBeDefined();
expect(result['@context']).toContain(
'https://www.w3.org/2018/credentials/v1'
Expand All @@ -55,15 +53,12 @@ describe('CredentialPlugin', () => {
expect(result.proof.signature).toEqual(signature);
expect(result.proof.key).toEqual(`${did}#controller`);
});

it('should throw an error if DID not managed by KMS', async () => {
const credentialPlugin = new CredentialPlugin();

const credentialPlugin = new CredentialIssuerOA();
const args: ICreateVerifiableCredentialArgs = {
credential: RAW_OA_CREDENTIAL,
proofFormat: 'OpenAttestationMerkleProofSignature2018',
};

const did = 'did:ethr:0xabc123';
const issuerAgentContext = {
agent: {
Expand All @@ -80,46 +75,41 @@ describe('CredentialPlugin', () => {
}),
},
} as unknown as IssuerAgentContext;

await expect(
credentialPlugin.createVerifiableCredentialOA(args, issuerAgentContext)
).rejects.toThrow('No signing key for ' + did);
});


it('should throw an error if payload is not in OpenAttestation format', async () => {
const credentialPlugin = new CredentialPlugin();

const credentialPlugin = new CredentialIssuerOA();
const args: ICreateVerifiableCredentialArgs = {
credential: INVALID_RAW_OA_CREDENTIAL,
proofFormat: 'OpenAttestationMerkleProofSignature2018',
};

await expect(
credentialPlugin.createVerifiableCredentialOA(args, {} as IssuerAgentContext)
).rejects.toThrow('invalid_argument: credential is not a valid OpenAttestation document');
credentialPlugin.createVerifiableCredentialOA(
args,
{} as IssuerAgentContext
)
).rejects.toThrow(
'invalid_argument: credential is not a valid OpenAttestation document'
);
});

it('should throw an error if key type is not Secp256k1', async () => {
const credentialPlugin = new CredentialPlugin();
const credentialPlugin = new CredentialIssuerOA();
const args: ICreateVerifiableCredentialArgs = {
credential: RAW_OA_CREDENTIAL,
proofFormat: 'OpenAttestationMerkleProofSignature2018',
};

const issuerAgentContext = {
agent: {
didManagerGet: jest.fn().mockImplementation(() => {
throw new Error();
}),
},
} as unknown as IssuerAgentContext;

await expect(
credentialPlugin.createVerifiableCredentialOA(args, issuerAgentContext)
).rejects.toThrow(
'invalid_argument: did not found in the DID manager'
);
).rejects.toThrow('invalid_argument: did not found in the DID manager');
});
});
});
Loading

0 comments on commit 0c3c3dd

Please sign in to comment.