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

mock set to null when interface and namespace have the same name #792

Open
egriff38 opened this issue Jun 17, 2021 · 14 comments
Open

mock set to null when interface and namespace have the same name #792

egriff38 opened this issue Jun 17, 2021 · 14 comments
Labels
bug Something isn't working

Comments

@egriff38
Copy link

Subject of the issue

When there is a namespace and an interface with the same name, with the namespace declared first, mocking that interface returns null.

Your environment

  • ts-auto-mock version: 3.2.2
  • typescript version: 4.3.2
  • node version: 14.3.0
  • npm version: 7.6.0
  • list any other dependencies that you think are relevant
    • though it is probably irrelevant to the example, I am using jest-ts-auto-mock v2.0.0

Steps to reproduce

import {createMock} from 'ts-auto-mock'
namespace B {}
interface B {
  b: string
}
console.log(createMock<B>())

Expected behavior

Should log {b: ""}

Actual behavior

Actually logs null

@egriff38
Copy link
Author

This is true of exported namespaces/interfaces as well, and I also noticed that creating a copy of the type: type B2 = {[b in keyof B]: B[b]} creates a mock labeled with { [Symbol(__marker)]: true }

Thanks in advance!!

@Pmyl
Copy link
Collaborator

Pmyl commented Jun 17, 2021

I confirmed the bug, I'll take care of it!

The last thing you said is normal, for the transformer B is an empty object so that generates an empty object (plus a marker for mocks to work that won't impact any runtime execution)

@Pmyl
Copy link
Collaborator

Pmyl commented Jun 17, 2021

PR open to fix it #793 now waiting for @uittorio to review it, in one day max it should go in 😄

@Pmyl Pmyl added the bug Something isn't working label Jun 17, 2021
@Pmyl
Copy link
Collaborator

Pmyl commented Jun 17, 2021

@egriff38 new version released, let me know if that fixes the issue!

@egriff38
Copy link
Author

So I'm seeing a different issue now. I use Wallaby.js for testing, and for all of the tests I am now seeing the following error generated in the console:

Runtime error: TypeError: Cannot read property 'declarations' of undefined 
  at Object.t.GetTypeParameterDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:49554) 
  at Object.t.GetDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:13939) 
  at Object.t.GetTypeReferenceDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:53964) 
  at Object.t.GetDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:12964) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:56503 
  at Array.forEach (<anonymous>) 
  at Object.addFromTypeReferenceNode (./node_modules/ts-auto-mock/transformer/index.js:1:56452) 
  at f (./node_modules/ts-auto-mock/transformer/index.js:1:77392) 
  at Object.t.CreateMockFactory (./node_modules/ts-auto-mock/transformer/index.js:1:78001) 
  at Object.t.GetTypeReferenceDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:53937) 
  at Object.t.GetDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:12964) 
  at Object.t.GetPropertyDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:41654) 
  at Object.t.GetDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:13351) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:33699 
  at Array.reduce (<anonymous>) 
  at Object.t.GetMockPropertiesAssignments (./node_modules/ts-auto-mock/transformer/index.js:1:33669) 
  at Object.d [as GetMockPropertiesFromDeclarations] (./node_modules/ts-auto-mock/transformer/index.js:1:32493) 
  at Object.t.GetMappedDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:26025) 
  at Object.t.GetDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:14958) 
  at Object.t.GetTypeAliasDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:47446) 
  at Object.t.GetDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:12895) 
  at e.createMockFactory (./node_modules/ts-auto-mock/transformer/index.js:1:69499) 
  at Object.t.CreateMockFactory (./node_modules/ts-auto-mock/transformer/index.js:1:77978) 
  at Object.t.GetTypeReferenceDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:53937) 
  at Object.t.GetDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:12964) 
  at Object.t.GetPropertyDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:41654) 
  at Object.t.GetDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:13351) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:33699 
  at Array.reduce (<anonymous>) 
  at Object.t.GetMockPropertiesAssignments (./node_modules/ts-auto-mock/transformer/index.js:1:33669) 
  at d (./node_modules/ts-auto-mock/transformer/index.js:1:32493) 
  at Object.t.GetMockPropertiesFromSymbol (./node_modules/ts-auto-mock/transformer/index.js:1:32646) 
  at Object.t.GetProperties (./node_modules/ts-auto-mock/transformer/index.js:1:40911) 
  at Object.t.GetInterfaceDeclarationDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:22991) 
  at Object.t.GetDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:13113) 
  at e.createMockFactory (./node_modules/ts-auto-mock/transformer/index.js:1:69499) 
  at Object.t.CreateMockFactory (./node_modules/ts-auto-mock/transformer/index.js:1:77978) 
  at Object.t.GetTypeReferenceDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:53937) 
  at Object.t.GetDescriptor (./node_modules/ts-auto-mock/transformer/index.js:1:12964) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:63597 
  at Object.t.getMock (./node_modules/ts-auto-mock/transformer/index.js:1:63627) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:7785 
  at Object.run (./node_modules/ts-auto-mock/transformer/index.js:1:8701) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:6222 
  at m (./node_modules/ts-auto-mock/transformer/index.js:1:6232) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:6258 
  at visitNode (./node_modules/typescript/lib/typescript.js:83603:23) 
  at Object.visitEachChild (./node_modules/typescript/lib/typescript.js:84017:184) 
  at m (./node_modules/ts-auto-mock/transformer/index.js:1:5904) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:6258 
  at visitNode (./node_modules/typescript/lib/typescript.js:83603:23) 
  at Object.visitEachChild (./node_modules/typescript/lib/typescript.js:84062:64) 
  at m (./node_modules/ts-auto-mock/transformer/index.js:1:5904) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:6258 
  at visitNodes (./node_modules/typescript/lib/typescript.js:83656:48) 
  at Object.visitEachChild (./node_modules/typescript/lib/typescript.js:84056:50) 
  at m (./node_modules/ts-auto-mock/transformer/index.js:1:5904) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:6258 
  at visitNode (./node_modules/typescript/lib/typescript.js:83603:23) 
  at visitFunctionBody (./node_modules/typescript/lib/typescript.js:83774:23) 
  at Object.visitEachChild (./node_modules/typescript/lib/typescript.js:83996:389) 
  at m (./node_modules/ts-auto-mock/transformer/index.js:1:5904) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:6258 
  at visitNodes (./node_modules/typescript/lib/typescript.js:83656:48) 
  at Object.visitEachChild (./node_modules/typescript/lib/typescript.js:83978:173) 
  at m (./node_modules/ts-auto-mock/transformer/index.js:1:5904) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:6258 
  at visitNode (./node_modules/typescript/lib/typescript.js:83603:23) 
  at Object.visitEachChild (./node_modules/typescript/lib/typescript.js:84062:64) 
  at m (./node_modules/ts-auto-mock/transformer/index.js:1:5904) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:6258 
  at visitNodes (./node_modules/typescript/lib/typescript.js:83656:48) 
  at Object.visitEachChild (./node_modules/typescript/lib/typescript.js:84056:50) 
  at m (./node_modules/ts-auto-mock/transformer/index.js:1:5904) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:6258 
  at visitNode (./node_modules/typescript/lib/typescript.js:83603:23) 
  at visitFunctionBody (./node_modules/typescript/lib/typescript.js:83774:23) 
  at Object.visitEachChild (./node_modules/typescript/lib/typescript.js:83996:389) 
  at m (./node_modules/ts-auto-mock/transformer/index.js:1:5904) 
  at ./node_modules/ts-auto-mock/transformer/index.js:1:6258

