jestjs

Jest - Mock a constant property from a module for a specific test

别说谁变了你拦得住时间么 提交于 2021-01-26 21:21:02
问题 So, I'm attempting to do something which on the surface should be very simple... I have some constants defined in: ` //constants.js module.exports = { MY_CONSTANT: "TEST" } ` I have a file which I'm trying to test which has a branching statement like this: ` //file to test //... if(CONSTANTS.MY_CONSTANT === "TEST") {...} ... ` And I have a test like this: ` //test it("Should do something when MY_CONSTANT === "TEST, () => { //This is fine as it is exported as TEST }) it("Should do something

Jest - Mock a constant property from a module for a specific test

痞子三分冷 提交于 2021-01-26 21:15:36
问题 So, I'm attempting to do something which on the surface should be very simple... I have some constants defined in: ` //constants.js module.exports = { MY_CONSTANT: "TEST" } ` I have a file which I'm trying to test which has a branching statement like this: ` //file to test //... if(CONSTANTS.MY_CONSTANT === "TEST") {...} ... ` And I have a test like this: ` //test it("Should do something when MY_CONSTANT === "TEST, () => { //This is fine as it is exported as TEST }) it("Should do something

Set moment timezone in Jest tests

爷,独闯天下 提交于 2021-01-26 11:10:09
问题 I have the util function that is parsing given date (i.e. '2019-01-28') in specific date format and then using momentJS retrieving beginning of that day and converting it to ISO date format: dates.js import moment from 'moment' export const getApiDateFormat = (date, dateFormat = getLocaleDateString()) => moment(date, dateFormat) .startOf('day') .toISOString() I would like to test this function using Jest and set the specific timezone for moment to use those tests independent of my location.

Set moment timezone in Jest tests

社会主义新天地 提交于 2021-01-26 11:07:45
问题 I have the util function that is parsing given date (i.e. '2019-01-28') in specific date format and then using momentJS retrieving beginning of that day and converting it to ISO date format: dates.js import moment from 'moment' export const getApiDateFormat = (date, dateFormat = getLocaleDateString()) => moment(date, dateFormat) .startOf('day') .toISOString() I would like to test this function using Jest and set the specific timezone for moment to use those tests independent of my location.

Set moment timezone in Jest tests

为君一笑 提交于 2021-01-26 11:04:29
问题 I have the util function that is parsing given date (i.e. '2019-01-28') in specific date format and then using momentJS retrieving beginning of that day and converting it to ISO date format: dates.js import moment from 'moment' export const getApiDateFormat = (date, dateFormat = getLocaleDateString()) => moment(date, dateFormat) .startOf('day') .toISOString() I would like to test this function using Jest and set the specific timezone for moment to use those tests independent of my location.

Babel throwing Support for the experimental syntax 'jsx' isn't currently enabled

泪湿孤枕 提交于 2021-01-26 03:15:02
问题 I started newly writing unit test cases using Jest and Enzyme for the react application and when try to run test cases using jest like "test": "jest --watch" rather "test": "react-scripts test" tests going through babel for the runner to understand react syntax. And have been doing setup step by step using babel but this error Support for the experimental syntax 'jsx' isn't currently enabled stopping me to go further. And as suggested in some threads I have been trying with npm install --save

Babel throwing Support for the experimental syntax 'jsx' isn't currently enabled

[亡魂溺海] 提交于 2021-01-26 03:13:08
问题 I started newly writing unit test cases using Jest and Enzyme for the react application and when try to run test cases using jest like "test": "jest --watch" rather "test": "react-scripts test" tests going through babel for the runner to understand react syntax. And have been doing setup step by step using babel but this error Support for the experimental syntax 'jsx' isn't currently enabled stopping me to go further. And as suggested in some threads I have been trying with npm install --save

Jest transformIgnorePatterns not working

余生颓废 提交于 2021-01-26 03:09:08
问题 I have spent a long time looking at other questions about this and looking at other projects on Github but none of the answers seem to work for me. I am loading a third party library in my project, and when running Jest tests I get the error export default portalCommunication; ^^^^^^ SyntaxError: Unexpected token export > 1 | import portalCommunication from 'mathletics-portal-communication-service'; I have tried updating my Jest config in many ways to get it to transpile this library but I

Jest transformIgnorePatterns not working

耗尽温柔 提交于 2021-01-26 03:09:07
问题 I have spent a long time looking at other questions about this and looking at other projects on Github but none of the answers seem to work for me. I am loading a third party library in my project, and when running Jest tests I get the error export default portalCommunication; ^^^^^^ SyntaxError: Unexpected token export > 1 | import portalCommunication from 'mathletics-portal-communication-service'; I have tried updating my Jest config in many ways to get it to transpile this library but I

How to test class constructor in Jest

让人想犯罪 __ 提交于 2021-01-22 04:29:31
问题 Let's say I have a class like following: class SomeClass { constructor(a, b) { this.a = a; this.b = b; } } How can I test through Jest that constructor was initialized the right way? Say... this.a = a and this.b = b and not vice versa? I know that I can execute toBeCalledWith but that won't let me check the constructor's logic. I was also thinking about making mockImplementation but in this case it seems pointless as I will rewrite the logic, or I may not be aware of all the nuances of