Skip to content

Commit

Permalink
feat: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
nailiable committed Nov 14, 2024
1 parent 43403ea commit b544d6e
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/backend/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import swc from 'unplugin-swc'
import { defineProject } from 'vitest/config'
import { defineProject, Plugin } from 'vitest/config'

export default defineProject({
test: {
Expand All @@ -19,6 +19,6 @@ export default defineProject({
decoratorMetadata: true,
},
},
}),
}) as Plugin,
],
})
7 changes: 1 addition & 6 deletions packages/schedule/src/decorators/cron.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { CronJob, CronJobParams } from 'cron'
import { CronJobParams } from 'cron'
import 'reflect-metadata'

export type CronTime = CronJobParams['cronTime']
export const CronWatermark = '__naily_schedule_cron__'

CronJob.from({
cronTime: '',
onTick: () => {},
})

export interface BaseCronOptions {
unrefTimeout?: boolean
scheduleName?: string
Expand Down
1 change: 1 addition & 0 deletions packages/schedule/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './cron-expression'
export * from './decorators'
export * from './plugin'
export * from './scheduler.registry'
export * from './scheduler-executor'
export * from './scheduler-scanner'
Expand Down
4 changes: 2 additions & 2 deletions packages/schedule/src/scheduler-scanner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ClassWrapper } from '@nailyjs/ioc'
import { CronMetadata, CronWatermark, IntervalMetadata, TimeoutMetadata, TimeoutWatermark } from './decorators'
import { CronMetadata, CronWatermark, IntervalMetadata, IntervalWatermark, TimeoutMetadata, TimeoutWatermark } from './decorators'
import { IntervalMetadataWrapper } from './wrappers'
import { CronMetadataWrapper } from './wrappers/cron-metadata-wrapper'
import { TimeoutMetadataWrapper } from './wrappers/timeout-metadata-wrapper'
Expand Down Expand Up @@ -32,7 +32,7 @@ export class SchedulerScanner {
}

getIntervalTasks(): IntervalMetadataWrapper[] {
const tasks: IntervalMetadata[] = this.wrapper.getMetadata(TimeoutWatermark) || []
const tasks: IntervalMetadata[] = this.wrapper.getMetadata(IntervalWatermark) || []
return tasks.map(task => new IntervalMetadataWrapper(this, task))
}

Expand Down
26 changes: 26 additions & 0 deletions packages/schedule/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { AbstractBootstrap, Injectable } from '@nailyjs/ioc'
import { Interval, SchedulePlugin, Timeout } from '../src'

describe('schedule test', () => {
it('should work', async () => {
@Injectable()
class _TestService {
@Interval(1000)
async test() {
console.log('test, interval')
}

@Timeout(10)
async test2() {
console.log('test2, timeout')
}
}

class Bootstrap extends AbstractBootstrap {
async run(): Promise<any> {
await this.getPluginRunner().runBeforeRun()
}
}
await new Bootstrap().use(SchedulePlugin()).run()
})
})
4 changes: 3 additions & 1 deletion packages/schedule/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"compilerOptions": {
"target": "ES2022",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "ES2022",
"moduleResolution": "Bundler"
},
"include": ["src"]
"include": ["src", "test", "../../shims.d.ts"]
}
24 changes: 24 additions & 0 deletions packages/schedule/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import swc from 'unplugin-swc'
import { defineProject, Plugin } from 'vitest/config'

export default defineProject({
test: {
globals: true,
},

plugins: [
swc.vite({
jsc: {
parser: {
syntax: 'typescript',
decorators: true,
tsx: true,
},
transform: {
legacyDecorator: true,
decoratorMetadata: true,
},
},
}) as Plugin,
],
})

0 comments on commit b544d6e

Please sign in to comment.