node.js - 我如何使用 jest 来模拟一个 promisified 函数
问题描述
我有一个非常简单的小实用功能
xml2JSON
如下
import { promisify } from 'util'
import { parseString } from 'xml2js'
const xml2js = promisify(parseString)
const xmlToJSON = async xml => xml2js(xml)
export default xmlToJSON
我试图开玩笑地测试它,嘲笑我不需要关心的东西
import * as util from 'util'
import * as xml2js from 'xml2js'
import xmlToJSON from './xmlToJSON'
jest.mock('util')
jest.mock('xml2js')
describe('xmlToJSON', () => {
const promisifiedParseString = jest.fn()
util.promisify = jest.fn(() => promisifiedParseString)
const js = { some: 'result' }
const xml = '<some>result</some>'
let result
beforeAll(async () => {
promisifiedParseString.mockResolvedValue(js)
result = await xmlToJSON(xml)
})
it('promisified the original parseString', () => {
expect(util.promisify).toHaveBeenCalledWith(xml2js.parseString)
})
it('called the promisified parseString with the xml', () => {
expect(promisifiedParseString).toHaveBeenCalledWith(xml)
})
it('returned the expected result', () => {
expect(result).toEqual(js)
})
})
但我得到了错误
TypeError: xml2js is not a function
4 | const xml2js = promisify(parseString)
5 |
> 6 | const xmlToJSON = async xml => xml2js(xml)
| ^
7 |
8 | export default xmlToJSON
9 |
我究竟做错了什么?
更新
根据以下建议,我尝试更改导入顺序
import * as util from 'util'
import * as xml2js from 'xml2js'
jest.mock('util')
jest.mock('xml2js')
const promisifiedParseString = jest.fn()
util.promisify = jest.fn(() => promisifiedParseString)
import xmlToJSON from './xmlToJSON'
describe('xmlToJSON', () => {
const js = { some: 'result' }
const xml = '<some>result</some>'
let result
beforeAll(async () => {
promisifiedParseString.mockResolvedValue(js)
result = await xmlToJSON(xml)
})
it('promisified the original parseString', () => {
expect(util.promisify).toHaveBeenCalledWith(xml2js.parseString)
})
it('called the promisified parseString with the xml', () => {
expect(promisifiedParseString).toHaveBeenCalledWith(xml)
})
it('returned the expected result', () => {
expect(result).toEqual(js)
})
})
但这没什么区别
解决方案
util.promisify
在导入正在使用它的文件之前,您需要更改行为。
所以顺序应该是这样的:
util.promisify = jest.fn(() => promisifiedParseString)
import xmlToJSON from './xmlToJSON'
推荐阅读
- android - Kotlin Flow 与 Android LiveData
- javascript - 在第一个图表底部动态添加 Y 轴到 highchart 而不减小前一个图表的大小
- scala - 使用保存的模型来转换另一个数据而无需再次拟合 Spark
- timer - CountDownTimer:如何增加而不是减少计时器
- vuejs2 - Bootstrap-Table Vuejs OnExpandRow 事件集成
- python - power bi中添加语言检测栏
- python - 根据另一个数组的特定值递增和插入值
- firebase - 如果我想在本机模式和数据存储模式下一起使用数据存储,是否可以使用 2 个谷歌云项目?
- r - 执行块后删除变量
- python - 为什么我在 20% 的尝试中得到 pymodbus ModbusIOException?