Skip to content

Commit

Permalink
feat(test-utils): BREAKING Session API Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 24, 2020
1 parent c46f9eb commit 8799a2d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
14 changes: 9 additions & 5 deletions build/jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import { resolve } from 'path'

const args = ['jest', '--coverage']

if (process.argv[2]) {
args.push(process.argv[2])
if (process.argv[3]) {
args.push('--collectCoverageFrom', `**/${process.argv[3]}/**/*.ts`)
const [,, argv2, argv3] = process.argv

if (argv2 && !argv2.startsWith('-')) {
args.push(argv2)
if (argv3 && !argv3.startsWith('-')) {
args.push('--collectCoverageFrom', `**/${argv3}/**/*.ts`, ...process.argv.slice(3))
} else {
args.push(...process.argv.slice(3))
}
} else {
args.push('packages/.+\\.spec\\.ts')
args.push('packages/.+\\.spec\\.ts', ...process.argv.slice(2))
}

const child = spawn('npx', args, { stdio: 'inherit' })
Expand Down
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ module.exports = {
moduleNameMapper: {
"koishi-(test-utils|(database|plugin)-[\\w-]+)(/dist)?": "<rootDir>/packages/$1/src",
"koishi-[\\w-]+": "<rootDir>/packages/$0/src",
"shiki-core": "<rootDir>/bots/shiki/core",
"touhou-words": "<rootDir>/bots/shiki/lib/touhou-words",
"shiki-universe": "<rootDir>/bots/shiki/lib/shiki-universe/src",
"inference-puzzles": "<rootDir>/bots/shiki/lib/inference-puzzles/src",
},
coverageReporters: ['text', 'lcov'],
coveragePathIgnorePatterns: [
Expand Down
1 change: 1 addition & 0 deletions packages/test-utils/src/koishi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface MockedRandomMultiPick extends RealRandomMultiPick, jest.Mock<any, [rea
mockIndicesOnce (...indices: number[]): this
}

export const sleep = jest.fn(_utils.sleep)
export const randomBool = jest.fn(_utils.randomBool)
export const randomId = jest.fn(_utils.randomId)
export const randomReal = jest.fn(_utils.randomReal)
Expand Down
25 changes: 11 additions & 14 deletions packages/test-utils/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,29 @@ export class Session {
}

async send (message: string) {
let payload: ResponsePayload = null
let replies: string[] = []
function $response (data: ResponsePayload) {
payload = data
if (data.reply) replies.push(data.reply)
}
await this.app.receiveMessage({ ...this.meta, message, $response })
return payload
return replies
}

async getReply (message: string) {
const response = await this.send(message)
return response?.reply
}

shouldHaveReply (message: string, reply?: string) {
async shouldHaveReply (message: string, reply?: string) {
const replies = await this.send(message)
const lastReply = replies[replies.length - 1]
if (reply) {
return expect(this.getReply(message)).resolves.toBe(reply)
return expect(lastReply).toBe(reply)
} else {
return expect(this.getReply(message)).resolves.toBeTruthy()
return expect(lastReply).toBeTruthy()
}
}

shouldHaveNoResponse (message: string) {
return expect(this.send(message)).resolves.toBeNull()
shouldHaveNoReplies (message: string) {
return expect(this.send(message)).resolves.toHaveLength(0)
}

shouldMatchSnapshot (message: string) {
return expect(this.getReply(message)).resolves.toMatchSnapshot(message)
return expect(this.send(message)).resolves.toMatchSnapshot(message)
}
}

0 comments on commit 8799a2d

Please sign in to comment.