From 4d49bcc2ef70bc796dfebe4b46a120dc18c31fbb Mon Sep 17 00:00:00 2001 From: Shigma Date: Sun, 12 May 2024 16:04:41 +0800 Subject: [PATCH] test: use mock from node:test instead of jest mock --- package.json | 1 - packages/core/tests/command.spec.ts | 14 +++++++------- packages/core/tests/middleware.spec.ts | 6 +++--- packages/loader/tests/loader.spec.ts | 6 +++--- packages/loader/tests/utils.ts | 2 +- packages/utils/tests/observe.spec.ts | 2 +- plugins/common/broadcast/tests/index.spec.ts | 16 ++++++++-------- plugins/common/echo/tests/index.spec.ts | 2 +- 8 files changed, 24 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index e2e3f5ed66..9e5a831a77 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/core/tests/command.spec.ts b/packages/core/tests/command.spec.ts index 95716c06d7..55540441cf 100644 --- a/packages/core/tests/command.spec.ts +++ b/packages/core/tests/command.spec.ts @@ -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) @@ -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()) @@ -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') @@ -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) }) @@ -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 diff --git a/packages/core/tests/middleware.spec.ts b/packages/core/tests/middleware.spec.ts index 7f33bb2a7d..3555b80f39 100644 --- a/packages/core/tests/middleware.spec.ts +++ b/packages/core/tests/middleware.spec.ts @@ -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 any> @@ -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 = [] }) diff --git a/packages/loader/tests/loader.spec.ts b/packages/loader/tests/loader.spec.ts index 457d81edc7..5292cde0a4 100644 --- a/packages/loader/tests/loader.spec.ts +++ b/packages/loader/tests/loader.spec.ts @@ -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' @@ -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) diff --git a/packages/loader/tests/utils.ts b/packages/loader/tests/utils.ts index 226e70ff1e..8d7f540544 100644 --- a/packages/loader/tests/utils.ts +++ b/packages/loader/tests/utils.ts @@ -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 diff --git a/packages/utils/tests/observe.spec.ts b/packages/utils/tests/observe.spec.ts index ee13fd2725..250b2f34a0 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 * as jest from 'jest-mock' +import { mock as jest } from 'node:test' describe('Observer API', () => { it('type checks', () => { diff --git a/plugins/common/broadcast/tests/index.spec.ts b/plugins/common/broadcast/tests/index.spec.ts index 953f4bfb3e..20808061df 100644 --- a/plugins/common/broadcast/tests/index.spec.ts +++ b/plugins/common/broadcast/tests/index.spec.ts @@ -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({ @@ -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') }) }) diff --git a/plugins/common/echo/tests/index.spec.ts b/plugins/common/echo/tests/index.spec.ts index fb2638c210..ee70ee56ee 100644 --- a/plugins/common/echo/tests/index.spec.ts +++ b/plugins/common/echo/tests/index.spec.ts @@ -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'