简体   繁体   English

模拟电容器插件离子与 Jest 反应

[英]Mock capacitor plugin ionic react with Jest

I'm trying to mock a capacitor plugin with ionic react:我正在尝试使用离子反应模拟电容器插件:

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)
        }
    }
}

I want to mock import { Plugins } from '@capacitor/core';我想模拟import { Plugins } from '@capacitor/core';

I tried:我试过了:

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 __ folder __ 模拟 __ 文件夹

I tried with __ mocks __ folder too ( __mocks__/@capacitor/core.ts )我也尝试使用 __ mocks __ 文件夹( __mocks__/@capacitor/core.ts

export const Plugins = {};

But I still get但我仍然得到

OAuth2Client does not have web implementation.

Meaning that the mock did not worked这意味着模拟没有工作

Tried with too:也试过:

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

I moved from __mocks__ folder from root to src folder, and it worked.我从__mocks__文件夹从根目录移动到src文件夹,它工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM