jestjs

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

谁说我不能喝 提交于 2020-12-13 10:36:45
问题 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 {

How to test axios with config option

喜夏-厌秋 提交于 2020-12-13 03:30:40
问题 I'm trying to test the below axios request written for both put/post as a config option: export function saveCourse(course){ const config = { method: course.id ? 'PUT' : 'POST',// POST for create, PUT to update when id already exists. url: baseUrl + (course.id || ''), headers: { "content-type": "application/json" }, data: course } return axios(config) .then((res) => { if(res.status === 201 || res.status === 200) return res.data; if(res.status === 400){ const error = res.text(); throw new

Test on function call in Vue template only passes if the function is called with parentheses

这一生的挚爱 提交于 2020-12-12 11:28:32
问题 I am using Vue v2.6 with Jest (v24.9) and Vue-Test-Utils (v1.03). In order to mock a method I have seen two different syntaxes, wrapper.vm.updateCart = jest.fn(); and wrapper.setMethods({ updateCart: jest.fn() }); but the first is not documented, while the second is deprecated (see docs). The problem is that with both these methods, the only way to make a test pass is to call the method with the parentheses inside the template, which is weird because all the docs I've read somehow encourage

How to mock jQuery .done() so it executes correctly with Jest?

前提是你 提交于 2020-12-12 10:59:28
问题 I'm trying to write a unit-test for a password changing React-module, but i can't get the code to be executed in brackets. I've written a mock for the module MyAPI, the mock code is executed just fine and with console.log("something") i can see the output in console. However, i can't get the code to run after the .done(function (data). It's most likely because the mock is replacing those with it's own code. I know one option is to use a fake server like Nock, but I wouldn't want to turn it

How to trace a failed asynchronous test in Jest?

一世执手 提交于 2020-12-12 06:20:23
问题 - UPDATE - The issue has been identified. In the actual codebase the assertion is passed to a imported callback, and once the callback executes with a failed test, it raises a promise rejection. So, this is close to how the test was actually written: describe( "file system", () => { it( "should check if the file exists", async () => { call( async () => { const received = await fileExists(); const expected = true; expect( received ).toBe( expected ); }); }); }); and the complex callback is

How to trace a failed asynchronous test in Jest?

扶醉桌前 提交于 2020-12-12 06:17:31
问题 - UPDATE - The issue has been identified. In the actual codebase the assertion is passed to a imported callback, and once the callback executes with a failed test, it raises a promise rejection. So, this is close to how the test was actually written: describe( "file system", () => { it( "should check if the file exists", async () => { call( async () => { const received = await fileExists(); const expected = true; expect( received ).toBe( expected ); }); }); }); and the complex callback is

Jest - Testing modals in React gives error

旧街凉风 提交于 2020-12-12 05:27:16
问题 I am using react-test-renderer with Jest to test react components. But if I test a react-mui modal dialog like this: describe('Dashboard', function () { let dashboard; beforeEach(async () => { testRenderer = TestRenderer.create(<MemoryRouter><Route component={Dashboard} /></MemoryRouter>); dashboard = testRenderer.root.findByType(Dashboard); await waitForExpect(() => expect(dashboard.instance.state.hasLoaded).toBeTruthy()); }); it('opens dialog on clicking the new class', async () => { const

Jest and SCSS variables

一曲冷凌霜 提交于 2020-12-12 01:08:01
问题 I'm using Jest in my project and got stuck on one test which contains SCSS variable. $grey-light: #C7C7C7; I get this Jest error: Jest encountered an unexpected token and it points on the previous mentioned line of code. 回答1: I ran into this as well and it was solved by using the identity-obj-proxy package: https://github.com/keyz/identity-obj-proxy Just follow the instructions in the Jest docs: https://jestjs.io/docs/en/webpack#mocking-css-modules And should run the tests with no issues

Jest and SCSS variables

橙三吉。 提交于 2020-12-12 01:07:32
问题 I'm using Jest in my project and got stuck on one test which contains SCSS variable. $grey-light: #C7C7C7; I get this Jest error: Jest encountered an unexpected token and it points on the previous mentioned line of code. 回答1: I ran into this as well and it was solved by using the identity-obj-proxy package: https://github.com/keyz/identity-obj-proxy Just follow the instructions in the Jest docs: https://jestjs.io/docs/en/webpack#mocking-css-modules And should run the tests with no issues

import with jest error: Unexpected token import

怎甘沉沦 提交于 2020-12-10 10:45:43
问题 I've seen similar questions but still can't find a viable solution. I'm trying to integrate Jest into a working project, which uses import/export default in hundreds of places. The following test does work for Jest using require: const bar = require('../../flows/foo'); test('adds 1 + 2 to equal 3', () => { expect(bar.foobar(1, 2)).toBe(3); }); when export is: module.exports = { foobar: foobar, fizz: fizz } The functions I'll want to be testing however are exported using: export default {