javascript - 如何在 Jest 中进行单元测试并检查函数是否正在调用预期的 firebase 方法?

标签 javascript typescript unit-testing firebase-authentication jestjs

App.ts中,我正在调用firebase.auth().signInWithEmailAndPassword(email, password)方法

现在我想进行单元测试,以检查当从 App.spec.ts 调用 myAuthenticationPlugin.authenticate(email,password) 方法时,它还会调用 firebase.auth().signInWithEmailAndPassword(email, password) 方法,因为这就是 App.ts 本质上所做的..

我尝试了多种解决方法,但没有效果。

App.ts

const App= {
    authenticate: async (email, password) => {
        await firebase.auth().signInWithEmailAndPassword(email, password)
  },
}

App.spec.ts

import myAuthenticationPlugin from 'authenticationPlugin/App'
import firebase from 'firebase/app'
jest.mock('firebase/app', () => {
  const firebase = {
    auth: jest.fn(() => {
      return {
        currentUser: {
          email: 'test',
          uid: '123',
          emailVerified: true
        },

        signInWithEmailAndPassword: jest.fn().mockImplementation()
      }
    }),
    initializeApp: jest.fn()
  }
  return firebase
})

describe('Test for authenticate ()', () => {
    it('signInWithEmailAndPassword ()', () => {
      const email = 'test'
      const password = 'mypassword'
      myAuthenticationPlugin.authenticate(email, password)
      expect(firebase.auth().signInWithEmailAndPassword).toHaveBeenCalled()
    })
  })

我收到错误

● App.js (Authentication Plugin) › Test for authenticate () › signInWithEmailAndPassword ()

    expect(jest.fn()).toHaveBeenCalled()

    Expected number of calls: >= 1
    Received number of calls:    0

      44 |       const password = 'mypassword'
      45 |       myAuthenticationPlugin.authenticate(email, password)
    > 46 |       expect(firebase.auth().signInWithEmailAndPassword).toHaveBeenCalled()
         |                                                          ^
      47 |     })
      48 |   })
      49 | })

      at Object.<anonymous> (tests/unit/App.spec.ts:46:58)

最佳答案

这是统一测试解决方案:

app.ts:

import firebase from 'firebase/app';

const App = {
  authenticate: async (email, password) => {
    await firebase.auth().signInWithEmailAndPassword(email, password);
  },
};

export default App;

app.test.ts:

import App from './app';
import firebase from 'firebase/app';

jest.mock('firebase/app', () => {
  return {
    auth: jest.fn().mockReturnThis(),
    signInWithEmailAndPassword: jest.fn(),
  };
});

describe('61352544', () => {
  it('should pass', async () => {
    const email = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c79647d716c70795c7b717d7570327f7371" rel="noreferrer noopener nofollow">[email protected]</a>';
    const password = '123';
    await App.authenticate(email, password);
    expect(firebase.auth().signInWithEmailAndPassword).toBeCalledWith(email, password);
  });
});

100%覆盖率的单元测试结果:

 PASS  stackoverflow/61352544/app.test.ts (12.122s)
  61352544
    ✓ should pass (9ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 app.ts   |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        14.061s

源代码:https://github.com/mrdulin/react-apollo-graphql-starter-kit/tree/master/stackoverflow/61352544

关于javascript - 如何在 Jest 中进行单元测试并检查函数是否正在调用预期的 firebase 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61352544/

相关文章:

typescript - 如何在打开的编辑器中搜索具有给定扩展名的文件?

python - 如何将测试名称和模块添加到结果中的测试文档字符串?

unit-testing - 如何断言与 stretr/testify/mock AssertCalled 的部分匹配?

javascript - 如何获取作为相对元素的子元素的 DOM 元素的鼠标位置?

php - 如何从 PHP 端将 JavaScript 函数中的数组字符串作为参数传递?

javascript - 使用 CSS 转换或 javascript 缩放元素以动态适应其父元素

typescript - 如何在 typescript 中使用 es6-promises?

javascript - js 中的闭包如何与 let 一起工作?

具有 clipboardData 属性的 Angular2 组件

ios - 类比较,isKindOfClass 在 Kiwi 规范中不起作用