From bc53e08cafe8d95b2404d2cd00a989e3c960fdb6 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Fri, 5 Mar 2021 16:49:02 +0800 Subject: [PATCH] feat(test-utils): optimize assertion output --- packages/koishi-test-utils/src/app.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/koishi-test-utils/src/app.ts b/packages/koishi-test-utils/src/app.ts index 3cc51e7a86..ab7ddf5d91 100644 --- a/packages/koishi-test-utils/src/app.ts +++ b/packages/koishi-test-utils/src/app.ts @@ -1,6 +1,7 @@ import { AppOptions, App, Adapter, Session, Bot, AuthorInfo } from 'koishi-core' import { assert } from 'chai' import { Socket } from 'net' +import { format } from 'util' import * as http from 'http' import * as memory from './memory' @@ -172,17 +173,19 @@ export class TestSession { async shouldReply(message: string, reply?: string | RegExp | (string | RegExp)[]) { if (!reply) { const result = await this.receive(message) - return assert.ok(result.length, `expected "${message}" to be replied but not received nothing`) + return assert.ok(result.length, format(RECEIVED_NOTHING, message)) } if (!Array.isArray(reply)) reply = [reply] const result = await this.receive(message, reply.length) for (const index in reply) { const expected = reply[index] + const actual = result[index] + assert.ok(actual, format(RECEIVED_NOTHING, message)) if (typeof expected === 'string') { - assert.strictEqual(result[index], expected) + assert.strictEqual(actual, expected, format(RECEIVED_OTHERWISE, message, `"${expected}"`, actual)) } else { - assert.match(result[index], expected) + assert.match(actual, expected, format(RECEIVED_OTHERWISE, message, expected.toString(), actual)) } } } @@ -193,4 +196,7 @@ export class TestSession { } } +const RECEIVED_NOTHING = 'expected "%s" to be replied but received nothing' +const RECEIVED_OTHERWISE = 'expected "%s" to be replied with %s but received "%s"' + export { MockedApp as App }