【发布时间】:2021-10-13 07:59:03
【问题描述】:
我正在创建单元测试,但我可以找到一种方法来模拟 firebase 函数并在调用它们时指定返回类型。下面我发布了我想要模拟的内容(account.service.ts)以及我当前进行的测试。我想模拟并指定要使用 admin 返回的内容...(也就是设置 resp 对象值)。
以管理员身份从“firebase-admin”导入 *;
account.service.ts
const resp = admin
.auth()
.createUser({
email: registerDto.email,
emailVerified: false,
password: registerDto.password,
displayName: registerDto.displayName,
disabled: false,
})
.then(
(userCredential): Account => ({
uid: userCredential.uid,
email: userCredential.email,
emailVerified: userCredential.emailVerified,
displayName: userCredential.displayName,
message: 'User is successfully registered!',
}),
)
.catch((error) => {
// eslint-disable-next-line max-len
throw new HttpException(`${'Bad Request Error creating new user: '}${error.message}`, HttpStatus.BAD_REQUEST);
});
account.service.spec.ts
describe('61396089', () => {
afterEach(() => {
jest.restoreAllMocks();
});
const obj = {
uid: 'uid',
email: 'email',
emailVerified: 'emailVerified',
displayName: 'displayName',
message: 'User is successfully registered!',
};
const jestAdminMock = {
admin: () => ({
auth: () => ({
createUser: () => ({
then: () => ({
catch: () => obj,
}),
}),
}),
}),
};
it('should pass', () => {
const mockDataTable = {
admin: jest.fn().mockReturnThis(),
auth: jest.fn().mockReturnThis(),
createUser: jest.fn().mockReturnThis(),
then: jest.fn().mockReturnThis(),
catch: jest.fn().mockReturnValueOnce(obj),
};
jest.spyOn(jestAdminMock, 'admin').mockImplementationOnce(() => mockDataTable);
const actual = service.registerUser(registerDTO);
expect(actual).toBe(obj);
});
});
});
【问题讨论】:
-
admin对象从何而来? -
以管理员身份从 'firebase-admin' 导入 *;
标签: node.js typescript firebase unit-testing jestjs