【问题标题】:How to mock nanoid for testing?如何模拟 nanoid 进行测试?
【发布时间】:2021-08-26 02:37:58
【问题描述】:

我正在尝试模拟 nanoid 以进行测试,但它似乎不起作用。

我的功能

  public async createApp(appDto: ApplicationDto): Promise<string> {
    const appWithToken = { ...appDto, accessToken: nanoid() };
    const application = await this.applicationModel.create(appWithToken);

    return application.id;
  }

我的测试:

  beforeEach(() => {
    mockRepository.create.mockResolvedValueOnce({ id: mockId });
  });

  test("creates application and returns an id", async () => {
    const mockAppDto: ApplicationDto = { email: "123@mock.com" };
    const application = await applicationService.createApplication(mockAppDto);

    expect(mockRepository.create).toHaveBeenCalledWith(mockAppDto); //how do I mock the nanoid here?
    expect(application).toBe(mockId);
  });

所以基本上我正在努力弄清楚如何模拟函数内部生成的 nanoid。

我在文件顶部尝试了以下内容:

jest.mock('nanoid', () => 'mock id');

但它根本不起作用。

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript typescript jestjs mocking


    【解决方案1】:

    您没有正确模拟 nanoid 模块。它使用命名导出来导出nanoid 函数。

    使用jest.mock(moduleName, factory, options) 是正确的,factory 参数是可选的。它将创建一个模拟的nanoid 函数。

    此外,您可以使用ts-jest/utils 中的mocked 函数来处理TS 类型。

    例如

    Example.ts:

    import { nanoid } from 'nanoid';
    
    export interface ApplicationDto {}
    
    export class Example {
      constructor(private applicationModel) {}
    
      public async createApp(appDto: ApplicationDto): Promise<string> {
        const appWithToken = { ...appDto, accessToken: nanoid() };
        const application = await this.applicationModel.create(appWithToken);
    
        return application.id;
      }
    }
    

    Example.test.ts:

    import { nanoid } from 'nanoid';
    import { Example, ApplicationDto } from './Example';
    import { mocked } from 'ts-jest/utils';
    
    jest.mock('nanoid');
    
    const mnanoid = mocked(nanoid);
    
    describe('67898249', () => {
      afterAll(() => {
        jest.resetAllMocks();
      });
      it('should pass', async () => {
        mnanoid.mockReturnValueOnce('mock id');
        const mockAppDto: ApplicationDto = { email: '123@mock.com' };
        const mockApplicationModel = { create: jest.fn().mockReturnValueOnce({ id: 1 }) };
        const example = new Example(mockApplicationModel);
        const actual = await example.createApp(mockAppDto);
        expect(actual).toEqual(1);
        expect(mockApplicationModel.create).toBeCalledWith({ email: '123@mock.com', accessToken: 'mock id' });
      });
    });
    

    测试结果:

     PASS  examples/67898249/Example.test.ts (9.134 s)
      67898249
        ✓ should pass (4 ms)
    
    ------------|---------|----------|---------|---------|-------------------
    File        | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ------------|---------|----------|---------|---------|-------------------
    All files   |     100 |      100 |     100 |     100 |                   
     Example.ts |     100 |      100 |     100 |     100 |                   
    ------------|---------|----------|---------|---------|-------------------
    Test Suites: 1 passed, 1 total
    Tests:       1 passed, 1 total
    Snapshots:   0 total
    Time:        10.1 s
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 2020-02-28
      • 2018-04-04
      • 2017-04-27
      • 2021-09-27
      相关资源
      最近更新 更多