mirror of
https://github.com/logto-io/logto.git
synced 2025-03-31 22:51:25 -05:00
test(core): add ut for app (#227)
* test(core): add ut for app add ut for app * fix(jest): remove jest setup remove jest setup * fix(ut): mock env viriables mock env viriables * refactor(ut): add env variable test utils add env variable test util * fix(core): fix rebase issue fix rebase issue * fix(ut): update app ut update app ut * fix(core): remove supertest dependency remove supertest * fix(ut): cr fix cr fix
This commit is contained in:
parent
108705076f
commit
af89e9ac30
3 changed files with 70 additions and 0 deletions
packages/core
|
@ -10,6 +10,7 @@ module.exports = {
|
|||
},
|
||||
moduleNameMapper: {
|
||||
'^@/(.*)$': '<rootDir>/src/$1',
|
||||
'^jose/(.*)$': '<rootDir>/node_modules/jose/dist/node/cjs/$1',
|
||||
},
|
||||
coveragePathIgnorePatterns: ['/node_modules/', '/build/'],
|
||||
coverageReporters: ['text-summary', 'lcov'],
|
||||
|
|
57
packages/core/src/app/init.test.ts
Normal file
57
packages/core/src/app/init.test.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
import Koa from 'koa';
|
||||
|
||||
/**
|
||||
* Need to mock env variables ahead
|
||||
*/
|
||||
|
||||
// eslint-disable-next-line import/order
|
||||
import { envVariablesSetUp } from '@/utils/test-utils';
|
||||
|
||||
envVariablesSetUp();
|
||||
|
||||
/* eslint-disable import/first */
|
||||
import * as koaErrorHandler from '@/middleware/koa-error-handler';
|
||||
import * as koaI18next from '@/middleware/koa-i18next';
|
||||
import * as koaOIDCErrorHandler from '@/middleware/koa-oidc-error-handler';
|
||||
import * as koaSlonikErrorHandler from '@/middleware/koa-slonik-error-handler';
|
||||
import * as koaUIProxy from '@/middleware/koa-ui-proxy';
|
||||
import * as koaUserLog from '@/middleware/koa-user-log';
|
||||
import * as initOidc from '@/oidc/init';
|
||||
import * as initRouter from '@/routes/init';
|
||||
|
||||
import initI18n from '../i18n/init';
|
||||
import initApp from './init';
|
||||
/* eslint-enable import/first */
|
||||
|
||||
describe('App Init', () => {
|
||||
const listenMock = jest.spyOn(Koa.prototype, 'listen').mockImplementation(jest.fn());
|
||||
|
||||
const middlewareList = [
|
||||
koaErrorHandler,
|
||||
koaI18next,
|
||||
koaOIDCErrorHandler,
|
||||
koaSlonikErrorHandler,
|
||||
koaUIProxy,
|
||||
koaUserLog,
|
||||
];
|
||||
const initMethods = [initRouter, initOidc];
|
||||
|
||||
const middlewareSpys = middlewareList.map((module) => jest.spyOn(module, 'default'));
|
||||
const initMethodSpys = initMethods.map((module) => jest.spyOn(module, 'default'));
|
||||
|
||||
it('app init properly with 404 not found route', async () => {
|
||||
const app = new Koa();
|
||||
await initI18n();
|
||||
await initApp(app);
|
||||
|
||||
for (const middleware of middlewareSpys) {
|
||||
expect(middleware).toBeCalled();
|
||||
}
|
||||
|
||||
for (const inits of initMethodSpys) {
|
||||
expect(inits).toBeCalled();
|
||||
}
|
||||
|
||||
expect(listenMock).toBeCalled();
|
||||
});
|
||||
});
|
|
@ -21,3 +21,15 @@ export const createTestPool = <T extends QueryResultRowType>(
|
|||
);
|
||||
},
|
||||
});
|
||||
|
||||
export const envVariablesSetUp = () => {
|
||||
const OIDC_PROVIDER_PRIVATE_KEY_BASE64 =
|
||||
'LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlDV2dJQkFBS0JnR3pLendQcVp6Q3dncjR5a0U1NTN2aWw3QTZYM2l1VnJ3TVJtbVJDTVNBL3lkUm04bXA1CjlHZUYyMlRCSVBtUEVNM29Lbnk4KytFL2FDRnByWXVDa0loREhodVR5N1diT25nd3kyb3JpYnNEQm1OS3FybTkKM0xkYWYrZm1aU2tsL0FMUjZNeUhNV2dTUkQrbFhxVnplNFdSRGIzVTlrTyt3RmVXUlNZNmlRL2pBZ01CQUFFQwpnWUJOZkczUjVpUTFJNk1iZ0x3VGlPM3N2NURRSEE3YmtETWt4bWJtdmRacmw4TlRDemZoNnBiUEhTSFVNMUlmCkxXelVtMldYanFzQUZiOCsvUnZrWDh3OHI3SENNUUdLVGs0ay9adkZ5YUhkM2tIUXhjSkJPakNOUUtjS2NZalUKRGdnTUVJeW5PblNZNjJpWEV6RExKVTJEMVUrY3JEbTZXUTVHaG1NS1p2Vnl3UUpCQU1lcFBFV2gwakNDOEdmQwpQQU1yT1JvOHJYeHYwVEdXNlJWYmxad0ppdjhNeGZacnpZT1cwZUFPek9IK0ZRWE90SjNTdUZONzdEcVQ5TDI3CmN2M3QySkVDUVFDTGZZeVl2ZUg0UnY2bnVET0RnckkzRUJHMFNJbURHcC94UUV2NEk5Z0hrRFF0aFF4bW5xNTEKZ1QxajhFN1lmRHEwMTkvN2htL3dmMXNzMERQNkpic3pBa0JqOEUzKy9MVGRHMjJDUWpNUDB2N09KemtmWkVqdAo3WC9WOVBXNkdQeStGWUt4aWR4ZzFZbFFBWmlFTms0SGppUFNLN3VmN2hPY2JwcStyYWt0ZVhSQkFrQmhaaFFECkh5c20wbVBFTnNGNWhZdnRHTUpUOFFaYnpmNTZWUnYyc3dpSUYyL25qT3hneDFJbjZFczJlamlEdnhLNjdiV1AKQ29zbEViaFhMVFh0NStTekFrQjJQOUYzNExubE9tVjh4Zjk1VmVlcXNPbDFmWWx2Uy9vUUx1a2ZxVkJsTmtzNgpzdmNLVDJOQjlzSHlCeE8vY3Zqa0ZpWXdHR2MzNjlmQklkcDU1S2IwCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0t';
|
||||
const UI_SIGN_IN_ROUTE = '/sign-in';
|
||||
|
||||
process.env = {
|
||||
...process.env,
|
||||
OIDC_PROVIDER_PRIVATE_KEY_BASE64,
|
||||
UI_SIGN_IN_ROUTE,
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue