javascript 如何在 JEST 中模拟 Cookie.get('language')
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50761393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to mock Cookie.get('language') in JEST
提问by Kimaya
I have a function which sets a language in cookie and gets it for some functionality. How do I set the value of language correctly for testing in JEST
我有一个函数,它在 cookie 中设置一种语言并获取它的某些功能。如何在 JEST 中正确设置语言值以进行测试
function resolveLanguage () {
// browser cookie is checked to see if there is a value for language
const lang = Cookie.get('language')
// If no lang, it is defaulted to en
return lang || 'en'
}
Now I want to test it in JEST
现在我想在 JEST 中测试它
it('can resolve the language using a cookie', () => {
Cookie.__defineGetter__("language", function() {
return 'fr'
})
// console.log(Cookie.language) //returns fr
// console.log(Cookie.get('language')) //returns fr
expect(module.resolveLanguage()).toBe('fr') ---FAILS because it returns en
})
回答by iflp
Don't have to download any deps, just use:
不必下载任何 deps,只需使用:
Object.defineProperty(window.document, 'cookie', {
writable: true,
value: 'myCookie=omnomnom',
});
(writableneeds to be set to true)
(writable需要设置为true)
回答by Andreas K?berle
You need to mock js-cookieusing jest to set the language set you want.
您需要js-cookie使用 jest进行模拟以设置您想要的语言集。
import Cookie from 'js-cookie'
jest.mock('js-cookie', ()=> jest.fn())
Cookie.setMockImplementation(()=>({get: () => 'fr'}))
Or if you only need to set it once
或者如果你只需要设置一次
jest.mock('js-cookie', ()=>({get: () => 'fr'}))
Note that this solution would always return 'fr' for all Cookie.getcalls. If you need support multiple values on get you could do something like this:
请注意,此解决方案将始终为所有Cookie.get调用返回 'fr' 。如果您需要在 get 上支持多个值,您可以执行以下操作:
jest.mock('js-cookie', ()=>({get: key => {
language: 'fr',
somethingElse: 'someThing'
}[key]}))
回答by itsme_seenu
The below code helped with mocking set/get for cookies for all types of tests in Jest:
以下代码有助于在 Jest 中为所有类型的测试模拟设置/获取 cookie:
Object.defineProperty(document, "doctype", {
value: "<!DOCTYPE html>"
});
let setCookie = (v) => v;
Object.defineProperty(window.navigator, 'cookieEnabled', (function (_value) {
return {
get: function _get() {
return _value;
},
set: function _set(v) {
_value = v;
},
configurable: true
};
})(window.navigator.cookieEnabled));
setCookie = (v) => v;
Object.defineProperty(window.document, 'cookie', (function (_value) {
return {
get: function _get() {
return _value;
},
set: function _set(v) {
_value = setCookie(v);
},
configurable: true
};
})(window.navigator.cookieEnabled));
回答by misspiggy
I tried this and it worked:
我试过了,它奏效了:
import Cookies from 'js-cookie';
Cookies.get = jest.fn()
.mockImplementation(() => 'fr');
If you want to return different values each time you call Cookies.get(), you can do this:
如果您想在每次调用时返回不同的值Cookies.get(),您可以这样做:
Cookies.get = jest.fn()
.mockImplementationOnce(() => 'en') // first time
.mockImplementationOnce(() => 'de'); // second time
回答by Mat Lipe
When using the setFilesAfterEnvoption in JEST, adding the following to the defined setup file works pretty well.
在 JEST 中使用setFilesAfterEnv选项时,将以下内容添加到定义的安装文件中效果很好。
Enables normal cookie handling without any mocking.
无需任何模拟即可启用正常的 cookie 处理。
let __cookies;
Object.defineProperty( window.document, 'cookie', {
get: () => __cookies,
set: v => __cookies = v,
slit: s => __cookies.split( s ),
} );

