0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

chore(toolkit): add UTs for connector-kit (#3760)

This commit is contained in:
Darcy Ye 2023-04-28 10:50:05 +08:00 committed by GitHub
parent db4bc9e3ba
commit 6a463f6a2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 94 additions and 6 deletions

View file

@ -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)$': '<rootDir>/../../shared/lib/esm/module-proxy.js',
},
};
export default config;

View file

@ -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",

View file

@ -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<typeof testingTypeGuard>;
const testingConfig = { foo: 'foo', bar: 1, baz: true };
expect(() => {
validateConfig<TestingType>(testingConfig, testingTypeGuard);
}).not.toThrow();
});
it('invalid config', () => {
const testingTypeGuard = z.record(z.string());
type TestingType = z.infer<typeof testingTypeGuard>;
const testingConfig = { foo: 'foo', bar: 1 };
expect(() => {
validateConfig<TestingType>(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();
});
});
});

View file

@ -0,0 +1,5 @@
{
"extends": "./tsconfig",
"include": ["src"],
"exclude": ["src/**/*.test.ts"]
}

View file

@ -0,0 +1,8 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"isolatedModules": false,
"allowJs": true
},
"include": ["src"]
}

View file

@ -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