jest icon indicating copy to clipboard operation
jest copied to clipboard

automock is crashing

Open Alexsey opened this issue 5 years ago • 10 comments

🐛 Bug Report

When setting in jest.config.js property automock: true the tests run is crashing with error

>npx jest
 FAIL  ./index.test.js
  ×  (30ms)

  ● 

    TypeError: Cannot destructure property 'formatters' of 'module.exports' as it is undefined.

    > 1 | require('dotenv-parse-variables')
        | ^
      2 | 

      at Object.<anonymous> (node_modules/debug/src/node.js:238:8)
      at Object.<anonymous> (index.js:1:1)
      at Object.<anonymous> (index.test.js:4:3)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        2.893s, estimated 4s
Ran all test suites.

To Reproduce

Consider two files: index.js

require('dotenv-parse-variables')

index.test.js

jest.unmock('./index')

test('', () => {
  require('./index')
})

Steps to reproduce the behavior: run npx jest

Expected behavior

Jest should not crash

Link to repl or repo (highly encouraged)

https://github.com/Alexsey/jest-automock-crash

envinfo

>npx envinfo --preset jest
npx: installed 1 in 2.334s

  System:
    OS: Windows 10 10.0.18362
    CPU: (4) x64 Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz
  Binaries:
    Node: 13.7.0 - C:\Program Files\nodejs\node.EXE
    npm: 6.13.6 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: ^25.1.0 => 25.1.0

Alexsey avatar Feb 15 '20 20:02 Alexsey

I was able to reproduce this issue and appears the problem is with mocking modules that return a method.

Here is a simple example:

//my-module
function setup() {
    return {
        stuff: 'it'
    };
}

module.exports = setup;

With mocking, require('./my-module')() will return undefined.

That specific issue is from the debug package internally expecting a specific object after calling the method from the export.

carsonreinke avatar Jul 10 '20 19:07 carsonreinke

I'm also seeing this issue from the debug package. Do you know of any fix or workaround?

aMoniker avatar Mar 08 '21 07:03 aMoniker

I'm having the same error when using automock true. Is this still an unresolved issue?

MuriloEduardo avatar Jun 28 '21 13:06 MuriloEduardo

this is actually a major issue - i've just been spending hours trying to figure out why automocking is failing using jest.mock('my_node_module_name');

when automocking a class accessing ClassName.mock.instances[0] returns an empty object.

I tried debugging by creating a custom mock in mocks with the same name as the node_modules library and im receiving TypeError: Cannot destructure property 'formatters' of 'module.exports' as it is undefined. error.

It seems that these might be related?

mattjbrent avatar Aug 31 '21 20:08 mattjbrent

i didnt receive this error until I added automock: true to jest config json file as files in mocks werent creating an automock - they were failing silently.

This hasnt been picked up in months and renders this package pretty defunct for our integration tests. Any news on an update or fix?

mattjbrent avatar Aug 31 '21 20:08 mattjbrent

looks like this links to #6127

mattjbrent avatar Aug 31 '21 20:08 mattjbrent

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Feb 17 '23 16:02 github-actions[bot]

Still relevant

Alexsey avatar Feb 17 '23 17:02 Alexsey

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Feb 17 '24 18:02 github-actions[bot]

Still relevant

Alexsey avatar Feb 17 '24 18:02 Alexsey