How to change the ua of jsdom in test file in jest

because there is logic in the code to determine whether it is a mobile phone / PC, so you need to change the ua in the test code to test. How can it be implemented?

Apr.18,2022

configure jest config testEnvironmentOptions.userAgent , refer to https://codeshelper.com/q/10.... Or use setupFiles , for example

Object.defineProperty(global.window.navigator, "userAgent", {
  writable: false, 
  value: "Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/7.0.4(0x17000428) NetType/4G Language/zh_CN"
});

I don't know if it's all right, but I haven't tested it.

Menu