From f9e274848f09fe33953370d05e3d408f8d40ef74 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Thu, 28 Apr 2022 02:44:11 +0800 Subject: [PATCH] test: fix test imports --- .github/workflows/test.yaml | 2 +- docs/guide/misc/unit-tests.md | 3 +-- packages/core/src/command.ts | 13 +++++++++++-- packages/core/src/index.ts | 3 +++ packages/core/tests/command.spec.ts | 10 +++++++--- packages/core/tests/context.spec.ts | 8 +++++--- packages/core/tests/database.spec.ts | 11 +++++++---- packages/core/tests/help.spec.ts | 3 +-- packages/core/tests/hook.spec.ts | 2 +- packages/core/tests/parser.spec.ts | 6 ++++-- packages/core/tests/runtime.spec.ts | 3 +-- packages/koishi/src/index.ts | 3 --- packages/segment/tests/segment.spec.ts | 6 ++++-- packages/utils/tests/observe.spec.ts | 2 +- plugins/a11y/admin/tests/index.spec.ts | 8 +++++--- plugins/a11y/rate-limit/tests/index.spec.ts | 3 +-- plugins/a11y/schedule/tests/index.spec.ts | 5 ++--- plugins/a11y/sudo/tests/index.spec.ts | 3 +-- plugins/a11y/switch/tests/index.spec.ts | 5 ++--- plugins/a11y/verifier/tests/index.spec.ts | 2 +- plugins/common/broadcast/tests/index.spec.ts | 5 ++--- plugins/common/echo/tests/index.spec.ts | 2 +- plugins/common/feedback/tests/index.spec.ts | 2 +- plugins/common/forward/tests/index.spec.ts | 11 ++++++----- plugins/common/recall/tests/index.spec.ts | 2 +- plugins/eval/tests/index.spec.ts | 5 ++--- plugins/eval/tests/sandbox.spec.ts | 6 ++++-- 27 files changed, 76 insertions(+), 58 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 7fe60b4ae8..4f11af3e1e 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -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 diff --git a/docs/guide/misc/unit-tests.md b/docs/guide/misc/unit-tests.md index 09a1182d77..e0a968bb59 100644 --- a/docs/guide/misc/unit-tests.md +++ b/docs/guide/misc/unit-tests.md @@ -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') diff --git a/packages/core/src/command.ts b/packages/core/src/command.ts index 8185d43676..1237034f3b 100644 --- a/packages/core/src/command.ts +++ b/packages/core/src/command.ts @@ -174,12 +174,21 @@ export class Command(name: K, config: Argv.TypedOptionConfig): Command> + option(name: K, config: Argv.TypedOptionConfig<(source: string) => R>): Command> + option(name: K, config: Argv.TypedOptionConfig): Command> + option(name: K, config?: Argv.OptionConfig): Command> option(name: K, desc: string, config: Argv.TypedOptionConfig): Command> option(name: K, desc: string, config: Argv.TypedOptionConfig<(source: string) => R>): Command> option(name: K, desc: string, config: Argv.TypedOptionConfig): Command> option(name: K, desc: D, config?: Argv.OptionConfig): Command>> - 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 } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 559d417823..0318d73f78 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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 } diff --git a/packages/core/tests/command.spec.ts b/packages/core/tests/command.spec.ts index 8ada36414f..0fddab0928 100644 --- a/packages/core/tests/command.spec.ts +++ b/packages/core/tests/command.spec.ts @@ -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') diff --git a/packages/core/tests/context.spec.ts b/packages/core/tests/context.spec.ts index 3e638994ea..4123411750 100644 --- a/packages/core/tests/context.spec.ts +++ b/packages/core/tests/context.spec.ts @@ -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' }) diff --git a/packages/core/tests/database.spec.ts b/packages/core/tests/database.spec.ts index 14cd34b8d4..857cefe4cd 100644 --- a/packages/core/tests/database.spec.ts +++ b/packages/core/tests/database.spec.ts @@ -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()) diff --git a/packages/core/tests/help.spec.ts b/packages/core/tests/help.spec.ts index 2d0256aef6..42da638628 100644 --- a/packages/core/tests/help.spec.ts +++ b/packages/core/tests/help.spec.ts @@ -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') diff --git a/packages/core/tests/hook.spec.ts b/packages/core/tests/hook.spec.ts index df8322ddd6..b29a0696eb 100644 --- a/packages/core/tests/hook.spec.ts +++ b/packages/core/tests/hook.spec.ts @@ -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) diff --git a/packages/core/tests/parser.spec.ts b/packages/core/tests/parser.spec.ts index f2d5efd301..950453b281 100644 --- a/packages/core/tests/parser.spec.ts +++ b/packages/core/tests/parser.spec.ts @@ -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() diff --git a/packages/core/tests/runtime.spec.ts b/packages/core/tests/runtime.spec.ts index 3cee4a03ab..a1d4ee8f55 100644 --- a/packages/core/tests/runtime.spec.ts +++ b/packages/core/tests/runtime.spec.ts @@ -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 diff --git a/packages/koishi/src/index.ts b/packages/koishi/src/index.ts index 135e444879..ab792cd94a 100644 --- a/packages/koishi/src/index.ts +++ b/packages/koishi/src/index.ts @@ -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, diff --git a/packages/segment/tests/segment.spec.ts b/packages/segment/tests/segment.spec.ts index d8c9d2ff52..b890038623 100644 --- a/packages/segment/tests/segment.spec.ts +++ b/packages/segment/tests/segment.spec.ts @@ -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()', () => { diff --git a/packages/utils/tests/observe.spec.ts b/packages/utils/tests/observe.spec.ts index 893c6f1f0e..ee13fd2725 100644 --- a/packages/utils/tests/observe.spec.ts +++ b/packages/utils/tests/observe.spec.ts @@ -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', () => { diff --git a/plugins/a11y/admin/tests/index.spec.ts b/plugins/a11y/admin/tests/index.spec.ts index 22bf04f131..453fe68320 100644 --- a/plugins/a11y/admin/tests/index.spec.ts +++ b/plugins/a11y/admin/tests/index.spec.ts @@ -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) diff --git a/plugins/a11y/rate-limit/tests/index.spec.ts b/plugins/a11y/rate-limit/tests/index.spec.ts index f77bd6a2e4..d40956abe6 100644 --- a/plugins/a11y/rate-limit/tests/index.spec.ts +++ b/plugins/a11y/rate-limit/tests/index.spec.ts @@ -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' @@ -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) diff --git a/plugins/a11y/schedule/tests/index.spec.ts b/plugins/a11y/schedule/tests/index.spec.ts index a11475bb25..e5a58f6676 100644 --- a/plugins/a11y/schedule/tests/index.spec.ts +++ b/plugins/a11y/schedule/tests/index.spec.ts @@ -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' @@ -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 diff --git a/plugins/a11y/sudo/tests/index.spec.ts b/plugins/a11y/sudo/tests/index.spec.ts index b7cbd96da9..a7e874756a 100644 --- a/plugins/a11y/sudo/tests/index.spec.ts +++ b/plugins/a11y/sudo/tests/index.spec.ts @@ -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) diff --git a/plugins/a11y/switch/tests/index.spec.ts b/plugins/a11y/switch/tests/index.spec.ts index 0fb4ed7659..9067bf4954 100644 --- a/plugins/a11y/switch/tests/index.spec.ts +++ b/plugins/a11y/switch/tests/index.spec.ts @@ -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') diff --git a/plugins/a11y/verifier/tests/index.spec.ts b/plugins/a11y/verifier/tests/index.spec.ts index f3024e57a3..4072f968bc 100644 --- a/plugins/a11y/verifier/tests/index.spec.ts +++ b/plugins/a11y/verifier/tests/index.spec.ts @@ -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) diff --git a/plugins/common/broadcast/tests/index.spec.ts b/plugins/common/broadcast/tests/index.spec.ts index cfae850e19..0bb4df0944 100644 --- a/plugins/common/broadcast/tests/index.spec.ts +++ b/plugins/common/broadcast/tests/index.spec.ts @@ -1,8 +1,7 @@ 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({ @@ -10,7 +9,7 @@ const app = new App({ }) app.plugin(mock, { selfIds: ['514', '114'] }) -app.plugin(memory) +app.plugin('database-memory') app.plugin(broadcast) const client = app.mock.client('123') diff --git a/plugins/common/echo/tests/index.spec.ts b/plugins/common/echo/tests/index.spec.ts index 3512ba6555..a811f177f6 100644 --- a/plugins/common/echo/tests/index.spec.ts +++ b/plugins/common/echo/tests/index.spec.ts @@ -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' diff --git a/plugins/common/feedback/tests/index.spec.ts b/plugins/common/feedback/tests/index.spec.ts index 4896603be4..7bf921fc44 100644 --- a/plugins/common/feedback/tests/index.spec.ts +++ b/plugins/common/feedback/tests/index.spec.ts @@ -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' diff --git a/plugins/common/forward/tests/index.spec.ts b/plugins/common/forward/tests/index.spec.ts index a1c0bf5218..6822d6048a 100644 --- a/plugins/common/forward/tests/index.spec.ts +++ b/plugins/common/forward/tests/index.spec.ts @@ -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) @@ -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) diff --git a/plugins/common/recall/tests/index.spec.ts b/plugins/common/recall/tests/index.spec.ts index e7a6bf0dd1..911397bfff 100644 --- a/plugins/common/recall/tests/index.spec.ts +++ b/plugins/common/recall/tests/index.spec.ts @@ -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() diff --git a/plugins/eval/tests/index.spec.ts b/plugins/eval/tests/index.spec.ts index 5d5b269290..1e6dce3490 100644 --- a/plugins/eval/tests/index.spec.ts +++ b/plugins/eval/tests/index.spec.ts @@ -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, { diff --git a/plugins/eval/tests/sandbox.spec.ts b/plugins/eval/tests/sandbox.spec.ts index ce3f2dfa06..25239e3702 100644 --- a/plugins/eval/tests/sandbox.spec.ts +++ b/plugins/eval/tests/sandbox.spec.ts @@ -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()