jestjs

Mocking shelljs with Jest - [TypeError: shell.exec is not a function]

不羁的心 提交于 2020-12-15 06:46:11
问题 As mentioned in my previous question about mocking, I am new to Jest and testing and I seem to be getting some curveballs. This time around I am having trouble mocking shelljs in my CLI application. Automocking jest.mock('shelljs'); didn't work and errored as:[TypeError: shell.exec is not a function] So I went ahead and tried to use mockImplementation() jest.mock('shelljs', () => { return jest.fn().mockImplementation(() => { return { exec: () => {} }; }); }); To my surprise I am still getting

Reactjs npm test: Jest encountered an unexpected token

你。 提交于 2020-12-15 06:08:21
问题 I'm getting an error when running npm test. I feel like I have tried every existing suggestion online, but I can not get it to work. Here is my package.json . I thought the "transformIgnorePatterns": [ "/node_modules/(?!@total/*)", ], would solve the issue, but it didn't. { "name": "ssu-dashboard-client", "version": "2.1.0", "description": "Dashboard for operation planning", "main": "server.js", "author": "olahalvorsen", "repository": {}, "license": "MIT", "scripts": { "dev": "cross-env NODE

Reactjs npm test: Jest encountered an unexpected token

こ雲淡風輕ζ 提交于 2020-12-15 06:08:05
问题 I'm getting an error when running npm test. I feel like I have tried every existing suggestion online, but I can not get it to work. Here is my package.json . I thought the "transformIgnorePatterns": [ "/node_modules/(?!@total/*)", ], would solve the issue, but it didn't. { "name": "ssu-dashboard-client", "version": "2.1.0", "description": "Dashboard for operation planning", "main": "server.js", "author": "olahalvorsen", "repository": {}, "license": "MIT", "scripts": { "dev": "cross-env NODE

cannot mock function in useEffect when toMatchSnapshot in Jest

旧巷老猫 提交于 2020-12-15 04:28:50
问题 I'm trying to get all snapshot on my LayoutDummy.js to complete my unit test coverage when I Run Test I got error, it says dummyFunc is not a function like below I have wrote my actual code in functional component and testing file like this LayoutDummy.js import React, { useEffect, useState } from 'react'; const LayoutDummy = () => { const [caption, setCaption] = useState('loading...'); useEffect(() => { dummyFunc(); }, []); const dummyFunc = () => { setCaption('dummyFunc is running'); };

Mocking API calls with Jest

混江龙づ霸主 提交于 2020-12-15 03:36:01
问题 I am trying to mock axios module inside my test file like this. import axios from 'axios'; jest.mock('axios', () => ({ __esModule: true, create: jest.fn(), get: jest.fn(), post: jest.fn(), default:jest.fn() })); jest.mock("../../utils/api"); Here is mocked API in a folder __mocks__ //../../utils/api const mockLogin = jest.fn((email, password) => { return Promise.resolve({ email, password }); }); const errors = jest.fn((errors) =>{ return errors }); module.exports = { ...jest.requireActual("..

Mocking API calls with Jest

耗尽温柔 提交于 2020-12-15 03:35:16
问题 I am trying to mock axios module inside my test file like this. import axios from 'axios'; jest.mock('axios', () => ({ __esModule: true, create: jest.fn(), get: jest.fn(), post: jest.fn(), default:jest.fn() })); jest.mock("../../utils/api"); Here is mocked API in a folder __mocks__ //../../utils/api const mockLogin = jest.fn((email, password) => { return Promise.resolve({ email, password }); }); const errors = jest.fn((errors) =>{ return errors }); module.exports = { ...jest.requireActual("..

Jest not covered the function inside useEffect

瘦欲@ 提交于 2020-12-15 01:55:23
问题 I want to test a function that fetch something from server, it looks like this : import React, { useEffect } from 'react'; function Component1() { useEffect(() => { fetchSomeData(); }, []) const fetchSomeData = async () =>{ console.log('fetchSomeData') } return <div>Component1</div>; } export default Component1; and test file: import React from 'react'; import { render, unmountComponentAtNode } from 'react-dom'; import { act } from 'react-dom/test-utils'; import Enzyme, { shallow } from

Jest not covered the function inside useEffect

五迷三道 提交于 2020-12-15 01:53:50
问题 I want to test a function that fetch something from server, it looks like this : import React, { useEffect } from 'react'; function Component1() { useEffect(() => { fetchSomeData(); }, []) const fetchSomeData = async () =>{ console.log('fetchSomeData') } return <div>Component1</div>; } export default Component1; and test file: import React from 'react'; import { render, unmountComponentAtNode } from 'react-dom'; import { act } from 'react-dom/test-utils'; import Enzyme, { shallow } from

React Testing Library Mock function not called

谁说胖子不能爱 提交于 2020-12-13 11:04:25
问题 I am pretty new to testing and attempting to write what should be a simple test for our project... test('Attempt Login', async () => { const submitHandler = jest.fn( ()=> console.log('hello')) const { debug, getByText, getByTestId, getByPlaceholderText } = render ( <Router> <LoginPage submitHandler={submitHandler} /> </Router> ) fireEvent.change(getByPlaceholderText("Enter Username"), { target: { value: "admin" } }); fireEvent.change(getByPlaceholderText("Enter Password"), { target: { value:

Jest & Hooks : TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined

我是研究僧i 提交于 2020-12-13 10:36:53
问题 In create-react-app, I'm trying to simple test with jest but I'm getting this error : TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined. A part of my component AppBarUser.js /... const AppBarUser = () => { const classes = useStyles(); const [, formDispatch] = useContext(FormContext); const [t] = useContext(TranslationContext); const [openDrawer, setDrawer] = useState(false); const [userInfos, setData] = useState({}); useEffect(() => { const fetchData = async () => { try {