假设您有一系列帮助功能的模块。在某些项目中,可能只需要一项功能。您还希望模块易于测试,例如用户应该能够模拟函数,而不必诉诸额外的模拟库。请查看以下两种情况。
可测试
// code.js
export const namespace = {
a() {...},b() {this.a();},c()
};
// user.js
const {namespace} = require("./code.js");
export function userFn() {
namespace.b();
}
// ===> Everything in code.js will be bundled. :(
// test.js
const {namespace} = require("./code.js");
const {userFn} = require("user.js");
spyOn(namespace.b)
userFn();
assert.equal(namespaced.b.called,1);
摇树
// code.js
export function a() {...}
export function b() {a()}
export function c() {}
// user.js
import {b} from "./code.js";
function userFn() {b();}
//Function "c" from code.js will not be bundled.
// test.js
// ===> You only really need to know that "b" was called. How to test that? :(
我们是否可以具有可测试的辅助功能模块,并且可以摇摇树的功能-最好不需要用户拥有额外的测试库左右?
此外,如果我的场景中有错误,请对我大喊。