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

feat(typeQuery): add support for typeof of an imported module #128

Merged
merged 4 commits into from
Jan 5, 2020

Conversation

Pmyl
Copy link
Collaborator

@Pmyl Pmyl commented Jan 2, 2020

This PR adds support to cases like this:

import * as STAR from './module1';
import REQUIRE = require('./module2');

const module1Mock: typeof STAR = createMock<typeof STAR>();
const module2Mock: typeof REQUIRE = createMock<typeof REQUIRE>();

It takes in account multiple exports, export default (with ad hoc code) and export equals.

I've also:

  • added some types to make more clear what is happening with properties and signatures removing the confusion that we had before where everything was casted to a different type every few lines
  • added a method to get the alias of a symbol safely (if you call the typescript method with a non-alias symbol it will fail)
  • fixed a case where creating a mock of a typeof Enum was breaking the original Enum
  • fixed the mock of typeof obj.method
  • added some more random casting because typescript typings are weird

Please Note: the mock of a module will contain everything that is exported, even interfaces and types that are not available to use.

@Pmyl Pmyl requested a review from uittorio January 2, 2020 18:27
uittorio
uittorio previously approved these changes Jan 2, 2020
Copy link
Member

@uittorio uittorio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy with this, just concern about declaration index access. Are you sure there are now ways to have multiple declarations in these scenarios? :)

@Pmyl
Copy link
Collaborator Author

Pmyl commented Jan 3, 2020

Happy with this, just concern about declaration index access. Are you sure there are now ways to have multiple declarations in these scenarios? :)

When do you think an imported symbol could have multiple declarations? I have to be honest I don't remember the case

@Pmyl
Copy link
Collaborator Author

Pmyl commented Jan 3, 2020

@uittorio I've changed the code slightly to fix a problem where a variable with inferred type of the module didn't work.

To be clear, this is the case:

import * as STAR from './module';

const moduleVariable = STAR;
createMock<typeof moduleVariable>();

I've reorganised the code a bit, I think now it makes more sense.

Let me know about the multiple declarations (comment above).

@uittorio
Copy link
Member

uittorio commented Jan 4, 2020

Happy with this, just concern about declaration index access. Are you sure there are now ways to have multiple declarations in these scenarios? :)

When do you think an imported symbol could have multiple declarations? I have to be honest I don't remember the case

multiple declaration exists when you declare a variableDeclaration and an interfaceDeclaration with the same name


const Interface: Interface;

interface Interface {
    a: string;
}

Or when you declare multiple declaration of the same interface

interface Interface {
    a: string;
}
interface Interface {
    b: string
}

The declaration[0] in the second scenario is smart enough to get all the properties even if it doesn't look like it contains them. I just had an issue in the past that for some reason there was a variable declaration on top and I couldn't find the right declaration.

@Pmyl
Copy link
Collaborator Author

Pmyl commented Jan 5, 2020

@uittorio those cases work fine for the same reason: I'm getting the declaration and then I'm sending it again to do the process cycle (a.k.a. GetDescriptor) where, if needed, it will gets all the declarations.

@Pmyl Pmyl merged commit a9e25a3 into master Jan 5, 2020
@Pmyl Pmyl deleted the feature/type-query-full-module branch January 5, 2020 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants