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

refactor(core): fix test

This commit is contained in:
Gao Sun 2023-01-07 21:06:17 +08:00
parent 8d829b8f3d
commit de15b03c68
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
2 changed files with 40 additions and 25 deletions

View file

@ -1,27 +1,7 @@
import { createMockUtils, pickDefault } from '@logto/shared/esm';
import { pickDefault } from '@logto/shared/esm';
import Koa from 'koa';
import { emptyMiddleware } from '#src/utils/test-utils.js';
const { jest } = import.meta;
const { mockEsm } = createMockUtils(jest);
const middlewareList = [
'error-handler',
'i18next',
'audit-log',
'oidc-error-handler',
'slonik-error-handler',
'spa-proxy',
].map((name) => {
const mock = jest.fn(() => emptyMiddleware);
mockEsm(`#src/middleware/koa-${name}.js`, () => ({
default: mock,
...(name === 'audit-log' && { LogEntry: jest.fn() }),
}));
return mock;
});
const initI18n = await pickDefault(import('../i18n/init.js'));
const initApp = await pickDefault(import('./init.js'));
@ -34,10 +14,6 @@ describe('App Init', () => {
await initI18n();
await initApp(app);
for (const middleware of middlewareList) {
expect(middleware).toBeCalled();
}
expect(listenMock).toBeCalled();
});
});

View file

@ -0,0 +1,39 @@
import { createMockUtils, pickDefault } from '@logto/shared/esm';
import { createMockProvider } from '#src/test-utils/oidc-provider.js';
import { emptyMiddleware } from '#src/utils/test-utils.js';
const { jest } = import.meta;
const { mockEsm, mockEsmDefault } = createMockUtils(jest);
const middlewareList = [
'error-handler',
'i18next',
'audit-log',
'oidc-error-handler',
'slonik-error-handler',
'spa-proxy',
].map((name) => {
const mock = jest.fn(() => emptyMiddleware);
mockEsm(`#src/middleware/koa-${name}.js`, () => ({
default: mock,
...(name === 'audit-log' && { LogEntry: jest.fn() }),
}));
return mock;
});
// eslint-disable-next-line unicorn/consistent-function-scoping
mockEsmDefault('#src/oidc/init.js', () => () => createMockProvider);
const Tenant = await pickDefault(import('./Tenant.js'));
describe('Tenant', () => {
it('should call middleware factories', () => {
const _ = new Tenant('foo');
for (const middleware of middlewareList) {
expect(middleware).toBeCalled();
}
});
});