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

enhancement: mock different values based on other fields #135

Open
arthurfiorette opened this issue Jan 20, 2022 · 3 comments
Open

enhancement: mock different values based on other fields #135

arthurfiorette opened this issue Jan 20, 2022 · 3 comments
Labels
enhancement 🎉 New feature or request

Comments

@arthurfiorette
Copy link

arthurfiorette commented Jan 20, 2022

Sorry for opening another issue. But i need to generate a different values depending on another fields, example:

export class Example {
  @Mock()
  isActive: boolean;

  @Mock() // Should only be defined if isActive is false.
  deactivationReason?: string;
}

So, it would be cool if i could write something like that:

export class Example {
  @Mock()
  isActive: boolean;

  @Mock((faker, self) => self.isActive ? null : faker.someMethod())
  deactivationReason?: string;
}

Where data is what is already generated, like isActive (Probably depends on execution / field order) because it was declared first.

Also, if it is possible, you also could add the hability to return promises.

Thanks for making this amazing library!

@arthurfiorette
Copy link
Author

arthurfiorette commented Jan 20, 2022

Also, if i use null or undefined as the @Mock(() => result), it throws:

    TypeError: Cannot convert undefined or null to object
        at Function.getOwnPropertyNames (<anonymous>)

      at getClassMembers (../../../node_modules/@plumier/reflect/lib/parser.js:87:28)
      at parseMethods (../../../node_modules/@plumier/reflect/lib/parser.js:134:21)
      at parseClassNoCache (../../../node_modules/@plumier/reflect/lib/parser.js:173:18)
      at ../../../node_modules/@plumier/reflect/lib/helpers.js:18:31
      at walkTypeMembers (../../../node_modules/@plumier/reflect/lib/walker.js:35:45)
      at walkTypeMembersRecursive (../../../node_modules/@plumier/reflect/lib/walker.js:54:23)
      at reflectClass (../../../node_modules/@plumier/reflect/lib/reflect.js:15:55)
      at reflectModuleOrClass (../../../node_modules/@plumier/reflect/lib/reflect.js:66:16)
      at ../../../node_modules/@plumier/reflect/lib/helpers.js:18:31

@omermorad
Copy link
Owner

Got you! Thanks a lot, @arthurfiorette. Don't be sorry, it's our job to make improve this library over time :)

@omermorad
Copy link
Owner

Also, if i use null or undefined as the @Mock(() => result), it throws:

    TypeError: Cannot convert undefined or null to object
        at Function.getOwnPropertyNames (<anonymous>)

      at getClassMembers (../../../node_modules/@plumier/reflect/lib/parser.js:87:28)
      at parseMethods (../../../node_modules/@plumier/reflect/lib/parser.js:134:21)
      at parseClassNoCache (../../../node_modules/@plumier/reflect/lib/parser.js:173:18)
      at ../../../node_modules/@plumier/reflect/lib/helpers.js:18:31
      at walkTypeMembers (../../../node_modules/@plumier/reflect/lib/walker.js:35:45)
      at walkTypeMembersRecursive (../../../node_modules/@plumier/reflect/lib/walker.js:54:23)
      at reflectClass (../../../node_modules/@plumier/reflect/lib/reflect.js:15:55)
      at reflectModuleOrClass (../../../node_modules/@plumier/reflect/lib/reflect.js:66:16)
      at ../../../node_modules/@plumier/reflect/lib/helpers.js:18:31

Can you open a new issue with the full steps to reproduce?

@omermorad omermorad added the enhancement 🎉 New feature or request label Jan 22, 2022
@omermorad omermorad changed the title Mock different values based on other fields enhancement: mock different values based on other fields Jan 22, 2022
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