c7bdd3770a
- reminder.test.js:43101用例新增对 remind_logs.error 的断言,区分 「真发送失败」(应为微信原始错误信息)与「因halted被跳过」(应为 quota_exhausted兜底文案),此前该区别完全没有测试覆盖 - wx.test.js:把恢复 axios.get/post 的语句移入 try/finally,避免断言 先抛错时污染同进程内后续用例 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
const test = require('node:test')
|
||
const assert = require('node:assert')
|
||
const { useTempDb } = require('./helper')
|
||
|
||
// wx.js 本身不连库,但按项目约定统一在 require 业务模块前处理好临时库,
|
||
// 避免以后 wx.js 引入了连库逻辑时这份测试悄悄读写到真实数据库。
|
||
useTempDb()
|
||
|
||
const axios = require('axios')
|
||
const wx = require('../src/wx')
|
||
|
||
test('微信返回非 0 errcode 时,抛出的 Error 对象应带上原始 errcode', async () => {
|
||
const restoreGet = axios.get
|
||
const restorePost = axios.post
|
||
|
||
try {
|
||
// getAccessToken 走 axios.get,sendSubscribeMessage 走 axios.post,两个都要 mock
|
||
axios.get = async () => ({ data: { access_token: 'fake_token', expires_in: 7200 } })
|
||
axios.post = async () => ({ data: { errcode: 43101, errmsg: 'user refuse to accept the msg' } })
|
||
|
||
await assert.rejects(
|
||
() => wx.sendSubscribeMessage({
|
||
touser: 'oTest',
|
||
page: 'pages/index/index',
|
||
templateId: 'tplTest',
|
||
data: {}
|
||
}),
|
||
(err) => {
|
||
assert.strictEqual(err.errcode, 43101, 'Error 对象应带上微信返回的原始 errcode,而不是靠字符串匹配')
|
||
return true
|
||
}
|
||
)
|
||
} finally {
|
||
// 断言先抛错也要恢复,避免污染同进程内后续用例
|
||
axios.get = restoreGet
|
||
axios.post = restorePost
|
||
}
|
||
})
|