Files
wxserver/server/test/wx.test.js
T
yuming 57470ade4f 补充发送侧分组与错误分支的测试覆盖,保留43101原始错误信息的修复
- reminder.js:43101 跳过日志改为记录微信原始错误信息,便于和「额度耗尽」的
  兜底文案区分排查(前一实施者已验证,此次原样带入提交)
- reminder.test.js:新增 runOnce 按 openid 分组结算用例,验证多用户额度互不
  串号;将「非额度类错误」用例扩到 2 条数据,证明该分支不会误中止后续发送
- 新建 wx.test.js:验证 sendSubscribeMessage 在微信返回非 0 errcode 时,
  抛出的 Error 对象确实带有正确的 errcode 属性

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-16 09:07:46 +08:00

36 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
// getAccessToken 走 axios.getsendSubscribeMessage 走 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
}
)
axios.get = restoreGet
axios.post = restorePost
})