Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
raju-opti committed Nov 15, 2024
1 parent 20771dc commit e92f027
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
34 changes: 30 additions & 4 deletions lib/service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
*/

import { it, expect } from 'vitest';
import { BaseService, ServiceState } from './service';

import { BaseService, ServiceState, StartupLog } from './service';
import { LogLevel } from './modules/logging';
import { getMockLogger } from './tests/mock/mock_logger';
class TestService extends BaseService {
constructor() {
super();
constructor(startUpLogs?: StartupLog[]) {
super(startUpLogs);
}

start(): void {
super.start();
this.setState(ServiceState.Running);
this.startPromise.resolve();
}
Expand Down Expand Up @@ -64,6 +66,30 @@ it('should return correct state when getState() is called', () => {
expect(service.getState()).toBe(ServiceState.Failed);
});

it('should log startupLogs on start', () => {
const startUpLogs: StartupLog[] = [
{
level: LogLevel.WARNING,
message: 'warn message',
params: [1, 2]
},
{
level: LogLevel.ERROR,
message: 'error message',
params: [3, 4]
},
];

const logger = getMockLogger();
const service = new TestService(startUpLogs);
service.setLogger(logger);
service.start();

expect(logger.log).toHaveBeenCalledTimes(2);
expect(logger.log).toHaveBeenNthCalledWith(1, LogLevel.WARNING, 'warn message', 1, 2);
expect(logger.log).toHaveBeenNthCalledWith(2, LogLevel.ERROR, 'error message', 3, 4);
});

it('should return an appropraite promise when onRunning() is called', () => {
const service1 = new TestService();
const onRunning1 = service1.onRunning();
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default defineConfig({
test: {
onConsoleLog: () => true,
environment: 'happy-dom',
include: ['**/cache.spec.ts'],
include: ['**/service.spec.ts'],
typecheck: {
tsconfig: 'tsconfig.spec.json',
},
Expand Down

0 comments on commit e92f027

Please sign in to comment.