Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automock is crashing #9576

Open
Alexsey opened this issue Feb 15, 2020 · 10 comments
Open

automock is crashing #9576

Alexsey opened this issue Feb 15, 2020 · 10 comments

Comments

@Alexsey
Copy link

Alexsey commented Feb 15, 2020

πŸ› 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
@carsonreinke
Copy link

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.

@aMoniker
Copy link

aMoniker commented Mar 8, 2021

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

@MuriloEduardo
Copy link

MuriloEduardo commented Jun 28, 2021

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

@mattjbrent
Copy link

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
Copy link

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
Copy link

looks like this links to #6127

@github-actions
Copy link

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 github-actions bot added the Stale label Feb 17, 2023
@Alexsey
Copy link
Author

Alexsey commented Feb 17, 2023

Still relevant

@github-actions github-actions bot removed the Stale label Feb 17, 2023
Copy link

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 github-actions bot added the Stale label Feb 17, 2024
@Alexsey
Copy link
Author

Alexsey commented Feb 17, 2024

Still relevant

@github-actions github-actions bot removed the Stale label Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants