MorJS Mock mock 文件类型说明
我们目前兼容 .ts .mjs .jsonc .json5 .json .js .cjs 五种文件类型格式,优先级顺序依次,以下分别说明不同类型文件的写法:
.json文件、.json5文件、.jsonc文件,标准 json 类型文件,返回结果对应 json 的内容
// /mock/wx/getSystemInfo.json
{
"data": { "a": 1, "b": 2 }
}.ts文件、.js文件、.mjs文件、.cjs文件,支持 ESM 和 CJS 两种写法;- 同时支持函数写法,根据入参的不同,返回不同 mock 结果
/* /mock/my/getHAMemoryInfo.js */
// ESModule 写法
export default {
evaluatedStatus: 'good',
currentMemory: 123
}
// CommonJS 写法
module.exports = {
evaluatedStatus: 'good',
currentMemory: 123
}
// 函数写法,根据参数 opts 的不同,return 不同 mock 结果
module.exports = function (opts) {
const {
data: { type }
} = opts
switch (type) {
case 'ali':
return { res: 'alipay' }
case 'wx':
return { res: 'wechat' }
default:
return { res: '其他' }
}
}