This issue is not occurring when I run the jest command, but since the stacktrace starts with ts-auto-mock I figured I'd start here

@Pmyl
Copy link
Collaborator

Pmyl commented Jun 18, 2021

If it doesn't happen with jest it may be that wallaby.js is doing something fancy when compiling typescript to make it faster that breaks the transformer (or maybe it's using a different typescript version?)... I've never used it though and it seems to need a license to be able to use it...

Can you give me an example of interface that breaks when using wallaby and also the stacktrace? The smaller the interface the better obviously but any would be fine, at least I can start looking at where it fails and for what reason and maybe patch the issue without having to use wallaby

@egriff38
Copy link
Author

egriff38 commented Jun 18, 2021

It very well might be some fanciness, but it ceases to fail when I change the version back from 3.2.3 to 3.2.2. The runtime is actually throwing these errors before the tests even begin to execute, so I'm not entirely sure how I'd go about reproducing, unfortunately ☹️. This is the line it is now getting hung up on though, AFAIK:

const declaration: ts.Declaration = type.symbol.declarations[0];

If you're still not sure of anything that might have changed from 3.2.2 to 3.2.3 that caused type.symbol to be undefined in the above reference, I'll go ahead and open a ticket on the wallaby side. As an aside, they do offer free access to OSS owners and contributers, so if you'd like to test with the suite they'll hook you up pro bono.

@Pmyl I really appreciate the assist up to this point! You've been very responsive and super helpful

EDIT: I found there is a block in place in a similar use case in another place in the code:

if (!symbol?.declarations.length) {
return GetUndefinedDescriptor();
}

Would this pattern make sense in the typeParameters function? It would likely solve the undefined issue I'm running into

@Pmyl
Copy link
Collaborator

Pmyl commented Jun 19, 2021

My guess is that since in 3.2.2 it didn't pick up the interface correctly (because it was considering it to be null) it didn't get to the error, now in 3.2.3 the interface works correctly and one of the properties (or even deeper than that) there is a weird type that make the code explode. If you can tell me what's the interface that breaks it I can research on it, I can fix the error handling when the symbol is undefined to just return null but that would mean sweeping the bug under the rug instead of fixing it (i.e. we will have a type that generates null and it's not written anywhere in the docs).

It's definitely better than what we have but I would like to try and fix it if you have an interface ready to reproduce the bug. I get it that it may be not possible due to not wanting to expose your code or it's just time consuming and you don't want to do it, let me know! I'll investigate a bit by myself as well in the meantime

@uittorio
Copy link
Member

Wow, this bug seems interesting! There is a trial license in wallaby js but I think we still need the interface and which type of wallaby integration you are using (vscode, intellij, ...) in order to find the issue

@egriff38
Copy link
Author

I will try to reproduce a minimal repository the causes the bug sometime today. As I said, the errors are actually occurring before any of the tests actually run, so if that's the case in general it should be a fairly lean example.

@egriff38
Copy link
Author

Should we open a new ticket for the wallaby-related issue?

@Pmyl
Copy link
Collaborator

Pmyl commented Jun 21, 2021

Not at the moment, I want to make sure it's not our fault first

@egriff38
Copy link
Author

I have been able to reproduce running just ttsc. Still working toward narrowing down the cause.

@uittorio
Copy link
Member

@egriff38 Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants