Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对多个测试使用jest.mock_Javascript_Firebase Authentication_Jestjs_Mocking - Fatal编程技术网

Javascript 对多个测试使用jest.mock

Javascript 对多个测试使用jest.mock,javascript,firebase-authentication,jestjs,mocking,Javascript,Firebase Authentication,Jestjs,Mocking,我正在尝试为firebase auth createUserWithEmailAndPassword()函数编写一个单元测试。我有一个我编写的助手类,它调用这个函数并返回一个承诺。我试图在测试中模拟firebase createUserWithEmailAndPassword()函数,但它只适用于一个测试用例。我不知道如何为其他测试用例更改createUserWithEmailAndPassword()的模拟。我使用jest.fn().mockRejectedValueOnce()拒绝承诺并返回

我正在尝试为firebase auth createUserWithEmailAndPassword()函数编写一个单元测试。我有一个我编写的助手类,它调用这个函数并返回一个承诺。我试图在测试中模拟firebase createUserWithEmailAndPassword()函数,但它只适用于一个测试用例。我不知道如何为其他测试用例更改createUserWithEmailAndPassword()的模拟。我使用jest.fn().mockRejectedValueOnce()拒绝承诺并返回错误代码。我要做的是使用mockRejectdValueOnce()重新模拟以处理备用错误代码,并使用mockResolveValueOnce()处理成功案例。我已经尝试将jest.mock(…)移动到测试本身中,但是mock不再工作,而是调用实际函数。这是我要测试的helper类

import app from 'firebase/app';
import 'firebase/auth';

const config = {
  apiKey: "somevalue",
  authDomain: "somevalue",
  databaseURL: "somevalue",
  projectId: "somevalue",
  storageBucket: "somevalue",
  messagingSenderId: "somevalue",
  appId: "somevalue",
  measurementId: "somevalue"
};

class Firebase {

  private auth: app.auth.Auth;

  constructor() {
    app.initializeApp(config);
    this.auth = app.auth();
  }

  public async register(email: string, password: string, name: string): Promise<any> {
    return await this.auth.createUserWithEmailAndPassword(email, password);
  }

}

export default new Firebase();

如果我将模拟移动到测试本身中,它将停止工作。我想使用mock编写更多的测试,但不确定如何做到这一点。非常感谢您的帮助

不确定它是否正是您想要的,但一种方法是链接多个
mockResolvedValueOnce
mockRejectedValueOnce
调用,而不是以您希望在测试中使用它们的任何顺序使用单个调用

从:


这是可行的,但是没有办法在测试中指定mock吗?
import { FirebaseAccountManager } from './FirebaseAccountManager';
import IAccount from './IAccount';

jest.mock('firebase/app', () => (
  {
    auth: jest.fn().mockReturnThis(),
    initializeApp: jest.fn(),
    createUserWithEmailAndPassword: jest.fn().mockRejectedValueOnce({
      code: 'auth/invalid-email'
    }),
  }
));

describe('test', () => {

  test('aTest', async () => {
    const newAccount: IAccount = { firstName: 'asdf', lastName: 'asdf', email: 'asdf.adf.com', password: 'qwer', phoneNumber: '', workStatus: '', city: '', postalCode: '', country: '' }

    const fam = new FirebaseAccountManager();
    await expect(fam.register(newAccount)).rejects.toEqual({
      code: 'auth/invalid-email'
    });

  });
});
test('async test', async () => {
  const asyncMock = jest
    .fn()
    .mockResolvedValue('default')
    .mockResolvedValueOnce('first call')
    .mockResolvedValueOnce('second call');

  await asyncMock(); // first call
  await asyncMock(); // second call
  await asyncMock(); // default
  await asyncMock(); // default
});