Skip to content

Commit

Permalink
test: fix test imports
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 27, 2022
1 parent e7687d9 commit f9e2748
Show file tree
Hide file tree
Showing 27 changed files with 76 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [14, 16, 18]
node-version: [12, 14, 16, 18]

steps:
- name: Check out
Expand Down
3 changes: 1 addition & 2 deletions docs/guide/misc/unit-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ it('example 1', async () => {
```ts no-extra-header
import { App } from 'koishi'
import mock from '@koishijs/plugin-mock'
import memory from '@koishijs/plugin-database-memory'

const app = new App()
app.plugin(mock)
app.plugin(memory)
app.plugin('database-memory')

// 这次我们来测试一下这个指令
app.command('foo', { authority: 2 }).action(() => 'bar')
Expand Down
13 changes: 11 additions & 2 deletions packages/core/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,21 @@ export class Command<U extends User.Field = never, G extends Channel.Field = nev
return this
}

option<K extends string>(name: K, config: Argv.TypedOptionConfig<RegExp>): Command<U, G, A, Extend<O, K, string>>
option<K extends string, R>(name: K, config: Argv.TypedOptionConfig<(source: string) => R>): Command<U, G, A, Extend<O, K, R>>
option<K extends string, R extends string>(name: K, config: Argv.TypedOptionConfig<R[]>): Command<U, G, A, Extend<O, K, R>>
option<K extends string>(name: K, config?: Argv.OptionConfig): Command<U, G, A, Extend<O, K, any>>
option<K extends string>(name: K, desc: string, config: Argv.TypedOptionConfig<RegExp>): Command<U, G, A, Extend<O, K, string>>
option<K extends string, R>(name: K, desc: string, config: Argv.TypedOptionConfig<(source: string) => R>): Command<U, G, A, Extend<O, K, R>>
option<K extends string, R extends string>(name: K, desc: string, config: Argv.TypedOptionConfig<R[]>): Command<U, G, A, Extend<O, K, R>>
option<K extends string, D extends string>(name: K, desc: D, config?: Argv.OptionConfig): Command<U, G, A, Extend<O, K, Argv.OptionType<D>>>
option(name: string, desc: string, config: Argv.OptionConfig = {}) {
this._createOption(name, desc, config)
option(name: string, ...args: [Argv.OptionConfig?] | [string, Argv.OptionConfig?]) {
let desc = ''
if (typeof args[0] === 'string') {
desc = args.shift() as string
}
const config = args[0] as Argv.OptionConfig
this._createOption(name, desc, config || {})
this._disposables?.push(() => this.removeOption(name))
return this
}
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ export * from './database'
export * from './parser'
export * from './session'
export * from './internal'

const version: string = require('../package.json').version
export { version }
10 changes: 7 additions & 3 deletions packages/core/tests/command.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { App, Command, Logger, Next } from 'koishi'
import { inspect } from 'util'
import { expect } from 'chai'
import {} from 'chai-shape'
import { expect, use } from 'chai'
import shape from 'chai-shape'
import promise from 'chai-as-promised'
import mock from '@koishijs/plugin-mock'
import jest from 'jest-mock'
import * as jest from 'jest-mock'

use(shape)
use(promise)

const logger = new Logger('command')

Expand Down
8 changes: 5 additions & 3 deletions packages/core/tests/context.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { App, Context, Dict, noop } from 'koishi'
import { expect } from 'chai'
import { expect, use } from 'chai'
import { inspect } from 'util'
import mock from '@koishijs/plugin-mock'
import jest from 'jest-mock'
import {} from 'chai-shape'
import * as jest from 'jest-mock'
import shape from 'chai-shape'

use(shape)

const app = new App().plugin(mock)
const guildSession = app.mock.session({ userId: '123', guildId: '456', subtype: 'group' })
Expand Down
11 changes: 7 additions & 4 deletions packages/core/tests/database.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { App } from 'koishi'
import { expect } from 'chai'
import { expect, use } from 'chai'
import mock from '@koishijs/plugin-mock'
import memory from '@koishijs/plugin-database-memory'
import 'chai-shape'
import shape from 'chai-shape'
import promise from 'chai-as-promised'

use(shape)
use(promise)

const app = new App()

app.plugin(mock)
app.plugin(memory)
app.plugin('database-memory')

before(() => app.start())
after(() => app.stop())
Expand Down
3 changes: 1 addition & 2 deletions packages/core/tests/help.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { App } from 'koishi'
import mock from '@koishijs/plugin-mock'
import memory from '@koishijs/plugin-database-memory'

const app = new App()

app.plugin(mock)
app.plugin(memory)
app.plugin('database-memory')

app.i18n.define('$zh', 'commands.help.messages.global-epilog', 'EPILOG')

Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/hook.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { App, Middleware, Context, sleep, noop, Logger, Next } from 'koishi'
import { expect } from 'chai'
import mock from '@koishijs/plugin-mock'
import jest from 'jest-mock'
import * as jest from 'jest-mock'

const app = new App().plugin(mock)

Expand Down
6 changes: 4 additions & 2 deletions packages/core/tests/parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { App, Command } from 'koishi'
import { expect } from 'chai'
import {} from 'chai-shape'
import { expect, use } from 'chai'
import shape from 'chai-shape'

use(shape)

const app = new App()

Expand Down
3 changes: 1 addition & 2 deletions packages/core/tests/runtime.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { App, User, Channel, Command, sleep } from 'koishi'
import memory from '@koishijs/plugin-database-memory'
import mock, { DEFAULT_SELF_ID } from '@koishijs/plugin-mock'

const app = new App({
minSimilarity: 0,
})

app.plugin(memory)
app.plugin('database-memory')
app.plugin(mock)

// make coverage happy
Expand Down
3 changes: 0 additions & 3 deletions packages/koishi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ declare module '@koishijs/core' {
}
}

const { version } = require('../package.json')
export { version }

App.Config.list.unshift(App.Config.Network)
App.Config.list.push(Schema.object({
request: Quester.Config,
Expand Down
6 changes: 4 additions & 2 deletions packages/segment/tests/segment.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { segment } from 'koishi'
import { expect } from 'chai'
import {} from 'chai-shape'
import { expect, use } from 'chai'
import shape from 'chai-shape'

use(shape)

describe('Segment API', () => {
it('segment.escape()', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/tests/observe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { observe, noop, Dict } from 'koishi'
import { expect } from 'chai'
import jest from 'jest-mock'
import * as jest from 'jest-mock'

describe('Observer API', () => {
it('type checks', () => {
Expand Down
8 changes: 5 additions & 3 deletions plugins/a11y/admin/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { App, User, Channel, defineEnumProperty } from 'koishi'
import { expect } from 'chai'
import { expect, use } from 'chai'
import * as admin from '@koishijs/plugin-admin'
import memory from '@koishijs/plugin-database-memory'
import mock from '@koishijs/plugin-mock'
import promise from 'chai-as-promised'

use(promise)

const app = new App()

app.plugin(memory)
app.plugin('database-memory')
app.plugin(mock)
app.plugin(admin)

Expand Down
3 changes: 1 addition & 2 deletions plugins/a11y/rate-limit/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { App, Time } from 'koishi'
import mock from '@koishijs/plugin-mock'
import memory from '@koishijs/plugin-database-memory'
import * as admin from '@koishijs/plugin-admin'
import * as rate from '@koishijs/plugin-rate-limit'
import { install } from '@sinonjs/fake-timers'
Expand All @@ -9,7 +8,7 @@ const app = new App()
let now = Date.now()

app.plugin(mock)
app.plugin(memory)
app.plugin('database-memory')
app.plugin(admin)
app.plugin(rate)

Expand Down
5 changes: 2 additions & 3 deletions plugins/a11y/schedule/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { App, Time } from 'koishi'
import { install, InstalledClock } from '@sinonjs/fake-timers'
import * as schedule from '@koishijs/plugin-schedule'
import memory from '@koishijs/plugin-database-memory'
import mock from '@koishijs/plugin-mock'
import jest from 'jest-mock'
import * as jest from 'jest-mock'
import { expect } from 'chai'
import 'chai-shape'

Expand All @@ -13,7 +12,7 @@ const client2 = app.mock.client('123')

const send = app.bots[0].sendMessage = jest.fn(async () => [])

app.plugin(memory)
app.plugin('database-memory')
app.command('echo [content:text]').action((_, text) => text)

let clock: InstalledClock
Expand Down
3 changes: 1 addition & 2 deletions plugins/a11y/sudo/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { App } from 'koishi'
import memory from '@koishijs/plugin-database-memory'
import mock from '@koishijs/plugin-mock'
import * as sudo from '@koishijs/plugin-sudo'

const app = new App()

app.plugin(memory)
app.plugin('database-memory')
app.plugin(mock)
app.plugin(sudo)

Expand Down
5 changes: 2 additions & 3 deletions plugins/a11y/switch/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { App, Context } from 'koishi'
import { App } from 'koishi'
import * as _switch from '@koishijs/plugin-switch'
import memory from '@koishijs/plugin-database-memory'
import mock from '@koishijs/plugin-mock'

const app = new App()

app.plugin(memory)
app.plugin('database-memory')
app.plugin(mock)

const client = app.mock.client('123', '321')
Expand Down
2 changes: 1 addition & 1 deletion plugins/a11y/verifier/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai'
import { App, sleep, Session } from 'koishi'
import mock, { DEFAULT_SELF_ID } from '@koishijs/plugin-mock'
import jest from 'jest-mock'
import * as jest from 'jest-mock'
import * as verifier from '@koishijs/plugin-verifier'

const app = new App().plugin(mock)
Expand Down
5 changes: 2 additions & 3 deletions plugins/common/broadcast/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { App, Channel } from 'koishi'
import * as broadcast from '@koishijs/plugin-broadcast'
import mock from '@koishijs/plugin-mock'
import memory from '@koishijs/plugin-database-memory'
import jest from 'jest-mock'
import * as jest from 'jest-mock'
import { expect } from 'chai'

const app = new App({
delay: { broadcast: 0 },
})

app.plugin(mock, { selfIds: ['514', '114'] })
app.plugin(memory)
app.plugin('database-memory')
app.plugin(broadcast)

const client = app.mock.client('123')
Expand Down
2 changes: 1 addition & 1 deletion plugins/common/echo/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { App } from 'koishi'
import * as echo from '@koishijs/plugin-echo'
import mock from '@koishijs/plugin-mock'
import jest from 'jest-mock'
import * as jest from 'jest-mock'
import { expect } from 'chai'
import 'chai-shape'

Expand Down
2 changes: 1 addition & 1 deletion plugins/common/feedback/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { App } from 'koishi'
import * as feedback from '@koishijs/plugin-feedback'
import mock from '@koishijs/plugin-mock'
import jest from 'jest-mock'
import * as jest from 'jest-mock'
import { expect } from 'chai'
import 'chai-shape'

Expand Down
11 changes: 6 additions & 5 deletions plugins/common/forward/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { App } from 'koishi'
import { expect } from 'chai'
import {} from 'chai-shape'
import jest from 'jest-mock'
import memory from '@koishijs/plugin-database-memory'
import { expect, use } from 'chai'
import shape from 'chai-shape'
import * as jest from 'jest-mock'
import mock, { DEFAULT_SELF_ID } from '@koishijs/plugin-mock'
import * as forward from '@koishijs/plugin-forward'

use(shape)

const app = new App()

app.plugin(mock)
Expand Down Expand Up @@ -45,7 +46,7 @@ describe('@koishijs/plugin-forward', () => {
})

it('command usage', async () => {
app.plugin(memory)
app.plugin('database-memory')
await app._tasks.flush()
await app.mock.initUser('123', 3)

Expand Down
2 changes: 1 addition & 1 deletion plugins/common/recall/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { App } from 'koishi'
import { expect } from 'chai'
import * as recall from '@koishijs/plugin-recall'
import mock from '@koishijs/plugin-mock'
import jest from 'jest-mock'
import * as jest from 'jest-mock'
import 'chai-shape'

const app = new App()
Expand Down
5 changes: 2 additions & 3 deletions plugins/eval/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { App } from 'koishi'
import { resolve } from 'path'
import { promises as fs } from 'fs'
import memory from '@koishijs/plugin-database-memory'
import mock from '@koishijs/plugin-mock'
import * as eval from '@koishijs/plugin-eval'
import * as teach from '@koishijs/plugin-teach'
import * as teach from 'koishi-plugin-dialogue'

const app = new App()

app.plugin(memory)
app.plugin('database-memory')
app.plugin(mock)

app.plugin(eval, {
Expand Down
6 changes: 4 additions & 2 deletions plugins/eval/tests/sandbox.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Sandbox } from '@koishijs/plugin-eval/src/worker/sandbox'
import { inspect } from 'util'
import { expect } from 'chai'
import {} from 'chai-shape'
import { expect, use } from 'chai'
import shape from 'chai-shape'

use(shape)

describe('Eval Sandbox (Frozen)', () => {
const vm = new Sandbox()
Expand Down

0 comments on commit f9e2748

Please sign in to comment.