Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k views
in Technique[技术] by (71.8m points)

jestjs - Cannot mock fs with jest using typescript - Property 'mockReturnValue' does not exist on type

Problem:

Would someone help me figure out how to mock fs with jest using typescript? I've tried a few things, and here is the main one:

I'm attempting to use jest to mock 'fs', but I can't seem to get jest to automock the 'fs' library in Typescript.

Here is my code:

import fs from 'fs';

jest.mock('fs');

describe('helloWorld.ts', () => {
  it('foobar', () => {

    fs.readdirSync.mockReturnValue(['foo.js', 'bar.js']);
  })
});

Typescript tells me "Property 'mockReturnValue' does not exist on type..."

cannot mock fs with jest

Environment:

Node v14.15.1
Typescript: "^4.0.3"
VS Code Typescript: 4.1.2

On a related note, I tried this with spyOn and it failed:

I tried to use this but couldn't get spyOn to work either (reference: jest typescript property mock does not exist on type )

import fs from 'fs';

describe('helloWorld.ts', () => {
  it('foobar', () => {
    jest.spyOn(fs, 'readdirSync').mockImplementation(() => {
      return ['foo.js', 'bar.js'];
    });
    console.log(fs.readdirSync('.'));
  });
});

This code fails with this typescript error TS2345:

Argument of type '() => string[]' is not assignable to parameter of type '(path: PathLike, options: BaseEncodingOptions & { withFileTypes: true; }) => Dirent[]'.
      Type 'string[]' is not assignable to type 'Dirent[]'.
        Type 'string' is not assignable to type 'Dirent'.

Related references:

question from:https://stackoverflow.com/questions/65891484/cannot-mock-fs-with-jest-using-typescript-property-mockreturnvalue-does-not

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The TypeScript compiler does not know anything about fs being a mock.

You can tell it about this by using a type assertion:

(<jest.Mock>fs.readdirSync).mockReturnValue(...);

It becomes tedious work to do this everytime you use mocked functions imported from the fs module. To make the things simpler you can declare a variable whose type is a module mock, initialize it with fs and use it instead of fs in the tests:

import fs from 'fs';

jest.mock('fs');

const mockFS: jest.Mocked<typeof fs> = <jest.Mocked<typeof fs>>fs;

describe('helloWorld.ts', () => {
  it('foobar', () => {

    mockFS.readdirSync.mockReturnValue(['foo.js', 'bar.js']);
  });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...