文件名Postman.js
"use strict";
define([], function() {
var postManSingleton = (function() {
var postManInstance
, createPostMan = function() {
var events = {};
return {
publish: function(event, payload) {
if(!events.hasOwnProperty(event))
return;
events[event].forEach(function(listener){
listener(payload || {});
});
},
subscribe: function(event, listener) {
if(!events.hasOwnProperty(event))
events[event] = [];
var index = events[event].push(listener)-1;
return {
remove: function () {
delete events[event][index];
}
}
}
}
};
return {
getInstance: function() {
if(!postManInstance)
postManInstance = createPostMan();
return postManInstance;
}
}
}());
return postManSingleton.getInstance();
});
像在index.test.js中一样导入此文件
import * as Postman from 'postman.js';
如何以开玩笑模拟其发布和订阅事件。 我试过了
jest.mock('Postman.publish', () =>({//some code}))
错误:Cannot find module Postman.publish from 'index.test.js'
请帮助我们如何模拟上述文件。它甚至可能吗?
答案 0 :(得分:1)
你的Postman.js在AMD& Jest不支持AMD。所以你需要将AMD转换为commonjs。
您可以使用babel-plugin-transform-amd-to-commonjs进行转化。