问题描述
我正在尝试使用离子反应模拟电容器插件:
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()
};
解决方法
我从 __mocks__
文件夹从根目录移动到 src
文件夹,并且成功了。