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

Issue with fs module mock #1645

Closed
6 tasks done
mrmckeb opened this issue Jul 15, 2022 · 2 comments · Fixed by #1726
Closed
6 tasks done

Issue with fs module mock #1645

mrmckeb opened this issue Jul 15, 2022 · 2 comments · Fixed by #1726

Comments

@mrmckeb
Copy link

mrmckeb commented Jul 15, 2022

Describe the bug

When trying to mock fs (or node:fs), I'm only able to mock the return value or mock implementation under certain conditions.

In the below reproduction, switching the order of the two tests actually makes the tests work.

Note that this project is set to ESM, via "type": "module" in package.json.

Reproduction

File:

import { readFileSync } from 'node:fs';

export const readFile = () => {
  try {
    return readFileSync('some-file', 'utf-8');
  } catch (e) {
    return '';
  }
};

Test:

import { readFileSync } from 'node:fs';
import { afterEach, describe, expect, it, vi } from 'vitest';

vi.mock('node:fs', () => ({
  readFileSync: vi.fn().mockImplementation(() => ''),
}));

const mockReadFileSync = vi.mocked(readFileSync);

describe('env', () => {
  afterEach(() => {
    vi.resetModules();
  });

  it('should handle a missing file', async () => {
    mockReadFileSync.mockImplementation(() => {
      throw new Error('Some error');
    });
    const { readFile } = await import('./repro');
    expect(readFile()).toBe('');
  });

  it('should read a file', async () => {
    mockReadFileSync.mockImplementation(() => 'Some contents');
    const { readFile } = await import('./repro');
    expect(readFile()).toBe('Some contents');
  });
});

Config:

import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    mockReset: true,
  },
});

System Info

System:
    OS: macOS 12.4
    CPU: (8) arm64 Apple M1
    Memory: 177.42 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.6.0 - ~/Library/Caches/fnm_multishells/11869_1657854968244/bin/node
    npm: 8.13.2 - ~/Library/Caches/fnm_multishells/11869_1657854968244/bin/npm
  Browsers:
    Chrome: 103.0.5060.114
    Edge: 103.0.1264.62
    Firefox: 102.0.1
    Safari: 15.5
  npmPackages:
    vitest: ^0.18.0 => 0.18.0

Used Package Manager

pnpm

Validations

@sheremet-va
Copy link
Member

vi.resetModules removes mocks. This is bug with resetModules, and not with ESM mocking. Importing fs has nothing to do with ESM.

@mrmckeb
Copy link
Author

mrmckeb commented Aug 1, 2022

Thank you!

@github-actions github-actions bot locked and limited conversation to collaborators Jun 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants