jestjs

How to test mapStateToProps using React Redux and Jest?

眉间皱痕 提交于 2021-02-11 06:07:21
问题 When I create a test for my connected React component where I want to test the mapStateToProps logic I run into a problem which I'm not sure how to solve. Error message Expected: 1 Received: undefined 24 | it('should show previously rolled value', () => { 25 | // test that the state values were correctly passed as props > 26 | expect(wrapper.props().lastRolledNumber).toBe(1); When I check the wrapper.props() it only returns the store object and no properties. To make sure it's not my code I

Expect array argument to have set length

我的梦境 提交于 2021-02-10 17:47:48
问题 I'm a bit of a newbie to Jest so please forgive me if this is an obvious answer but I cannot find an answer after scrolling through the docs. I have a function (funcA) which passes an array of different lengths to another function (funcB) dependent on the arguments that funcA receives. I'm attempting to test the the length of the array that is passed to funcB is correct based on the arguments that I give to funcA . I am not that bothered about the contents of the array, just that it has a

React Jest/Enzyme Testing: useHistory Hook Breaks Test

烂漫一生 提交于 2021-02-10 17:45:48
问题 I'm fairly new to React, so please forgive my ignorance. I have a component: const Login: FunctionComponent = () => { const history = useHistory(); //extra logic that probably not necessary at the moment return ( <div> <form action=""> ...form stuff </form> </div> ) } When attempting to write jest/enzyme test, the one test case I've written is failing with the following error ` › encountered a declaration exception TypeError: Cannot read property 'history' of undefined` I've tried to use jest

How to fix “zone is not defined” in this project

耗尽温柔 提交于 2021-02-10 17:44:40
问题 I've migrated a project from Node 6 to Node 10 and in the process made some changes and version upgrades which resulted in failing test cases and whatever fixes I tried, the test are still failing on "ReferenceError: Zone is not defined" I've tried changing the versions of different dependencies incrementally from basic to latest and still the tests would not pass. Here is the Package json: "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "build:prod": "ng build --prod --env

how to test async function making use of moxios?

限于喜欢 提交于 2021-02-10 17:29:49
问题 Consider the following react code. sendFormData = async formData => { try { const image = await axios.post("/api/image-upload", formData); this.props.onFileNameChange(image.data); this.setState({ uploading: false }); } catch (error) { console.log("This errors is coming from ImageUpload.js"); console.log(error); } }; Here is the test it("should set uploading back to false on file successful upload", () => { const data = "dummydata"; moxios.stubRequest("/api/image-upload", { status: 200,

how to test async function making use of moxios?

℡╲_俬逩灬. 提交于 2021-02-10 17:29:20
问题 Consider the following react code. sendFormData = async formData => { try { const image = await axios.post("/api/image-upload", formData); this.props.onFileNameChange(image.data); this.setState({ uploading: false }); } catch (error) { console.log("This errors is coming from ImageUpload.js"); console.log(error); } }; Here is the test it("should set uploading back to false on file successful upload", () => { const data = "dummydata"; moxios.stubRequest("/api/image-upload", { status: 200,

React - how do I unit test an API call in Jest?

安稳与你 提交于 2021-02-10 17:26:42
问题 I have a bunch of API calls that I would like to unit test. As far as I know, unit testing API calls doesn't involve actually making those API calls. As far as I know you would simulate responses of those API calls and then test on the DOM changes however I'm currently struggling to do this. I have the following code: App.js function App() { const [text, setText] = useState(""); function getApiData() { fetch('/api') .then(res => res.json()) .then((result) => { console.log(JSON.stringify

React - how do I unit test an API call in Jest?

人盡茶涼 提交于 2021-02-10 17:23:11
问题 I have a bunch of API calls that I would like to unit test. As far as I know, unit testing API calls doesn't involve actually making those API calls. As far as I know you would simulate responses of those API calls and then test on the DOM changes however I'm currently struggling to do this. I have the following code: App.js function App() { const [text, setText] = useState(""); function getApiData() { fetch('/api') .then(res => res.json()) .then((result) => { console.log(JSON.stringify

React - how do I unit test an API call in Jest?

那年仲夏 提交于 2021-02-10 17:19:50
问题 I have a bunch of API calls that I would like to unit test. As far as I know, unit testing API calls doesn't involve actually making those API calls. As far as I know you would simulate responses of those API calls and then test on the DOM changes however I'm currently struggling to do this. I have the following code: App.js function App() { const [text, setText] = useState(""); function getApiData() { fetch('/api') .then(res => res.json()) .then((result) => { console.log(JSON.stringify

Unable to debug test case using Jest in chrome under node devTools

泪湿孤枕 提交于 2021-02-10 15:53:41
问题 I just started using Jest. For debugging under testcase file; describe("Filter function", () => { test("it should filter by a search", () => { const input = [ { id: 1, url: "https://www.google.com" }, { id: 2, url: "https://www.yahoo.com" }, { id: 3, url: "https://www.gmail.com" } ]; const output = [{ id: 3, url: "https://www.gmail.com" }]; debugger; expect(filterByTerm(input, "gmail")).toEqual(output); debugger; expect(filterByTerm(input, "GMAIL")).toEqual(output); }); }); under Project