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

createMockList() returns undefined when called with a passed-in function argument #595

Closed
ChrisKanaganayagam-AI opened this issue Oct 1, 2020 · 5 comments
Labels

Comments

@ChrisKanaganayagam-AI
Copy link

I have a bit of a weird issue when calling createMockList. Whenever I call createMockList with an argument passed into a function denoting how many of the objects I'd like to create, createMockList returns undefined. There are no issues if I hardcode the number in createMockList. Since a picture (or code in this case) might be worth a thousand words, here's something I quickly hacked together to highlight the issue:

import { createMockList } from 'ts-auto-mock';


export class Animal {
  numberOfLegs: number;
}

fdescribe('AnimalTest', () => {
  function createSpecificNumberOfMockAnimals(numberOfAnimals: number): Animal[] {
    return createMockList<Animal>(numberOfAnimals);
  }

  function createMockAnimals(): Animal[] {
    return createMockList<Animal>(2);
  }

  it('should create animals with specific number', async () => {
    const mockAnimals = createSpecificNumberOfMockAnimals(3);
    console.log(mockAnimals[0]); // log says undefined
    expect(mockAnimals.length).toEqual(3); // test failure saying "Error: Expected 0 to equal 3."
  });

  it('should create animals without specifying number', async () => {
    const mockAnimals = createMockAnimals();
    console.log(mockAnimals[0]); // log outputs an animal with a random number of legs
    expect(mockAnimals.length).toEqual(2); // test passes
  });
});

Environment

  • ts-auto-mock version: 2.6.4
  • typescript version: 3.8.3
  • node version: 10.13.0
  • npm version: 6.4.1
  • ts-patch: 1.3.0
  • angular: 9.1.12
  • karma: 4.4.1
  • jasmine-core: 3.4.0

Steps to reproduce

If you are able to run the two test cases above, the first test case fails. Second test case passes.

Expected behavior

We should be able to pass in an arg into a function to specify how many of an object to create with createMockList.

Actual behavior

Whenever an arg is passed into a function to specify the number, createMockList() returns undefined.

@uittorio
Copy link
Member

uittorio commented Oct 1, 2020

hi @ChrisKanaganayagam-AI ! Thanks for reporting the issue :)

I confirm that it's an issue. We currently try to understand the number using the typescript AST. In the scenario where you send a value from another function, it doesn't recognize the 'number'.

This is the code that does not translate correctly the number: here and here

We'll keep you update.. it shouldn't be too difficult finding the value :)

@ChrisKanaganayagam-AI
Copy link
Author

Thanks for looking into this so quickly @uittorio . Much appreciated! :)

uittorio added a commit that referenced this issue Oct 6, 2020
…instead of relying on a numeric literal

Closes #595
@typescripttdd
Copy link
Collaborator

🎉 This issue has been resolved in version 2.6.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

@uittorio
Copy link
Member

uittorio commented Oct 8, 2020

Just released @ChrisKanaganayagam-AI !! 💚 💚 💚 💚 💚

@ChrisKanaganayagam-AI
Copy link
Author

Thank you very much @uittorio . Much appreciated sir!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants