Skip to content

Commit

Permalink
feat(test-utils): optimize assertion output
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 5, 2021
1 parent 0ba42f9 commit bc53e08
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/koishi-test-utils/src/app.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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))
}
}
}
Expand All @@ -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 }

0 comments on commit bc53e08

Please sign in to comment.