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

Mocking a Readonly type returns undefined #616

Open
jasonadkison opened this issue Feb 2, 2021 · 1 comment
Open

Mocking a Readonly type returns undefined #616

jasonadkison opened this issue Feb 2, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@jasonadkison
Copy link

jasonadkison commented Feb 2, 2021

Subject of the issue

When trying to create a mock for Readonly type the result is undefined

Your environment

  • ts-auto-mock version: 3.1.0
  • typescript version: 4.1.3
  • node version: 12.14.0
  • npm version: 6.13.4

Steps to reproduce

test('can mock Readonly type', () => {
  const mock = createMock<Readonly<{ foo: string }>>();
  expect(typeof mock.foo).toBe('string'); // TypeError: Cannot read property 'foo' of undefined
});
test('can mock Readonly type', () => {
  const mock = createMock<Readonly<{ foo: string }>>({ foo: 'bar' });
  expect(typeof mock.foo).toBe('string'); // This works because of the property overrides provided to `createMock`
});

Expected behavior

The Readonly mock should be created.

Actual behavior

The mock is created as undefined.

@Pmyl
Copy link
Collaborator

Pmyl commented Feb 3, 2021

Unfortunately we don't support mapped types as described here https://typescript-tdd.github.io/ts-auto-mock/types-not-supported#indexed-access-type-with-generics

We would like to but typescript doesn't have very good tools for us to understand the type. We're always keeping it under the radar but right now we can't do much.

To cope with that we created "createHydratedMock" https://typescript-tdd.github.io/ts-auto-mock/create-hydrated-mock

That works very well if you need the whole mock to be required

Let me know

@Pmyl Pmyl added the enhancement New feature or request label Jun 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants