Ts-jest: Cannot destructure property `Menu` of 'undefined' or 'null'

Created on 13 Feb 2020  路  12Comments  路  Source: kulshekhar/ts-jest

I using Vue +Typescript + Electron

In App.ts file:

import { Vue, Component, Watch } from 'vue-property-decorator'
import { remote } from 'electron'

const { Menu } = remote

I get this error after jest running

TypeError: Cannot destructure property "Menu" of "undefined" or "null"

All 12 comments

hi, would you please provide us a minimum reproducible repo ?

hi, would you please provide us a minimum reproducible repo ?

https://github.com/jimhucksly/issues-1387-repoduce

hi, a few things you can do to improve your project when using typescript and ts-jest:

  • Make your tests file in typescript, for example App.spec.js can changed to App.spec.ts.
  • Your jest config in package.json can be improved, for example:
"preset": "ts-jest",
 "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "vue"
],
"transform": {
      "^.+\\.tsx?$": "ts-jest",
      "^.+\\.js$": "babel-jest",
      ".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
}
  • Your package.json needs 2 new dependencies ts-jest and @types/jest.
  • Delete App.js.
  • Your test file contains error too, it should be like this:
import { shallowMount } from '@vue/test-utils'
import App from '../src/App'

describe('App.js', () => {
  test('render App', () => {
    const wrapper = shallowMount(App)
    expect(wrapper.text()).toEqual(<something_here>)
  })
})

and regarding to your issue with electron in your App.ts, the import path isn't correct. It needs to be import remote from 'electron'.

import remote from 'electron'

const { Menu } = remote

@ahnpnl repo updated, problem is not solved

import like:
import remote from 'electron'
is not correct! We need imports remote as module of electron, but not the Electron itself

what is the issue when using import remote from 'electron' ? I'm not so familiar with electron but using that way solves the issue with jest. The error you got is from typescript compiler. It can't recognize import { remote } from 'electron' therefore it throws the error.

You can reproduce the behavior of typescript compiler by doing these following steps:

  • Compile App.ts to js file (I guess by running yarn build)
  • Run node src/App.js and you will get the same error.

When jest and ts-jest run, the same behavior occurs, first compiling ts to js (similar to yarn build) then typescript will run check on the code (similar to node src/App.js)

Problem is not with typescript.

Run node src/App.js and you will get the same error.

You can't run it like this
For work of Imports we need use babel, but it doesn't start here

you can also try to install ts-node, then run yarn ts-node src/App.ts and print out remote, you will see the result undefined. I don't know why it is undefined but something is missing which makes remote import is undefined.

and if you use syntax import remote from 'electron' gives the result
node_modules/electron/dist/Electron.app/Contents/MacOS/Electron

Cannot read property 'dispatch' of undefined

mounted() {
    > 28 |     this.$store.dispatch(...)

Jest is wonderful

Happy testing 馃榾

so what was the problem?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

remcohaszing picture remcohaszing  路  4Comments

bySabi picture bySabi  路  4Comments

ahnpnl picture ahnpnl  路  3Comments

stangerjm picture stangerjm  路  4Comments

GeeWee picture GeeWee  路  4Comments