jest-useragent-mock
Mock navigator.userAgent
when run unit test cases with jest.
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
- Add some common useragents, such as WeChat, etc.