首页 > 解决方案 > 如何在 Sveltekit 中用 __mocks__ 替换函数?

问题描述

我的目标是为我的打字稿功能编写高效的单元+集成测试。每个应该被模拟的函数都有一个__mocked__文件夹和一个匹配的文件名。因为我在配置 Sveltekit 时不知道怎么做,所以我目前在代码库中这样做:

export async function updateAuth0User(opts: any) {
    if (config.isTest) {
        // TODO One day would be nice if we could avoid this...
        const { updateAuth0User } = await import('./__mocks__/update-auth0-user');
        // @ts-ignore
        return updateAuth0User.apply(this, arguments);
    }
    // actual implementation, when not running tests;
}

以上可以自动完成,所以我可以避免config.isTest污染我的代码吗?

提前谢谢☺️

标签: sveltekit

解决方案


推荐阅读