Skip to content

Commit

Permalink
test: use mock from node:test instead of jest mock
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 12, 2024
1 parent 61e4248 commit 4d49bcc
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 25 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"eslint": "^8.57.0",
"eslint-plugin-mocha": "^10.4.1",
"globby": "^11.1.0",
"jest-mock": "^28.1.3",
"kleur": "^4.1.5",
"latest-version": "^5.1.0",
"mocha": "^9.2.2",
Expand Down
14 changes: 7 additions & 7 deletions packages/core/tests/command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { expect, use } from 'chai'
import shape from 'chai-shape'
import promise from 'chai-as-promised'
import mock from '@koishijs/plugin-mock'
import * as jest from 'jest-mock'
import { mock as jest } from 'node:test'

use(shape)
use(promise)
Expand Down Expand Up @@ -175,8 +175,8 @@ describe('Command API', () => {
let command: Command
beforeEach(() => {
command = app.command('test')
print.mockClear()
next.mockClear()
print.mock.resetCalls()
next.mock.resetCalls()
})
afterEach(() => command?.dispose())

Expand All @@ -195,7 +195,7 @@ describe('Command API', () => {
})

it('compose 1 (return in next function)', async () => {
next.mockResolvedValueOnce('result')
next.mock.mockImplementationOnce(() => Promise.resolve('result'))
command.action(({ next }) => next())

await expect(command.execute({ session }, next)).eventually.to.equal('result')
Expand Down Expand Up @@ -243,7 +243,7 @@ describe('Command API', () => {

await expect(command.execute({ session }, next)).eventually.to.equal('乌拉!')
expect(print.mock.calls).to.have.length(1)
expect(print.mock.calls[0][0]).to.match(/Error: message 1/)
expect(print.mock.calls[0].arguments[0]).to.match(/Error: message 1/)
expect(next.mock.calls).to.have.length(0)
})

Expand All @@ -256,12 +256,12 @@ describe('Command API', () => {

await expect(command.execute({ session }, next)).eventually.to.equal('发生未知错误。')
expect(print.mock.calls).to.have.length(1)
expect(print.mock.calls[0][0]).to.match(/Error: message 2/)
expect(print.mock.calls[0].arguments[0]).to.match(/Error: message 2/)
expect(next.mock.calls).to.have.length(1)
})

it('throw 3 (error in next function)', async () => {
next.mockRejectedValueOnce(new Error('message 3'))
next.mock.mockImplementationOnce(() => Promise.reject(new Error('message 3')))
command.action(({ next }) => next())

await expect(command.execute({ session }, next)).to.be.rejected
Expand Down
6 changes: 3 additions & 3 deletions packages/core/tests/middleware.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { App, SessionError, Middleware, sleep, noop, Logger, Next } from 'koishi'
import { expect } from 'chai'
import mock from '@koishijs/plugin-mock'
import * as jest from 'jest-mock'
import { mock as jest, Mock } from 'node:test'

type NextCallback = Extract<Next.Callback, (...args: any[]) => any>

Expand All @@ -27,10 +27,10 @@ after(() => {
})

describe('Middleware Runtime', () => {
let callSequence: jest.Mock[]
let callSequence: Mock<() => void>[]

beforeEach(() => {
print.mockClear()
print.mock.resetCalls()
app.$processor._hooks = []
callSequence = []
})
Expand Down
6 changes: 3 additions & 3 deletions packages/loader/tests/loader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai'
import { Context, sleep } from 'koishi'
import * as jest from 'jest-mock'
import { Mock } from 'node:test'
import mock from '@koishijs/plugin-mock'
import Loader from './utils'

Expand Down Expand Up @@ -90,8 +90,8 @@ describe('@koishijs/loader', () => {
app.plugin(mock)
expect(app.lifecycle._hooks['test/bar']).to.have.length(1)
expect(app.lifecycle._hooks['test/baz']).to.have.length(1)
const bar = app.lifecycle._hooks['test/bar'][0][1] as jest.Mock
const baz = app.lifecycle._hooks['test/baz'][0][1] as jest.Mock
const bar = app.lifecycle._hooks['test/bar'][0][1] as Mock<() => void>
const baz = app.lifecycle._hooks['test/baz'][0][1] as Mock<() => void>
expect(bar.mock.calls).to.have.length(0)
expect(baz.mock.calls).to.have.length(0)

Expand Down
2 changes: 1 addition & 1 deletion packages/loader/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context, Dict, Plugin } from 'koishi'
import { Loader } from '../src'
import * as jest from 'jest-mock'
import { mock as jest } from 'node:test'

export default class TestLoader extends Loader {
// @ts-ignore
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 * as jest from 'jest-mock'
import { mock as jest } from 'node:test'

describe('Observer API', () => {
it('type checks', () => {
Expand Down
16 changes: 8 additions & 8 deletions plugins/common/broadcast/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { App, Bot, Channel } from 'koishi'
import * as broadcast from '@koishijs/plugin-broadcast'
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 } from 'chai'

const app = new App({
Expand Down Expand Up @@ -35,18 +35,18 @@ describe('@koishijs/plugin-broadcast', () => {

await client.shouldNotReply('broadcast foo')
expect(send.mock.calls).to.have.length(2)
expect(send.mock.calls[0][0]).to.equal('222')
expect(send.mock.calls[1][0]).to.equal('111')
send.mockClear()
expect(send.mock.calls[0].arguments[0]).to.equal('222')
expect(send.mock.calls[1].arguments[0]).to.equal('111')
send.mock.resetCalls()

await client.shouldNotReply('broadcast -o foo')
expect(send.mock.calls).to.have.length(1)
expect(send.mock.calls[0][0]).to.equal('222')
send.mockClear()
expect(send.mock.calls[0].arguments[0]).to.equal('222')
send.mock.resetCalls()

await client.shouldNotReply('broadcast -of foo')
expect(send.mock.calls).to.have.length(2)
expect(send.mock.calls[0][0]).to.equal('222')
expect(send.mock.calls[1][0]).to.equal('333')
expect(send.mock.calls[0].arguments[0]).to.equal('222')
expect(send.mock.calls[1].arguments[0]).to.equal('333')
})
})
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, Bot, h } from 'koishi'
import * as echo from '@koishijs/plugin-echo'
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

0 comments on commit 4d49bcc

Please sign in to comment.