Skip to content

Commit

Permalink
test: replace jest-mock with node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 12, 2024
1 parent 7d0c00c commit a1ec80a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 30 deletions.
6 changes: 3 additions & 3 deletions packages/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 '../src'
import mock from '@koishijs/plugin-mock'
import * as jest from 'jest-mock'
import { mock as jest } from 'node:test'
import { expect, use } from 'chai'
import shape from 'chai-shape'

Expand All @@ -28,14 +28,14 @@ describe('koishi-plugin-feedback', () => {
expect(send1.mock.calls).to.have.length(0)
await client1.shouldReply('feedback foo', '反馈信息发送成功!')
expect(send1.mock.calls).to.have.length(1)
expect(send1.mock.calls).to.have.shape([['private:999', '收到来自 123 的反馈信息:\nfoo']])
expect(send1.mock.calls[0].arguments).to.have.shape(['private:999', '收到来自 123 的反馈信息:\nfoo'])

const send2 = app.bots[0].sendMessage = jest.fn(async () => ['2000'])
await client1.shouldNotReply('bar')
expect(send2.mock.calls).to.have.length(0)
await client1.shouldNotReply(`<quote id="1000"/> bar`)
expect(send2.mock.calls).to.have.length(1)
expect(send2.mock.calls).to.have.shape([['private:123', 'bar']])
expect(send2.mock.calls[0].arguments).to.have.shape(['private:123', 'bar'])

await client2.shouldReply('feedback -R', '反馈频道更新成功!')
await client2.shouldReply('feedback -R', '反馈频道没有改动。')
Expand Down
20 changes: 10 additions & 10 deletions packages/forward/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { App } from 'koishi'
import { expect, use } from 'chai'
import shape from 'chai-shape'
import * as jest from 'jest-mock'
import { mock as jest } from 'node:test'
import * as help from '@koishijs/plugin-help'
import memory from '@koishijs/plugin-database-memory'
import mock, { DEFAULT_SELF_ID } from '@koishijs/plugin-mock'
Expand Down Expand Up @@ -35,23 +35,23 @@ after(() => app.stop())

describe('koishi-plugin-forward', () => {
it('basic support', async () => {
send.mockClear()
send.mock.resetCalls()
await client2.shouldNotReply('hello')
expect(send.mock.calls).to.have.length(1)
expect(send.mock.calls).to.have.shape([['654', '123: hello']])
expect(send.mock.calls[0].arguments).to.have.shape(['654', '123: hello'])

send.mockClear()
send.mock.resetCalls()
await client3.shouldNotReply('hello')
expect(send.mock.calls).to.have.length(0)
await client3.shouldNotReply('<quote id="2000"/> hello')
expect(send.mock.calls).to.have.length(1)
expect(send.mock.calls).to.have.shape([['456', '789: hello']])
expect(send.mock.calls[0].arguments).to.have.shape(['456', '789: hello'])

send.mockClear()
send.mockImplementation(async () => ['3000'])
send.mock.resetCalls()
send.mock.mockImplementation(async () => ['3000'])
await client2.shouldNotReply('<quote id="3000"/> hello')
expect(send.mock.calls).to.have.length(1)
expect(send.mock.calls).to.have.shape([['654', '123: hello']])
expect(send.mock.calls[0].arguments).to.have.shape(['654', '123: hello'])
})

it('command usage', async () => {
Expand All @@ -66,10 +66,10 @@ describe('koishi-plugin-forward', () => {
await client2.shouldReply('forward add #654', '已成功添加目标频道 mock:654。')
await client2.shouldReply('forward ls', '当前频道的目标频道列表为:\nmock:654')

send.mockClear()
send.mock.resetCalls()
await client2.shouldNotReply('hello <at id="321"/>')
expect(send.mock.calls).to.have.length(1)
expect(send.mock.calls).to.have.shape([['654', '123: hello @foo']])
expect(send.mock.calls[0].arguments).to.have.shape(['654', '123: hello @foo'])

await client2.shouldReply('forward rm #654', '已成功移除目标频道 mock:654。')
await client2.shouldReply('forward ls', '当前频道没有设置目标频道。')
Expand Down
11 changes: 7 additions & 4 deletions packages/recall/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { App } from 'koishi'
import { expect, use } from 'chai'
import mock from '@koishijs/plugin-mock'
import * as jest from 'jest-mock'
import { mock as jest } from 'node:test'
import * as recall from '../src'
import shape from 'chai-shape'

Expand Down Expand Up @@ -29,7 +29,8 @@ describe('koishi-plugin-recall', () => {
user: { id: '123' },
})
await client.shouldNotReply('recall')
expect(del.mock.calls).to.have.shape([[client.event.channel?.id, '1234']])
expect(del.mock.calls).to.have.length(1)
expect(del.mock.calls[0].arguments).to.have.shape([client.event.channel?.id, '1234'])
})

it('reply', async () => {
Expand All @@ -41,7 +42,9 @@ describe('koishi-plugin-recall', () => {
guild: { id: '1919' },
user: { id: '123' },
})
await client.shouldNotReply('<quote id="114"/>recall')
expect(del.mock.calls).to.have.shape([[client.event.channel?.id, '114']])
// https://github.com/koishijs/common/pull/15
await client.shouldNotReply('<quote id="114">foo</quote>recall')
expect(del.mock.calls).to.have.length(1)
expect(del.mock.calls[0].arguments).to.have.shape([client.event.channel?.id, '114'])
})
})
4 changes: 2 additions & 2 deletions packages/schedule/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { install, InstalledClock } from '@sinonjs/fake-timers'
import * as schedule from '../src'
import memory from '@koishijs/plugin-database-memory'
import mock from '@koishijs/plugin-mock'
import * as jest from 'jest-mock'
import { mock as jest } from 'node:test'
import { expect, use } from 'chai'
import shape from 'chai-shape'

Expand Down Expand Up @@ -99,7 +99,7 @@ describe('koishi-plugin-schedule', () => {
it('database integration', async () => {
await clock.tickAsync(Time.day) // 02:31
expect(send.mock.calls).to.have.length(1)
expect(send.mock.calls[0]).to.shape([client2.event.channel?.id, 'bar'])
expect(send.mock.calls[0].arguments).to.shape([client2.event.channel?.id, 'bar'])
})

it('check arguments', async () => {
Expand Down
1 change: 0 additions & 1 deletion packages/spawn/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ ctx.plugin(spawn)
const client = ctx.mock.client('123')

describe('koishi-plugin-spawn', () => {
before(() => ctx.start())
after(() => ctx.stop())

it('basic support', async () => {
Expand Down
29 changes: 19 additions & 10 deletions packages/verifier/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect, use } from 'chai'
import { App, sleep, Universal } from 'koishi'
import shape from 'chai-shape'
import mock, { DEFAULT_SELF_ID } from '@koishijs/plugin-mock'
import * as jest from 'jest-mock'
import { mock as jest } from 'node:test'
import * as verifier from '../src'

use(shape)
Expand Down Expand Up @@ -71,13 +71,16 @@ describe('koishi-plugin-verifier', () => {
})

await receiveFriendRequest(instance.app, '321')
expect(instance.handleFriendRequest.mock.calls).to.have.shape([['flag', true, 'foo']])
expect(instance.handleFriendRequest.mock.calls).to.have.length(1)
expect(instance.handleFriendRequest.mock.calls[0].arguments).to.have.shape(['flag', true, 'foo'])

await receiveGroupRequest(instance.app, '321')
expect(instance.handleGuildRequest.mock.calls).to.have.shape([['flag', false, 'baz']])
expect(instance.handleGuildRequest.mock.calls).to.have.length(1)
expect(instance.handleGuildRequest.mock.calls[0].arguments).to.have.shape(['flag', false, 'baz'])

await receiveGroupMemberRequest(instance.app, '321')
expect(instance.handleGuildMemberRequest.mock.calls).to.have.shape([['flag', false, 'bar']])
expect(instance.handleGuildMemberRequest.mock.calls).to.have.length(1)
expect(instance.handleGuildMemberRequest.mock.calls[0].arguments).to.have.shape(['flag', false, 'bar'])
})

it('request handler: boolean', async () => {
Expand All @@ -88,13 +91,16 @@ describe('koishi-plugin-verifier', () => {
})

await receiveFriendRequest(instance.app, '321')
expect(instance.handleFriendRequest.mock.calls).to.have.shape([['flag', false]])
expect(instance.handleFriendRequest.mock.calls).to.have.length(1)
expect(instance.handleFriendRequest.mock.calls[0].arguments).to.have.shape(['flag', false])

await receiveGroupRequest(instance.app, '321')
expect(instance.handleGuildRequest.mock.calls).to.have.shape([['flag', false]])
expect(instance.handleGuildRequest.mock.calls).to.have.length(1)
expect(instance.handleGuildRequest.mock.calls[0].arguments).to.have.shape(['flag', false])

await receiveGroupMemberRequest(instance.app, '321')
expect(instance.handleGuildMemberRequest.mock.calls).to.have.shape([['flag', false]])
expect(instance.handleGuildMemberRequest.mock.calls).to.have.length(1)
expect(instance.handleGuildMemberRequest.mock.calls[0].arguments).to.have.shape(['flag', false])
})

it('request handler: function', async () => {
Expand All @@ -105,12 +111,15 @@ describe('koishi-plugin-verifier', () => {
})

await receiveFriendRequest(instance.app, '321')
expect(instance.handleFriendRequest.mock.calls).to.have.shape([['flag', true]])
expect(instance.handleFriendRequest.mock.calls).to.have.length(1)
expect(instance.handleFriendRequest.mock.calls[0].arguments).to.have.shape(['flag', true])

await receiveGroupRequest(instance.app, '321')
expect(instance.handleGuildRequest.mock.calls).to.have.shape([['flag', true]])
expect(instance.handleGuildRequest.mock.calls).to.have.length(1)
expect(instance.handleGuildRequest.mock.calls[0].arguments).to.have.shape(['flag', true])

await receiveGroupMemberRequest(instance.app, '321')
expect(instance.handleGuildMemberRequest.mock.calls).to.have.shape([['flag', true]])
expect(instance.handleGuildMemberRequest.mock.calls).to.have.length(1)
expect(instance.handleGuildMemberRequest.mock.calls[0].arguments).to.have.shape(['flag', true])
})
})

0 comments on commit a1ec80a

Please sign in to comment.