Skip to content

refactor: allow passing recursive partials in createFakePartial

Elwyn Benson requested to merge deep-partial-mocks into main

Description

The new createFakePartial utility allows easily creating complex mocks, however only works on a single level. This means for mocks with nested objects, you must define the nested objects fully, or use multiple createFakePartials:

      const mockLine = createFakePartial<vscode.TextLine>({
        lineNumber: 1,
        range: createFakePartial<vscode.Range>({
          start: createFakePartial<vscode.Position>({
            character: 1
          }),
        }),
      });

This MR updated createFakePartial to use a DeepPartial implementation, so you can always pass partials of nested objects:

      const mockLine = createFakePartial<vscode.TextLine>({
        lineNumber: 1,
        range: {
          start: {
            character: 1
          },
        },
      });

The nested objects are still type-safe partials.

I yoinked the DeepPartial type from the utility-types project: https://github.com/piotrwitek/utility-types/blob/master/src/mapped-types.ts#L504 as it handled arrays and more complex situations than some of the simple DeepPartial implementations on StackOverflow.

Related Issues

Resolves #1078 (closed)

How has this been tested?

Building and testing locally:

npm run build:browser
npm run build:desktop
npm run test

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation
  • Chore (Related to CI or Packaging to platforms)
  • Test gap

None of the above, [x] refactor (non-breaking / non-functional change)

Edited by Elwyn Benson

Merge request reports