question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async await not working while mocking

See original GitHub issue

Current behavior

My componentDidMount is an async function that looks like this

 async componentDidMount() {
    await this.runBeforeStart();
    await this.restoreState();
    this.prepareIntents(this.state.customMeta.intents)
  }

runBeforeStart = async () => {
    let { state, props } = this;
}

the test code looks like this

import { mount, shallow } from 'enzyme';
import App from './App';
import React from 'react';

describe('Render the right options', () => {
    const getWrapper = (props) => {
        return mount(<App {...props} />)
    }
    it('should render', async () => {

        let wrapper = getWrapper({ id: 1 });
    })
})

and when i run the test I get this error,

● Render the right options › should render

TypeError: Cannot read property 'Symbol(Symbol.iterator)' of object

  240 |   }
  241 | 
> 242 |   runBeforeStart = async () => {
      |                            ^
  243 |     let { state, props } = this;
  244 | 
  245 |     let before_starts = props.customMeta.before_start;

  at _callee5$ (src/App.js:242:28)
  at tryCatch (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:45:40)
  at Generator.invoke [as _invoke] (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:271:22)
  at Generator.prototype.(anonymous function) [as next] (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:97:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
  at node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
  at App.runBeforeStartFunction (node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12)
  at App.runBeforeStartFunction (src/App.js:322:16)
  at tryCatch (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:45:40)
  at Generator.invoke [as _invoke] (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:271:22)
  at Generator.prototype.(anonymous function) [as next] (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:97:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
  at node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
  at App.<anonymous> (node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12)
  at App.componentDidMount (src/App.js:2752:35)
  at commitLifeCycles (node_modules/react-dom/cjs/react-dom.development.js:17130:22)
  at commitAllLifeCycles (node_modules/react-dom/cjs/react-dom.development.js:18532:7)
  at HTMLUnknownElement.callCallback (node_modules/react-dom/cjs/react-dom.development.js:149:14)
  at invokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:193:27)
  at HTMLUnknownElementImpl._dispatch (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:119:9)
  at HTMLUnknownElementImpl.dispatchEvent (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:82:17)
  at HTMLUnknownElementImpl.dispatchEvent (node_modules/jsdom/lib/jsdom/living/nodes/HTMLElement-impl.js:30:27)
  at HTMLUnknownElement.dispatchEvent (node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:157:21)
  at Object.invokeGuardedCallbackDev (node_modules/react-dom/cjs/react-dom.development.js:199:16)
  at invokeGuardedCallback (node_modules/react-dom/cjs/react-dom.development.js:256:31)
  at commitRoot (node_modules/react-dom/cjs/react-dom.development.js:18744:7)
  at node_modules/react-dom/cjs/react-dom.development.js:20214:5
  at Object.unstable_runWithPriority (node_modules/react-dom/node_modules/scheduler/cjs/scheduler.development.js:255:12)
  at completeRoot (node_modules/react-dom/cjs/react-dom.development.js:20213:13)
  at performWorkOnRoot (node_modules/react-dom/cjs/react-dom.development.js:20142:9)
  at requestWork (node_modules/react-dom/cjs/react-dom.development.js:19886:7)
  at scheduleWork (node_modules/react-dom/cjs/react-dom.development.js:19707:5)
  at scheduleRootUpdate (node_modules/react-dom/cjs/react-dom.development.js:20368:3)
  at updateContainerAtExpirationTime (node_modules/react-dom/cjs/react-dom.development.js:20396:10)
  at updateContainer (node_modules/react-dom/cjs/react-dom.development.js:20453:10)
  at ReactRoot.Object.<anonymous>.ReactRoot.render (node_modules/react-dom/cjs/react-dom.development.js:20749:3)
  at node_modules/react-dom/cjs/react-dom.development.js:20886:14
  at unbatchedUpdates (node_modules/react-dom/cjs/react-dom.development.js:20250:14)
  at legacyRenderSubtreeIntoContainer (node_modules/react-dom/cjs/react-dom.development.js:20882:5)
  at Object.render (node_modules/react-dom/cjs/react-dom.development.js:20951:12)
  at render (node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:418:26)
  at fn (node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:342:37)
  at Object.batchedUpdates$1 [as unstable_batchedUpdates] (node_modules/react-dom/cjs/react-dom.development.js:20235:12)
  at Object.act (node_modules/react-dom/cjs/react-dom-test-utils.development.js:1161:27)
  at act (node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:342:13)
  at Object.wrapAct [as render] (node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:404:16)
  at new ReactWrapper (node_modules/enzyme/build/ReactWrapper.js:134:16)
  at mount (node_modules/enzyme/build/mount.js:21:10)
  at getWrapper (src/App.test.js:23:16)
  at getWrapper (src/App.test.js:29:23)
  at tryCatch (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:45:40)
  at Generator.invoke [as _invoke] (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:271:22)
  at Generator.prototype.(anonymous function) [as next] (node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:97:21)
  at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
  at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
  at node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
  at Object.<anonymous> (node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12)

Expected behavior

I expect the error not to occur

Your environment

API

mount

Version

| library | version

| enzyme | enzyme@^3.9.0: | react | react@^16.8.0: | react-dom | react-dom@^16.8.0: | react-test-renderer | jest@^23.6.0: | adapter (below) | enzyme-adapter-react-16

Adapter

enzyme-adapter-react-16

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ljharbcommented, May 23, 2019

I’d never heard of react-unit; react-testing-library is just a thin wrapper around the DOM that React already renders, so it’d suffer from a Babel issue like anything else.

Certainly any use of eval is abjectly bad code, and you shouldn’t be disabling eslint for it - Babel can’t transpile code you send into eval.

I’m not familiar with razzle either, but it doesn’t seem related. I think it’s probably the eval.

1reaction
ljharbcommented, May 22, 2019

Please do provide the full, unredacted code - every single line is potentially needed to be able to understand what went wrong.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enzyme async await mock function not being called
Use an async test function and await the Promise returned by onSubmit so it is completely done before continuing.
Read more >
Async await not working while mocking · Issue #2130
The error is coming from Babel, not enzyme - something with its async/await transpilation. It will happen without enzyme as well. What does...
Read more >
Testing Asynchronous Code
When you have code that runs asynchronously, Jest needs to know when the code it is testing has completed, before it can move...
Read more >
Async waits in React Testing Library
Mocking an API. Presently, when we run our test, we are hitting the actual API endpoint and waiting for the API to be...
Read more >
Async Methods
The async methods return Promises, so be sure to use await or .then when calling them. findBy Queries​. findBy methods are a combination...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found