ts-auto-mock
ts-auto-mock copied to clipboard
mock set to null when interface and namespace have the same name
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
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!!
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)
PR open to fix it #793 now waiting for @uittorio to review it, in one day max it should go in π
@egriff38 new version released, let me know if that fixes the issue!
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
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
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: https://github.com/Typescript-TDD/ts-auto-mock/blob/94dfbf0496d5856197601958067023e36a351801/src/transformer/descriptor/typeParameter/typeParameter.ts#L42
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: https://github.com/Typescript-TDD/ts-auto-mock/blob/a503b4afeb43479ca473bfe02b7a44962e20efaf/src/transformer/descriptor/typeQuery/typeQuery.ts#L29-L31 Would this pattern make sense in the typeParameters function? It would likely solve the undefined issue I'm running into
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
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
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.
Should we open a new ticket for the wallaby-related issue?
Not at the moment, I want to make sure it's not our fault first
I have been able to reproduce running just ttsc. Still working toward narrowing down the cause.
@egriff38 Any updates on this?