jest-useragent-mock

Mock `navigator.userAgent` when run unit test cases with jest.

Usage no npm install needed!

<script type="module">
  import jestUseragentMock from 'https://cdn.skypack.dev/jest-useragent-mock';
</script>

README

jest-useragent-mock

Mock navigator.userAgent when run unit test cases with jest.

Build Status license coverage

Install

// use yarn
yarn add jest-useragent-mock -D
// use npm
npm install jest-useragent-mock  --save-dev

Setup

jest.config.js

{
   "setupFiles": ["jest-useragent-mock"]
}

Usage

import { clear, mockUserAgent } from 'jest-useragent-mock'

describe('test usergaent', () => {
  afterEach(() => {
    clear()
  })

  it('can mock userAgent', () => {
    const mockAgent = 'test'
    mockUserAgent(mockAgent)
    expect(window.navigator.userAgent).toEqual(mockAgent)
  })
})

TODO

  1. Add some common useragents, such as WeChat, etc.