模拟电容器插件 ionic 与 Jest

时间:2021-01-08 08:43:38

标签: reactjs typescript ionic-framework capacitor capacitor-plugin

我正在尝试使用离子反应模拟电容器插件:

import { ICreateAccountAction } from "../../framework/auth/interfaces/ICreateAccountAction";
import { Plugins } from '@capacitor/core';
import oauthCreateAccountConfig from "../../config/azure/oauth_register.json"
import { CreateAccountResult } from "../../framework/auth/result/CreateAccountResult";
import { AuthError } from "../../framework/auth/exceptions/AuthError";


export class CreateAccount implements ICreateAccountAction {
    async execute() {
        try {
            const response = await Plugins.OAuth2Client.authenticate(oauthCreateAccountConfig)
            let accessToken = response["access_token"];
            let refreshToken = response["refresh_token"];

            return new CreateAccountResult(accessToken, refreshToken) 
        } catch (error) {
            throw new AuthError(error)
        }
    }
}

我想模拟import { Plugins } from '@capacitor/core';

我试过了:

describe("CreateAccount", () => {

    describe("#execute", () => {
        test("return `CreateAccountResult`", async () => {

            jest.mock('@capacitor/core', () => ({ Plugins: () => { return "" } }))
            const uat = new CreateAccount()

            const result = await uat.execute()

            expect(result).toBeInstanceOf(CreateAccountResult)
        })

    })
})

__ 模拟 __ 文件夹

我也尝试过 __ mocks __ 文件夹 (__mocks__/@capacitor/core.ts)

export const Plugins = {};

但我还是明白了

OAuth2Client does not have web implementation.

意味着模拟没有工作

也尝试过:

export const Plugins = {
    OAuth2Client: () => jest.fn()
};

1 个答案:

答案 0 :(得分:0)

我从 __mocks__ 文件夹从根目录移动到 src 文件夹,并且成功了。

相关问题