GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Mocking of fs module in Jest broken in 10.5.0 · Issue #21489 · nodejs/node

Mocking of fs module in Jest broken in 10.5.0 #21489

New issue
Copy link
New issue
Copy link
Closed
Closed
Mocking of fs module in Jest broken in 10.5.0#21489
Copy link
Labels
fsIssues and PRs related to file-system APIs and the fs module.

Description

@SimenB
SimenB
opened on Jun 23, 2018
Issue body actions
  • Version: v10.5.0
  • Platform: Darwin Simens-MBP 17.4.0 Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64 x86_64
  • Subsystem: fs

With the following file:

jest.mock('fs');

const mockedFs = require('fs');

console.log(typeof mockedFs.ReadStream);
console.log(typeof mockedFs.WriteStream);

test('empty test to have Jest run the file', () => {
  expect(true).toBe(true);
});

It prints the following in Node 10.4.1:

  ● Console

    console.log test.js:5
      function
    console.log test.js:6
      function

And the following in Node 10.5.0:

  ● Console

    console.log test.js:5
      undefined
    console.log test.js:6
      undefined

More concretely, the following is missing (File){Read,Write}Stream in 10.5.0, while they're present in 10.4.1:

const fs = require('fs');
const jestMock = require('jest-mock');

const {members: mockMembers} = jestMock.getMetadata(fs);

console.log(mockMembers);

Source of that function: https://github.com/facebook/jest/blob/24df0751a818e570fe74dd0d73285a63e322d37f/packages/jest-mock/src/index.js#L642-L723

Probably related to #20764.

I'll add that this should probably be addressed in Jest, but this is potentially pretty breaking for consumers of Jest (of which not everybody will upgrade to the latest version).

In particular, graceful-fs completely breaks if required after a jest.mock('fs');

jest.mock('fs');

require('graceful-fs');

Explodes with

    TypeError: Cannot read property 'prototype' of undefined

      1 | jest.mock('fs');
      2 |
    > 3 | require('graceful-fs');
      4 |

      at patch (node_modules/graceful-fs/graceful-fs.js:166:54)
      at Object.<anonymous> (ugh.test.js:3:1)

Activity

  1. vitkarpov commented on Jun 23, 2018

    @vitkarpov
    vitkarpov
    on Jun 23, 2018
    Contributor
    More actions

    Seems in v10.5.0 ReadStream and WriteStream became getters with lazy loading instead of regular properties.

    In Jest getSlots method passes getters, that's why this props are not included eventually. @SimenB Probably you know the reason why getters not included in the first place.

  2. addaleax
    added
    fsIssues and PRs related to file-system APIs and the fs module.
    on Jun 23, 2018
  3. SimenB commented on Jun 30, 2018

    @SimenB
    SimenB
    on Jun 30, 2018
    MemberAuthor
    More actions

    @jasnell Sorry to ping you directly, but are there any plans for a fix here?

  4. jasnell commented on Jul 12, 2018

    @jasnell
    jasnell
    on Jul 12, 2018
    Member
    More actions

    @SimenB ... my apologies for just seeing this, I've been on vacation and have a serious backlog of issues to catch up on. It appears that @targos has a potential fix in the works.

    Reacted by Simen Bekkhus
  5. SimenB commented on Jul 13, 2018

    @SimenB
    SimenB
    on Jul 13, 2018
    MemberAuthor
    More actions

    Dug into why we don't include accessors, and it seems to be by design: jestjs/jest#390

    I'll try to ask if it's still needed

  6. targos commented on Jul 13, 2018

    @targos
    targos
    on Jul 13, 2018
    Member
    More actions

    Our accessors are enumerable:

    > Object.getOwnPropertyDescriptor(fs, 'ReadStream')
    { get: [Function: get ReadStream],
      set: [Function: set ReadStream],
      enumerable: true,
      configurable: true }
    
  7. SimenB commented on Jul 13, 2018

    @SimenB
    SimenB
    on Jul 13, 2018
    MemberAuthor
    More actions

    Yeah, the PR is poorly named. If you look at the code it uses Object.getOwnPropertyDescriptor and bails if it's a getter, it doesn't actually care about enumerable if it's a getter, which isn't mentioned.

  8. vitkarpov commented on Jul 13, 2018

    @vitkarpov
    vitkarpov
    on Jul 13, 2018
    Contributor
    More actions

    And now enumerability check is gone but it was there in the first place 🤔 Seems like unexpected disappearing to me.

  9. SimenB commented on Jul 13, 2018

    @SimenB
    SimenB
    on Jul 13, 2018
    MemberAuthor
    More actions

    Good point, it would've worked if we'd have kept the enumerable check, I read that too quickly...

  10. SimenB commented on Jul 13, 2018

    @SimenB
    SimenB
    on Jul 13, 2018
    MemberAuthor
    More actions

    jestjs/jest#409, removed without any context provided... I've asked, but haven't received a response yet

  11. vitkarpov commented on Jul 13, 2018

    @vitkarpov
    vitkarpov
    on Jul 13, 2018
    Contributor
    More actions

    @SimenB I assume you've asked within some private channel, could you, please, provide an update here? I'm intrigued 😃

  12. targos
    added 2 commits that reference this issue on Jul 14, 2018

    fs: stop lazy loading stream constructors

    fbc41bb

    fs: stop lazy loading stream constructors

    484140e
  13. targos commented on Jul 19, 2018

    @targos
    targos
    on Jul 19, 2018
    Member
    More actions

    The breaking change is reverted on Node 10 (will be released with the next version)

    Reacted by Simen Bekkhus
  14. targos
    closed this as completedon Jul 19, 2018
  15. ckerr
    added a commit that references this issue on Nov 21, 2018

    fix: revert "fs: stop lazy loading stream constructors"

    1e7f829
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to file-system APIs and the fs module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.