From 8be56330f53291f6ede610cdc15f354628f8595e Mon Sep 17 00:00:00 2001 From: Yen Le Date: Tue, 3 Dec 2024 15:22:00 +0700 Subject: [PATCH 1/6] test: setup unit test for playground package --- .../untp-playground/__tests__/mocks/vc.ts | 314 ++++++++++++++++++ .../untp-playground/__tests__/page.test.tsx | 140 ++++++++ packages/untp-playground/jest.config.js | 38 --- packages/untp-playground/jest.config.ts | 203 +++++++++++ packages/untp-playground/jest.setup.ts | 1 + packages/untp-playground/package.json | 11 +- packages/untp-playground/tsconfig.json | 14 +- 7 files changed, 676 insertions(+), 45 deletions(-) create mode 100644 packages/untp-playground/__tests__/mocks/vc.ts create mode 100644 packages/untp-playground/__tests__/page.test.tsx delete mode 100644 packages/untp-playground/jest.config.js create mode 100644 packages/untp-playground/jest.config.ts create mode 100644 packages/untp-playground/jest.setup.ts diff --git a/packages/untp-playground/__tests__/mocks/vc.ts b/packages/untp-playground/__tests__/mocks/vc.ts new file mode 100644 index 00000000..dcc0a5d7 --- /dev/null +++ b/packages/untp-playground/__tests__/mocks/vc.ts @@ -0,0 +1,314 @@ +export const mockCredential = { + verifiableCredential: { + '@context': [ + 'https://www.w3.org/2018/credentials/v1', + 'https://w3id.org/vc-revocation-list-2020/v1', + 'https://w3id.org/security/suites/jws-2020/v1', + ], + type: ['VerifiableCredential'], + issuer: { + id: 'did:web:api.vckit.showthething.com', + }, + credentialSubject: { + pdfUrl: '', + number: 'C-101507453', + consignmentNumber: 'C-101507454', + forms: [ + { + serialNumber: '41384822', + type: 'LPAC1', + }, + ], + movementDate: '2023-06-10T00:00:00', + movementTime: '01:00', + createdAt: '2023-10-30T08:18:49.810Z', + updatedAt: '2023-10-30T08:18:49.810Z', + submittedAt: '2023-10-30T08:18:49.810Z', + origin: { + name: 'AgTrace farms', + pic: 'NH020188', + }, + destination: { + name: 'AgTrace processors', + pic: 'QBZZ2222', + }, + declaration: { + accept: true, + fullName: 'danny', + phone: '0299999999', + date: '2020-09-24T00:00:00', + signature: + 'file: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lYFsexLYsnj63PrxPXkRfDgBW29n7xwy2wobw1IdXqplpID/gGW0Q1EptiSeUw7Zi2ATqJWAF6HCcLjN+YqH717N9wKU7126u4DrkyWdEYXz3tw/Zob0vvPpGy1D/XCjaihnSLKpHMHrv4fmYXtb2zvnPbMOy3MABBDPA2gkirg6AnY2c9pOUD2VY+v7N5JuYv7zcNp6K+KZWp3hrqLDaSlKCgEILIrAVsVzK/VeVGejMBBYE4EtiIi3u3hYFD9bqPea+il1gcDiCGxBRFgoWly3o0AQOHwCiOfhtyE1gAAE9kAA8dwDdLKEAAQOn8DWxJON8YffZ6kBBBZBYAviuQjQFAICEFgXAcRzXe1JbSAAgZkIIJ4zgSYbCEBgXQQQz3W1J7WBAARmIoB4zgSabCAAgXURQDzX1Z7UBgIQmIkA4jkTaLKBAATWRQDxXFd7UhsIQGAmAojnTKDJBgIQWBcBxHNd7UltIACBmQggnjOBJhsIQGBdBBDPdbUntYEABGYigHjOBJpsIACBdRFAPNfVntQGAhCYiQDiORNosoEABNZFAPFcV3tSGwhAYCYCiOdMoMkGAhBYFwHEc13tSW0gAIGZCCCeM4EmGwhAYF0E/gOo3R2PX9lA0QAAAABJRU5ErkJggg==', + address: { + line1: '123', + town: 'Fake', + state: 'NSW', + postcode: '2000', + }, + email: 'dan@da.com', + certificateNumber: null, + }, + answers: [ + { + questionId: '3', + value: '1', + index: 0, + }, + { + questionId: '3', + value: '2', + index: 1, + }, + { + questionId: '4', + value: 'Hereford', + index: 0, + }, + { + questionId: '4', + value: 'Hereford', + index: 1, + }, + { + questionId: '5', + value: 'Bull : M', + index: 0, + }, + { + questionId: '5', + value: 'Bull : M', + index: 1, + }, + { + questionId: '8', + value: 'No', + index: 0, + }, + { + questionId: '8', + value: 'No', + index: 1, + }, + { + questionId: '10', + value: 'NH020188LEJ00005', + index: 0, + }, + { + questionId: '10', + value: 'NH020188LEJ00012', + index: 1, + }, + { + questionId: '15', + value: '1', + index: 0, + }, + { + questionId: '15', + value: '2', + index: 1, + }, + { + questionId: '16', + value: '2', + index: 1, + }, + { + questionId: '16', + value: '1', + index: 0, + }, + { + questionId: '17', + value: 'No', + index: null, + }, + { + questionId: '20', + value: '6to12Months', + index: null, + }, + { + questionId: '56', + value: 'Yes', + index: null, + }, + { + questionId: '57', + value: '6month detail', + index: null, + }, + { + questionId: '58', + value: 'No', + index: null, + }, + { + questionId: '59', + value: 'Yes', + index: null, + }, + { + questionId: '61', + value: 'stock feed1', + index: 0, + }, + { + questionId: '61', + value: 'stock feed2', + index: 1, + }, + { + questionId: '63', + value: '2023-06-01', + index: 0, + }, + { + questionId: '63', + value: '2023-06-01', + index: 1, + }, + { + questionId: '70', + value: 'Yes', + index: null, + }, + { + questionId: '72', + value: 'material1', + index: 0, + }, + { + questionId: '73', + value: '2022-06-01', + index: 0, + }, + { + questionId: '74', + value: '123', + index: 0, + }, + { + questionId: '75', + value: '2022-06-17', + index: 0, + }, + { + questionId: '76', + value: '2023-06-18', + index: 0, + }, + { + questionId: '88', + value: 'No', + index: null, + }, + { + questionId: '96', + value: 'Yes', + index: null, + }, + { + questionId: '98', + value: 'chemprod1', + index: 0, + }, + { + questionId: '99', + value: '2023-06-01', + index: 0, + }, + { + questionId: '100', + value: '999', + index: 0, + }, + { + questionId: '101', + value: '555', + index: 0, + }, + { + questionId: '102', + value: 'Yes', + index: null, + }, + { + questionId: '103', + value: '2023-06-08', + index: null, + }, + { + questionId: '158', + value: 'Mr Transporter', + index: 0, + }, + { + questionId: '158', + value: 'Mrs Transporter', + index: 1, + }, + { + questionId: '159', + value: 'ABC123', + index: 0, + }, + { + questionId: '159', + value: 'XYZ987', + index: 1, + }, + { + questionId: '160', + value: '0291919191', + index: 0, + }, + { + questionId: '160', + value: '0212121212', + index: 1, + }, + { + questionId: '162', + value: '2023-06-15', + index: 0, + }, + { + questionId: '162', + value: '2023-06-01', + index: 1, + }, + { + questionId: '163', + value: '13:00', + index: 0, + }, + { + questionId: '163', + value: '13:00', + index: 1, + }, + { + questionId: '164', + value: 'True', + index: 0, + }, + { + questionId: '164', + value: 'True', + index: 1, + }, + ], + }, + issuanceDate: '2023-10-30T08:18:51.864Z', + proof: { + type: 'JsonWebSignature2020', + created: '2023-10-30T08:18:52Z', + verificationMethod: + 'did:web:api.vckit.showthething.com#793a546194958d2cab66f4d80b11a029ca6a7f04209f0fa4003520d31543362c-key-0', + proofPurpose: 'assertionMethod', + jws: 'eyJhbGciOiJFZERTQSIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..W2FJVbKFjUOPgxggIkjTjzi5mAmJNVozbkjlUtYK0AK97uHpEh0p8oJaIsiwQ1_j7rqARJQgcIbRRIU4EV34Ag', + }, + }, +}; diff --git a/packages/untp-playground/__tests__/page.test.tsx b/packages/untp-playground/__tests__/page.test.tsx new file mode 100644 index 00000000..1da1a0d0 --- /dev/null +++ b/packages/untp-playground/__tests__/page.test.tsx @@ -0,0 +1,140 @@ +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; +import { toast } from 'sonner'; +import { decodeEnvelopedCredential, isEnvelopedProof } from '@/lib/credentialService'; +import { CredentialUploader } from '@/components/CredentialUploader'; +import Home from '../src/app/page'; +import { mockCredential } from './mocks/vc'; + +// Mock the dependencies +jest.mock('sonner', () => ({ + toast: { + error: jest.fn(), + }, +})); + +jest.mock('@/lib/credentialService', () => ({ + isEnvelopedProof: jest.fn(), + decodeEnvelopedCredential: jest.fn(), +})); + +// Mock child components +jest.mock('@/components/Header', () => ({ + Header: () =>
Header
, +})); + +jest.mock('@/components/Footer', () => ({ + Footer: () =>
Footer
, +})); + +jest.mock('@/components/TestResults', () => ({ + TestResults: () =>
Test Results
, +})); + +jest.mock('@/components/CredentialUploader', () => ({ + CredentialUploader: jest.fn(({ onCredentialUpload }: { onCredentialUpload: (credential: any) => void }) => ( + + )), +})); + +jest.mock('@/components/DownloadCredential', () => ({ + DownloadCredential: () =>
Download
, +})); + +describe('Home Component', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('renders all components correctly', () => { + render(); + + expect(screen.getByTestId('mock-header')).toBeInTheDocument(); + expect(screen.getByTestId('mock-footer')).toBeInTheDocument(); + expect(screen.getByTestId('mock-test-results')).toBeInTheDocument(); + expect(screen.getByTestId('mock-uploader')).toBeInTheDocument(); + expect(screen.getByTestId('mock-download')).toBeInTheDocument(); + }); + + it('handles valid credential upload', async () => { + (isEnvelopedProof as jest.Mock).mockReturnValue(false); + + render(); + + const uploader = screen.getByTestId('mock-uploader'); + fireEvent.click(uploader); + + await waitFor(() => { + expect(toast.error).not.toHaveBeenCalled(); + }); + }); + + it('handles invalid credential format', async () => { + (CredentialUploader as jest.Mock).mockImplementation( + ({ onCredentialUpload }: { onCredentialUpload: (credential: { verifiableCredential: any }) => void }) => ( + + ), + ); + + render(); + + const uploader = screen.getByTestId('mock-uploader'); + fireEvent.click(uploader); + + await waitFor(() => { + expect(toast.error).toHaveBeenCalledWith('Invalid credential format'); + }); + }); + + it('handles unknown credential type', async () => { + (CredentialUploader as jest.Mock).mockImplementation( + ({ onCredentialUpload }: { onCredentialUpload: (credential: { verifiableCredential: any }) => void }) => ( + + ), + ); + + (isEnvelopedProof as jest.Mock).mockReturnValue(false); + + render(); + + const uploader = screen.getByTestId('mock-uploader'); + fireEvent.click(uploader); + + await waitFor(() => { + expect(toast.error).toHaveBeenCalledWith('Unknown credential type'); + }); + }); + + it('handles enveloped credential correctly', async () => { + const mockEnvelopedCredential = { + type: ['VerifiableCredential', 'DigitalProductPassport'], + }; + + (isEnvelopedProof as jest.Mock).mockReturnValue(true); + (decodeEnvelopedCredential as jest.Mock).mockReturnValue(mockEnvelopedCredential); + + render(); + + const uploader = screen.getByTestId('mock-uploader'); + fireEvent.click(uploader); + + await waitFor(() => { + expect(decodeEnvelopedCredential).toHaveBeenCalled(); + expect(toast.error).not.toHaveBeenCalled(); + }); + }); +}); diff --git a/packages/untp-playground/jest.config.js b/packages/untp-playground/jest.config.js deleted file mode 100644 index 6878c1e3..00000000 --- a/packages/untp-playground/jest.config.js +++ /dev/null @@ -1,38 +0,0 @@ -const { defaults } = require('jest-config'); -const base = require('../../jest.config.base.js'); - -module.exports = { - ...base, - rootDir: './', - moduleFileExtensions: [...defaults.moduleFileExtensions, 'mts'], - collectCoverage: false, - collectCoverageFrom: [ - '!**/infra/**', - '!**/.next/**', - '!**/types/**', - '!**/build/**', - '!**/node_modules/**', - '!**/**/index.ts', - ], - coverageReporters: ['text', 'lcov', 'json', 'json-summary'], - coverageProvider: 'v8', - coverageDirectory: './coverage', - extensionsToTreatAsEsm: ['.ts'], - testMatch: ['**/__tests__/**/*.test.*'], - testPathIgnorePatterns: ['/node_modules/'], - preset: 'ts-jest', - testEnvironment: 'node', - automock: false, - moduleNameMapper: { - '^(\\.{1,2}/.*)\\.js$': '$1', - }, - transform: { - '\\.ts$': [ - 'ts-jest', - { - useESM: true, - }, - ], - }, - passWithNoTests: true, // TODO: remove this after adding tests -}; diff --git a/packages/untp-playground/jest.config.ts b/packages/untp-playground/jest.config.ts new file mode 100644 index 00000000..0e749cf6 --- /dev/null +++ b/packages/untp-playground/jest.config.ts @@ -0,0 +1,203 @@ +/** + * For a detailed explanation regarding each configuration property, visit: + * https://jestjs.io/docs/configuration + */ + +import type { Config } from 'jest'; +import nextJest from 'next/jest.js'; + +const createJestConfig = nextJest({ + // Provide the path to your Next.js app to load next.config.js and .env files in your test environment + dir: './', +}); + +const config: Config = { + // All imported modules in your tests should be mocked automatically + // automock: false, + + // Stop running tests after `n` failures + // bail: 0, + + // The directory where Jest should store its cached dependency information + // cacheDirectory: "/private/var/folders/gm/lxgxrqt116bctwr29c24v7yw0000gn/T/jest_dx", + + // Automatically clear mock calls, instances, contexts and results before every test + clearMocks: true, + + // Indicates whether the coverage information should be collected while executing the test + collectCoverage: true, + + // An array of glob patterns indicating a set of files for which coverage information should be collected + // collectCoverageFrom: undefined, + + // The directory where Jest should output its coverage files + coverageDirectory: 'coverage', + + // An array of regexp pattern strings used to skip coverage collection + // coveragePathIgnorePatterns: [ + // "/node_modules/" + // ], + + // Indicates which provider should be used to instrument code for coverage + // coverageProvider: "babel", + + // A list of reporter names that Jest uses when writing coverage reports + // coverageReporters: [ + // "json", + // "text", + // "lcov", + // "clover" + // ], + + // An object that configures minimum threshold enforcement for coverage results + // coverageThreshold: undefined, + + // A path to a custom dependency extractor + // dependencyExtractor: undefined, + + // Make calling deprecated APIs throw helpful error messages + // errorOnDeprecated: false, + + // The default configuration for fake timers + // fakeTimers: { + // "enableGlobally": false + // }, + + // Force coverage collection from ignored files using an array of glob patterns + // forceCoverageMatch: [], + + // A path to a module which exports an async function that is triggered once before all test suites + // globalSetup: undefined, + + // A path to a module which exports an async function that is triggered once after all test suites + // globalTeardown: undefined, + + // A set of global variables that need to be available in all test environments + // globals: {}, + + // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. + // maxWorkers: "50%", + + // An array of directory names to be searched recursively up from the requiring module's location + // moduleDirectories: [ + // "node_modules" + // ], + + // An array of file extensions your modules use + // moduleFileExtensions: [ + // "js", + // "mjs", + // "cjs", + // "jsx", + // "ts", + // "tsx", + // "json", + // "node" + // ], + + // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module + moduleNameMapper: { + '^@/(.*)$': '/src/$1', + }, + + // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader + // modulePathIgnorePatterns: [], + + // Activates notifications for test results + // notify: false, + + // An enum that specifies notification mode. Requires { notify: true } + // notifyMode: "failure-change", + + // A preset that is used as a base for Jest's configuration + // preset: undefined, + + // Run tests from one or more projects + // projects: undefined, + + // Use this configuration option to add custom reporters to Jest + // reporters: undefined, + + // Automatically reset mock state before every test + // resetMocks: false, + + // Reset the module registry before running each individual test + // resetModules: false, + + // A path to a custom resolver + // resolver: undefined, + + // Automatically restore mock state and implementation before every test + // restoreMocks: false, + + // The root directory that Jest should scan for tests and modules within + // rootDir: undefined, + + // A list of paths to directories that Jest should use to search for files in + // roots: [ + // "" + // ], + + // Allows you to use a custom runner instead of Jest's default test runner + // runner: "jest-runner", + + // The paths to modules that run some code to configure or set up the testing environment before each test + // setupFiles: [], + + // A list of paths to modules that run some code to configure or set up the testing framework before each test + setupFilesAfterEnv: ['/jest.setup.ts'], + + // The number of seconds after which a test is considered as slow and reported as such in the results. + // slowTestThreshold: 5, + + // A list of paths to snapshot serializer modules Jest should use for snapshot testing + // snapshotSerializers: [], + + // The test environment that will be used for testing + testEnvironment: 'jsdom', + + // Options that will be passed to the testEnvironment + // testEnvironmentOptions: {}, + + // Adds a location field to test results + // testLocationInResults: false, + + // The glob patterns Jest uses to detect test files + testMatch: ['**/__tests__/**/*.(spec|test).[jt]s?(x)', '**/?(*.)+(spec|test).[tj]s?(x)'], + + // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped + testPathIgnorePatterns: ['/node_modules/'], + + // The regexp pattern or array of patterns that Jest uses to detect test files + // testRegex: [], + + // This option allows the use of a custom results processor + // testResultsProcessor: undefined, + + // This option allows use of a custom test runner + // testRunner: "jest-circus/runner", + + // A map from regular expressions to paths to transformers + // transform: undefined, + + // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation + // transformIgnorePatterns: [ + // "/node_modules/", + // "\\.pnp\\.[^\\/]+$" + // ], + + // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them + // unmockedModulePathPatterns: undefined, + + // Indicates whether each individual test should be reported during the run + // verbose: undefined, + + // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode + // watchPathIgnorePatterns: [], + + // Whether to use watchman for file crawling + // watchman: true, +}; + +// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async +export default createJestConfig(config); diff --git a/packages/untp-playground/jest.setup.ts b/packages/untp-playground/jest.setup.ts new file mode 100644 index 00000000..7b0828bf --- /dev/null +++ b/packages/untp-playground/jest.setup.ts @@ -0,0 +1 @@ +import '@testing-library/jest-dom'; diff --git a/packages/untp-playground/package.json b/packages/untp-playground/package.json index 31e48058..5e6f1120 100644 --- a/packages/untp-playground/package.json +++ b/packages/untp-playground/package.json @@ -7,7 +7,8 @@ "build": "next build", "start": "next start", "lint": "next lint", - "test": "jest" + "test": "jest", + "test:watch": "jest --watch" }, "dependencies": { "@radix-ui/react-dialog": "^1.1.2", @@ -29,14 +30,22 @@ "tailwindcss-animate": "^1.0.7" }, "devDependencies": { + "@testing-library/dom": "^10.4.0", + "@testing-library/jest-dom": "^6.6.3", + "@testing-library/react": "^16.0.1", "@types/canvas-confetti": "^1.6.4", "@types/node": "^20", "@types/react": "^18", "@types/react-dom": "^18", "eslint": "^8", "eslint-config-next": "15.0.3", + "jest": "^29.7.0", + "jest-config": "^29.7.0", + "jest-environment-jsdom": "^29.7.0", + "jest-runtime": "^29.7.0", "postcss": "^8", "tailwindcss": "^3.4.1", + "ts-node": "^10.9.2", "typescript": "^5" } } diff --git a/packages/untp-playground/tsconfig.json b/packages/untp-playground/tsconfig.json index 7aa97d60..401d0910 100644 --- a/packages/untp-playground/tsconfig.json +++ b/packages/untp-playground/tsconfig.json @@ -22,21 +22,23 @@ "name": "next" } ], - "paths": { + "paths": { "@/*": [ "./src/*" ] } }, "include": [ - "next-env.d.ts", - "**/*.ts", - "**/*.tsx", - ".next/types/**/*.ts" + "next-env.d.ts", + "**/*.ts", + "**/*.tsx", + ".next/types/**/*.ts", + "__tests__/**/*.ts", + "__tests__/**/*.tsx" ], "exclude": [ "node_modules", ".next", "infra" ] -} \ No newline at end of file +} From d7e2d1511d1141ab1e4e50f66d15c0236af6e817 Mon Sep 17 00:00:00 2001 From: Yen Le Date: Tue, 3 Dec 2024 16:03:38 +0700 Subject: [PATCH 2/6] test: factor test strcuture in playground --- .github/workflows/build_test.yml | 1 + package.json | 1 + .../__tests__/{ => app}/page.test.tsx | 4 +- packages/untp-playground/jest.config.js | 27 +++ packages/untp-playground/jest.config.ts | 203 ------------------ packages/untp-playground/tsconfig.json | 3 +- 6 files changed, 33 insertions(+), 206 deletions(-) rename packages/untp-playground/__tests__/{ => app}/page.test.tsx (97%) create mode 100644 packages/untp-playground/jest.config.js delete mode 100644 packages/untp-playground/jest.config.ts diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml index db7d0c43..199e54e6 100644 --- a/.github/workflows/build_test.yml +++ b/.github/workflows/build_test.yml @@ -49,6 +49,7 @@ jobs: Services, ./packages/services/coverage/coverage-summary.json UNTP test suite, ./packages/untp-test-suite/coverage/coverage-summary.json VC test suite, ./packages/vc-test-suite/coverage/coverage-summary.json + UNTP Playground, ./packages/untp-playground/coverage/coverage-summary.json build_docs: runs-on: ubuntu-latest diff --git a/package.json b/package.json index 9953e2c0..40ed2076 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "test:untp-test-suite": "cd packages/untp-test-suite && yarn run test", "test:vc-test-suite": "cd packages/vc-test-suite && yarn run test", "test:integration:untp-test-suite": "cd packages/untp-test-suite && yarn run test:integration", + "test:untp-playground": "cd packages/untp-playground && yarn run test", "storybook:components": "cd packages/components && yarn run storybook", "storybook:mock-app": "cd packages/mock-app && yarn run storybook", "untp": "node ./packages/untp-test-suite/build/interfaces/cli/cli.js", diff --git a/packages/untp-playground/__tests__/page.test.tsx b/packages/untp-playground/__tests__/app/page.test.tsx similarity index 97% rename from packages/untp-playground/__tests__/page.test.tsx rename to packages/untp-playground/__tests__/app/page.test.tsx index 1da1a0d0..be9fed3d 100644 --- a/packages/untp-playground/__tests__/page.test.tsx +++ b/packages/untp-playground/__tests__/app/page.test.tsx @@ -2,8 +2,8 @@ import { render, screen, fireEvent, waitFor } from '@testing-library/react'; import { toast } from 'sonner'; import { decodeEnvelopedCredential, isEnvelopedProof } from '@/lib/credentialService'; import { CredentialUploader } from '@/components/CredentialUploader'; -import Home from '../src/app/page'; -import { mockCredential } from './mocks/vc'; +import Home from '../../src/app/page'; +import { mockCredential } from '../mocks/vc'; // Mock the dependencies jest.mock('sonner', () => ({ diff --git a/packages/untp-playground/jest.config.js b/packages/untp-playground/jest.config.js new file mode 100644 index 00000000..0366973f --- /dev/null +++ b/packages/untp-playground/jest.config.js @@ -0,0 +1,27 @@ +/** + * For a detailed explanation regarding each configuration property, visit: + * https://jestjs.io/docs/configuration + */ + +const nextJest = require('next/jest'); + +const createJestConfig = nextJest({ + // Provide the path to your Next.js app to load next.config.js and .env files in your test environment + dir: './', +}); + +const config = { + clearMocks: true, + collectCoverage: true, + coverageDirectory: 'coverage', + moduleNameMapper: { + '^@/(.*)$': '/src/$1', + }, + setupFilesAfterEnv: ['/jest.setup.ts'], + testEnvironment: 'jsdom', + testMatch: ['**/__tests__/**/*.(spec|test).[jt]s?(x)', '**/?(*.)+(spec|test).[tj]s?(x)'], + testPathIgnorePatterns: ['/node_modules/'], +}; + +// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async +module.exports = createJestConfig(config); diff --git a/packages/untp-playground/jest.config.ts b/packages/untp-playground/jest.config.ts deleted file mode 100644 index 0e749cf6..00000000 --- a/packages/untp-playground/jest.config.ts +++ /dev/null @@ -1,203 +0,0 @@ -/** - * For a detailed explanation regarding each configuration property, visit: - * https://jestjs.io/docs/configuration - */ - -import type { Config } from 'jest'; -import nextJest from 'next/jest.js'; - -const createJestConfig = nextJest({ - // Provide the path to your Next.js app to load next.config.js and .env files in your test environment - dir: './', -}); - -const config: Config = { - // All imported modules in your tests should be mocked automatically - // automock: false, - - // Stop running tests after `n` failures - // bail: 0, - - // The directory where Jest should store its cached dependency information - // cacheDirectory: "/private/var/folders/gm/lxgxrqt116bctwr29c24v7yw0000gn/T/jest_dx", - - // Automatically clear mock calls, instances, contexts and results before every test - clearMocks: true, - - // Indicates whether the coverage information should be collected while executing the test - collectCoverage: true, - - // An array of glob patterns indicating a set of files for which coverage information should be collected - // collectCoverageFrom: undefined, - - // The directory where Jest should output its coverage files - coverageDirectory: 'coverage', - - // An array of regexp pattern strings used to skip coverage collection - // coveragePathIgnorePatterns: [ - // "/node_modules/" - // ], - - // Indicates which provider should be used to instrument code for coverage - // coverageProvider: "babel", - - // A list of reporter names that Jest uses when writing coverage reports - // coverageReporters: [ - // "json", - // "text", - // "lcov", - // "clover" - // ], - - // An object that configures minimum threshold enforcement for coverage results - // coverageThreshold: undefined, - - // A path to a custom dependency extractor - // dependencyExtractor: undefined, - - // Make calling deprecated APIs throw helpful error messages - // errorOnDeprecated: false, - - // The default configuration for fake timers - // fakeTimers: { - // "enableGlobally": false - // }, - - // Force coverage collection from ignored files using an array of glob patterns - // forceCoverageMatch: [], - - // A path to a module which exports an async function that is triggered once before all test suites - // globalSetup: undefined, - - // A path to a module which exports an async function that is triggered once after all test suites - // globalTeardown: undefined, - - // A set of global variables that need to be available in all test environments - // globals: {}, - - // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. - // maxWorkers: "50%", - - // An array of directory names to be searched recursively up from the requiring module's location - // moduleDirectories: [ - // "node_modules" - // ], - - // An array of file extensions your modules use - // moduleFileExtensions: [ - // "js", - // "mjs", - // "cjs", - // "jsx", - // "ts", - // "tsx", - // "json", - // "node" - // ], - - // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module - moduleNameMapper: { - '^@/(.*)$': '/src/$1', - }, - - // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader - // modulePathIgnorePatterns: [], - - // Activates notifications for test results - // notify: false, - - // An enum that specifies notification mode. Requires { notify: true } - // notifyMode: "failure-change", - - // A preset that is used as a base for Jest's configuration - // preset: undefined, - - // Run tests from one or more projects - // projects: undefined, - - // Use this configuration option to add custom reporters to Jest - // reporters: undefined, - - // Automatically reset mock state before every test - // resetMocks: false, - - // Reset the module registry before running each individual test - // resetModules: false, - - // A path to a custom resolver - // resolver: undefined, - - // Automatically restore mock state and implementation before every test - // restoreMocks: false, - - // The root directory that Jest should scan for tests and modules within - // rootDir: undefined, - - // A list of paths to directories that Jest should use to search for files in - // roots: [ - // "" - // ], - - // Allows you to use a custom runner instead of Jest's default test runner - // runner: "jest-runner", - - // The paths to modules that run some code to configure or set up the testing environment before each test - // setupFiles: [], - - // A list of paths to modules that run some code to configure or set up the testing framework before each test - setupFilesAfterEnv: ['/jest.setup.ts'], - - // The number of seconds after which a test is considered as slow and reported as such in the results. - // slowTestThreshold: 5, - - // A list of paths to snapshot serializer modules Jest should use for snapshot testing - // snapshotSerializers: [], - - // The test environment that will be used for testing - testEnvironment: 'jsdom', - - // Options that will be passed to the testEnvironment - // testEnvironmentOptions: {}, - - // Adds a location field to test results - // testLocationInResults: false, - - // The glob patterns Jest uses to detect test files - testMatch: ['**/__tests__/**/*.(spec|test).[jt]s?(x)', '**/?(*.)+(spec|test).[tj]s?(x)'], - - // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped - testPathIgnorePatterns: ['/node_modules/'], - - // The regexp pattern or array of patterns that Jest uses to detect test files - // testRegex: [], - - // This option allows the use of a custom results processor - // testResultsProcessor: undefined, - - // This option allows use of a custom test runner - // testRunner: "jest-circus/runner", - - // A map from regular expressions to paths to transformers - // transform: undefined, - - // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation - // transformIgnorePatterns: [ - // "/node_modules/", - // "\\.pnp\\.[^\\/]+$" - // ], - - // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them - // unmockedModulePathPatterns: undefined, - - // Indicates whether each individual test should be reported during the run - // verbose: undefined, - - // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode - // watchPathIgnorePatterns: [], - - // Whether to use watchman for file crawling - // watchman: true, -}; - -// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async -export default createJestConfig(config); diff --git a/packages/untp-playground/tsconfig.json b/packages/untp-playground/tsconfig.json index 401d0910..69b111ae 100644 --- a/packages/untp-playground/tsconfig.json +++ b/packages/untp-playground/tsconfig.json @@ -34,7 +34,8 @@ "**/*.tsx", ".next/types/**/*.ts", "__tests__/**/*.ts", - "__tests__/**/*.tsx" + "__tests__/**/*.tsx", + "jest.config.js" ], "exclude": [ "node_modules", From 999aa2dad7a78fb2258733bd6e63cee15703a88f Mon Sep 17 00:00:00 2001 From: Yen Le Date: Tue, 3 Dec 2024 16:19:35 +0700 Subject: [PATCH 3/6] chore: add modulePathIgnorePatterns to fix jest-haste-map collision in playground --- package.json | 2 +- packages/untp-playground/jest.config.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 40ed2076..05b4a35b 100644 --- a/package.json +++ b/package.json @@ -21,8 +21,8 @@ "test:mock-app": "cd packages/mock-app && yarn run test", "test:untp-test-suite": "cd packages/untp-test-suite && yarn run test", "test:vc-test-suite": "cd packages/vc-test-suite && yarn run test", - "test:integration:untp-test-suite": "cd packages/untp-test-suite && yarn run test:integration", "test:untp-playground": "cd packages/untp-playground && yarn run test", + "test:integration:untp-test-suite": "cd packages/untp-test-suite && yarn run test:integration", "storybook:components": "cd packages/components && yarn run storybook", "storybook:mock-app": "cd packages/mock-app && yarn run storybook", "untp": "node ./packages/untp-test-suite/build/interfaces/cli/cli.js", diff --git a/packages/untp-playground/jest.config.js b/packages/untp-playground/jest.config.js index 0366973f..33c3a6cf 100644 --- a/packages/untp-playground/jest.config.js +++ b/packages/untp-playground/jest.config.js @@ -21,6 +21,7 @@ const config = { testEnvironment: 'jsdom', testMatch: ['**/__tests__/**/*.(spec|test).[jt]s?(x)', '**/?(*.)+(spec|test).[tj]s?(x)'], testPathIgnorePatterns: ['/node_modules/'], + modulePathIgnorePatterns: ['/build', '/dist', '/.next'], }; // createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async From 3de9ae0bdb0cd36f17e6128cbe3279169845d7fc Mon Sep 17 00:00:00 2001 From: Yen Le Date: Tue, 3 Dec 2024 16:55:37 +0700 Subject: [PATCH 4/6] chore: update jest config in playground to export json summary --- packages/untp-playground/infra/package.json | 2 +- packages/untp-playground/jest.config.js | 1 + packages/untp-playground/package.json | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/untp-playground/infra/package.json b/packages/untp-playground/infra/package.json index 176106d6..4fa1336f 100644 --- a/packages/untp-playground/infra/package.json +++ b/packages/untp-playground/infra/package.json @@ -1,5 +1,5 @@ { - "name": "untp-playground", + "name": "infra-untp-playground", "main": "index.ts", "devDependencies": { "@types/node": "^18", diff --git a/packages/untp-playground/jest.config.js b/packages/untp-playground/jest.config.js index 33c3a6cf..f74c68da 100644 --- a/packages/untp-playground/jest.config.js +++ b/packages/untp-playground/jest.config.js @@ -14,6 +14,7 @@ const config = { clearMocks: true, collectCoverage: true, coverageDirectory: 'coverage', + coverageReporters: ['text', 'lcov', 'json', 'json-summary'], moduleNameMapper: { '^@/(.*)$': '/src/$1', }, diff --git a/packages/untp-playground/package.json b/packages/untp-playground/package.json index 5e6f1120..1fdb8710 100644 --- a/packages/untp-playground/package.json +++ b/packages/untp-playground/package.json @@ -8,7 +8,8 @@ "start": "next start", "lint": "next lint", "test": "jest", - "test:watch": "jest --watch" + "test:watch": "jest --watch", + "test:coverage": "jest --coverage" }, "dependencies": { "@radix-ui/react-dialog": "^1.1.2", From e6a6e1952a055e788d3b452ec343ae7160f6c3bb Mon Sep 17 00:00:00 2001 From: Yen Le Date: Tue, 3 Dec 2024 17:03:54 +0700 Subject: [PATCH 5/6] doc: add document to run unit test in playground --- packages/untp-playground/README.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/untp-playground/README.md b/packages/untp-playground/README.md index 9ac047c7..f8edc7ec 100644 --- a/packages/untp-playground/README.md +++ b/packages/untp-playground/README.md @@ -11,6 +11,7 @@ Open [http://localhost:3000/](http://localhost:3000/) with your browser to see t ## Deployment We use Pulumi and GitHub actions to deploy the app. The following env variables needs to be set to define basePath in order to it work with the existing CloudFront Distribution: + ``` NEXT_PUBLIC_BASE_PATH: /untp-playground NEXT_PUBLIC_ASSET_PREFIX: /untp-playground @@ -19,7 +20,7 @@ NEXT_PUBLIC_IMAGE_PATH: /untp-playground/_next/image GitHub OIDC is configured in AWS account for GitHub Actions workflow to assume and run the deployment using Pulumi. The role to assume is set as a repository secret `UNCEFACT_AWS_ROLE_TO_ASSUME`. Pulume security prodiver for config is set to awskms. -The same backend state bucket and kms key are used for both test and prod Pulumi stacks. +The same backend state bucket and kms key are used for both test and prod Pulumi stacks. End-points: @@ -30,3 +31,25 @@ End-points: In future production enddpoint will be replaced with a production url, and current endpoint will become test. The production build is configured using Docker image https://nextjs.org/docs/pages/building-your-application/deploying#docker-image + +## Testing + +We use Jest for testing. + +To run tests: + +```bash +yarn test +``` + +To run tests in watch mode: + +```bash +yarn test:watch +``` + +To generate coverage report: + +```bash +yarn test:coverage +``` From 1f7f9d2958d5fc8c72ae810f3e9f5a03695d7c3d Mon Sep 17 00:00:00 2001 From: Yen Le Date: Tue, 3 Dec 2024 17:59:58 +0700 Subject: [PATCH 6/6] chore: update test coverage from in playground --- .../untp-playground/__tests__/app/page.test.tsx | 15 +++++++++++++++ packages/untp-playground/jest.config.js | 5 ++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/untp-playground/__tests__/app/page.test.tsx b/packages/untp-playground/__tests__/app/page.test.tsx index be9fed3d..8e5584af 100644 --- a/packages/untp-playground/__tests__/app/page.test.tsx +++ b/packages/untp-playground/__tests__/app/page.test.tsx @@ -137,4 +137,19 @@ describe('Home Component', () => { expect(toast.error).not.toHaveBeenCalled(); }); }); + + it('handles error decoding credential', async () => { + (isEnvelopedProof as jest.Mock).mockImplementationOnce(() => { + throw new Error('Error decoding credential'); + }); + + render(); + + const uploader = screen.getByTestId('mock-uploader'); + fireEvent.click(uploader); + + await waitFor(() => { + expect(toast.error).toHaveBeenCalledWith('Failed to process credential'); + }); + }); }); diff --git a/packages/untp-playground/jest.config.js b/packages/untp-playground/jest.config.js index f74c68da..ade2dc81 100644 --- a/packages/untp-playground/jest.config.js +++ b/packages/untp-playground/jest.config.js @@ -12,8 +12,6 @@ const createJestConfig = nextJest({ const config = { clearMocks: true, - collectCoverage: true, - coverageDirectory: 'coverage', coverageReporters: ['text', 'lcov', 'json', 'json-summary'], moduleNameMapper: { '^@/(.*)$': '/src/$1', @@ -21,8 +19,9 @@ const config = { setupFilesAfterEnv: ['/jest.setup.ts'], testEnvironment: 'jsdom', testMatch: ['**/__tests__/**/*.(spec|test).[jt]s?(x)', '**/?(*.)+(spec|test).[tj]s?(x)'], - testPathIgnorePatterns: ['/node_modules/'], + testPathIgnorePatterns: ['/node_modules/', '__tests__/mocks/*.ts'], modulePathIgnorePatterns: ['/build', '/dist', '/.next'], + collectCoverageFrom: ['src/**/*.{ts,tsx}', '!src/**/*.d.ts', '!src/**/index', '!src/**/types'], }; // createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async