diff --git a/packages/toolkit/connector-kit/jest.config.js b/packages/toolkit/connector-kit/jest.config.js new file mode 100644 index 000000000..47665f39a --- /dev/null +++ b/packages/toolkit/connector-kit/jest.config.js @@ -0,0 +1,13 @@ +/** @type {import('jest').Config} */ +const config = { + transform: {}, + coveragePathIgnorePatterns: ['/node_modules/', '/src/__mocks__/'], + coverageReporters: ['text-summary', 'lcov'], + coverageProvider: 'v8', + roots: ['./lib'], + moduleNameMapper: { + '^(chalk|inquirer)$': '/../../shared/lib/esm/module-proxy.js', + }, +}; + +export default config; diff --git a/packages/toolkit/connector-kit/package.json b/packages/toolkit/connector-kit/package.json index 7204ab010..8cc21ddaf 100644 --- a/packages/toolkit/connector-kit/package.json +++ b/packages/toolkit/connector-kit/package.json @@ -29,7 +29,11 @@ "build:test": "pnpm build", "lint": "eslint --ext .ts src", "lint:report": "pnpm lint --format json --output-file report.json", - "prepack": "pnpm build" + "prepack": "pnpm build", + "test:only": "NODE_OPTIONS=--experimental-vm-modules jest", + "test": "pnpm build:test && pnpm test:only", + "test:ci": "pnpm test:only", + "test:coverage": "pnpm test:only --silent --coverage" }, "dependencies": { "@logto/language-kit": "workspace:^1.0.0", @@ -39,10 +43,13 @@ "zod": "^3.20.2" }, "devDependencies": { + "@jest/types": "^29.0.3", "@silverhand/eslint-config": "3.0.1", "@silverhand/ts-config": "3.0.0", + "@types/jest": "^29.4.0", "@types/node": "^18.11.18", "eslint": "^8.34.0", + "jest": "^29.5.0", "lint-staged": "^13.0.0", "prettier": "^2.8.2", "tslib": "^2.4.1", diff --git a/packages/toolkit/connector-kit/src/index.test.ts b/packages/toolkit/connector-kit/src/index.test.ts new file mode 100644 index 000000000..f275d5f3f --- /dev/null +++ b/packages/toolkit/connector-kit/src/index.test.ts @@ -0,0 +1,50 @@ +import { z } from 'zod'; + +import { parseJson, parseJsonObject, validateConfig } from './index.js'; + +describe('connector-kit', () => { + describe('validateConfig', () => { + it('valid config', () => { + const testingTypeGuard = z.unknown(); + type TestingType = z.infer; + const testingConfig = { foo: 'foo', bar: 1, baz: true }; + expect(() => { + validateConfig(testingConfig, testingTypeGuard); + }).not.toThrow(); + }); + + it('invalid config', () => { + const testingTypeGuard = z.record(z.string()); + type TestingType = z.infer; + const testingConfig = { foo: 'foo', bar: 1 }; + expect(() => { + validateConfig(testingConfig, testingTypeGuard); + }).toThrow(); + }); + }); + + describe('parseJson', () => { + it('should return parsed result', () => { + const literalContent = 'foo'; + expect(parseJson(JSON.stringify(literalContent))).toEqual(literalContent); + + const objectContent = { foo: 'foo', bar: 1, baz: true, qux: [1, '2', null] }; + expect(parseJson(JSON.stringify(objectContent))).toEqual(objectContent); + }); + + it('throw error when parsing invalid Json string', () => { + expect(() => parseJson('[1,2,3,"4",]')).toThrow(); + }); + }); + + describe('parseJsonObject', () => { + it('should return parsed object', () => { + const objectContent = { foo: 'foo', bar: 1, baz: true, qux: [1, '2', null] }; + expect(parseJsonObject(JSON.stringify(objectContent))).toEqual(objectContent); + }); + + it('throw error when parsing non-object result', () => { + expect(() => parseJsonObject(JSON.stringify('foo'))).toThrow(); + }); + }); +}); diff --git a/packages/toolkit/connector-kit/tsconfig.build.json b/packages/toolkit/connector-kit/tsconfig.build.json new file mode 100644 index 000000000..665256ab7 --- /dev/null +++ b/packages/toolkit/connector-kit/tsconfig.build.json @@ -0,0 +1,5 @@ +{ + "extends": "./tsconfig", + "include": ["src"], + "exclude": ["src/**/*.test.ts"] +} diff --git a/packages/toolkit/connector-kit/tsconfig.test.json b/packages/toolkit/connector-kit/tsconfig.test.json new file mode 100644 index 000000000..55de18c33 --- /dev/null +++ b/packages/toolkit/connector-kit/tsconfig.test.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig", + "compilerOptions": { + "isolatedModules": false, + "allowJs": true + }, + "include": ["src"] +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index abdd573ce..d0b63539e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3681,18 +3681,27 @@ importers: specifier: ^3.20.2 version: 3.20.2 devDependencies: + '@jest/types': + specifier: ^29.0.3 + version: 29.1.2 '@silverhand/eslint-config': specifier: 3.0.1 version: 3.0.1(eslint@8.34.0)(prettier@2.8.4)(typescript@5.0.2) '@silverhand/ts-config': specifier: 3.0.0 version: 3.0.0(typescript@5.0.2) + '@types/jest': + specifier: ^29.4.0 + version: 29.4.0 '@types/node': specifier: ^18.11.18 version: 18.11.18 eslint: specifier: ^8.34.0 version: 8.34.0 + jest: + specifier: ^29.5.0 + version: 29.5.0(@types/node@18.11.18) lint-staged: specifier: ^13.0.0 version: 13.0.0 @@ -12564,7 +12573,7 @@ packages: resolution: {integrity: sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==} engines: {node: '>=12'} dependencies: - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jsonfile: 6.1.0 universalify: 2.0.0 dev: true @@ -12876,10 +12885,6 @@ packages: resolution: {integrity: sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==} dev: true - /graceful-fs@4.2.9: - resolution: {integrity: sha512-NtNxqUcXgpW2iMrfqSfR73Glt39K+BLwWsPs94yR63v45T0Wbej7eRmL5cWfwEgqXnmjQp3zaJTshdRW/qC2ZQ==} - dev: true - /grapheme-splitter@1.0.4: resolution: {integrity: sha512-bzh50DW9kTPM00T8y4o8vQg89Di9oLJVLW/KaOGIXJWP/iqCN6WKYkbNOF04vFLJhwcpYUh9ydh/+5vpOqV4YQ==} dev: true