jestjs

Trying to make a simple Axios mock with jest

﹥>﹥吖頭↗ 提交于 2021-01-27 07:32:23
问题 I am trying to understand this example, so the first thing I am trying is removing his axiosConfig.js , so the example looks more like the current case I would like to solve. However I get this error - Expected + Received Object { + "baseURL": "https://jsonplaceholder.typicode.com/albums", "method": "get", "url": "/3/photos?_limit=3", }, Number of calls: 1 39 | const photos = await getPhotosByAlbumID(3); 40 | expect(axios.request).toHaveBeenCalled(); > 41 | expect(axios.request)

Faulty auto import with path alias

◇◆丶佛笑我妖孽 提交于 2021-01-27 07:19:52
问题 I'm having strange issue when auto importing services/components from folder provided with path alias within Angular 9 application. Those are my aliases defined in tsconfig.json "paths": { "@core/*": ["app/core/*"], "@shared/*": ["app/shared/*"], "@state/*": ["app/state/*"] } Then in Core folder I'm reexporting all services in index file ( app/core/index.ts ) export * from './sse/sse.service'; export * from './rack/rack.service'; Now when I'm injecting the service in a constructor the

Jest unit test: setTimeout not firing in async test

牧云@^-^@ 提交于 2021-01-27 06:45:32
问题 I'm trying to understand how asynchronous testing works in Jest. What I'm trying to do is similar to an example from the Jest documentation. This works fine .. function doAsync(c) { c(true) } test('doAsync calls both callbacks', () => { expect.assertions(2); function callback1(data) { expect(data).toBeTruthy(); } function callback2(data) { expect(data).toBeTruthy(); } doAsync(callback1); doAsync(callback2); }); But I want to delay the callback invocations so I tried this .... function doAsync

Jest mockResolvedValue is not a function

匆匆过客 提交于 2021-01-27 06:06:37
问题 I'm getting an error mocking my api call TypeError: Cannot read property 'mockResolvedValue" of undefined and cannot figure out why. I'm utilizing jest to test my api fetch call function. This is my function I'm exporting: //file amData.jsx const axios = require('axios'); async function fetchAssetManagerSummary() { const response = await axios.get("https://fr-assetmanager.azurewebsites.net/File/GetOverview?runDat=04/01/2020"); return response.data; } module.exports = fetchAssetManagerSummary;

Jest mockResolvedValue is not a function

痞子三分冷 提交于 2021-01-27 06:04:38
问题 I'm getting an error mocking my api call TypeError: Cannot read property 'mockResolvedValue" of undefined and cannot figure out why. I'm utilizing jest to test my api fetch call function. This is my function I'm exporting: //file amData.jsx const axios = require('axios'); async function fetchAssetManagerSummary() { const response = await axios.get("https://fr-assetmanager.azurewebsites.net/File/GetOverview?runDat=04/01/2020"); return response.data; } module.exports = fetchAssetManagerSummary;

“SyntaxError: Cannot use import statement outside a module” with Babel, Jest, and webpack

北城以北 提交于 2021-01-27 05:42:16
问题 I have a problem with configurations of Babel 7, Webpack 4 and Jest. While I'm running tests I'm getting following error: SyntaxError: Cannot use import statement outside a module package.json "@babel/core": "^7.7.5", "@babel/highlight": "^7.8.3", "@babel/plugin-syntax-dynamic-import": "^7.8.3", "@babel/preset-env": "^7.8.4", "babel-core": "^7.0.0-bridge.0", "babel-loader": "^7.1.5", "@babel/plugin-transform-runtime": "^7.7.4", "@babel/preset-react": "^7.7.4", "@babel/runtime": "^7.8.4",

Simulate select with element-ui and vue-test-utils

放肆的年华 提交于 2021-01-27 04:00:19
问题 I'm doing unit tests with Jest and Element-ui in Vue on a component which contains a select with 2 options. I am selecting an option from a dropdown and then checking that an action has been called. 1) With normal select and option HTML tags this works perfectly. // Fruit.vue <template lang="pug"> select( v-model="fruit" ) option( v-for="item in fruits" :label="item.label" :value="item.value" ) </template> <script> export default { data () { return { fruits: [ { label: 'Banana', value: false

How to test useRef with Jest and react-testing-library?

落爺英雄遲暮 提交于 2021-01-27 03:54:17
问题 I'm using create-react-app, Jest and react-testing-library for the configuration of the chatbot project. I have a functional component that uses useRef hook. When a new message comes useEffect hook is triggered and cause scrolling event by looking a ref's current property. const ChatBot = () => { const chatBotMessagesRef = useRef(null) const chatBotContext = useContext(ChatBotContext) const { chat, typing } = chatBotContext useEffect(() => { if (typeof chatMessagesRef.current.scrollTo !==

Mock only one function from module but leave rest with original functionality

懵懂的女人 提交于 2021-01-26 23:10:58
问题 I only want to mock a single function (named export) from a module but leave the rest of the module functions intact. Using jest.mock('package-name') makes all exported functions mocks, which I don't want. I tried spreading the named exports back into the mock object... import * as utils from './utilities.js'; jest.mock(utils, () => ({ ...utils speak: jest.fn(), })); but got this error: The module factory of jest.mock() is not allowed to reference any out-of-scope variables. 回答1: The

Mock only one function from module but leave rest with original functionality

北城以北 提交于 2021-01-26 23:06:43
问题 I only want to mock a single function (named export) from a module but leave the rest of the module functions intact. Using jest.mock('package-name') makes all exported functions mocks, which I don't want. I tried spreading the named exports back into the mock object... import * as utils from './utilities.js'; jest.mock(utils, () => ({ ...utils speak: jest.fn(), })); but got this error: The module factory of jest.mock() is not allowed to reference any out-of-scope variables. 回答1: The