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

Using intermock in tests ? #53

Open
bogdan-calapod opened this issue Apr 13, 2021 · 7 comments
Open

Using intermock in tests ? #53

bogdan-calapod opened this issue Apr 13, 2021 · 7 comments

Comments

@bogdan-calapod
Copy link

Hello there and thank you for this great project!

I wanted to use intermock to generate stub data while testing. I tried to run it as:

import {mock} from 'intermock'

describe('generate mocked types', () => {
   it('should generate mocked types', () => {
     const mck = mock({files: [['./types.ts', 'MyType']]})
     console.log(mck)
  }
})

But whenever I run this, mck is empty ({}) - any idea what I'm doing wrong ? There's no error printed to the terminal, so I'm thinking that I'm doing something wrong somewhere 🤔

@bogdan-calapod
Copy link
Author

I've also tried running it in CLI and received:

Unsupported Primitive type 173

@bogdan-calapod
Copy link
Author

bogdan-calapod commented Apr 14, 2021

So, it seems that it doesn't like some of my types that look like:

interface TypeA {
    info: {
      id: number,
      name: string
    }
}

If I extract the type like this:

interface TypeA {
    info: TypeAInfo
}

interface TypeAInfo {
  id: number,
  name: string
}

It works great in CLI 🤔

I'm still doing something wrong when calling the mock function though.

@Xiphe
Copy link

Xiphe commented Apr 14, 2021

If I understood the API correctly this should work:

import {mock} from 'intermock'
import fs from 'fs'

describe('generate mocked types', () => {
   it('should generate mocked types', () => {
     const mck = mock({
       files: [['./types.ts', fs.readFileSync('./types.ts')]],
       interfaces: ['MyType']
     })
     console.log(mck)
  }
})

@bogdan-calapod
Copy link
Author

Ah, so I need to actually read the file contents - didn't think of that, will test and get back with the result

@fluffyspace
Copy link

Any luck? I'm trying intermock with CLI and in typescript tests and it doesn't work when the needed interface is in another file. I tried including files both in CLI and in typescript and it doesn't work... The only solution is to put all interfaces in one file.

@cjsilva-umich
Copy link

Would anyone be willing to put together a small codesandbox example of this working for testing as proposed? It would be really helpful. Maybe even enough to include in the docs cause I'm a little lost here.

@nec286
Copy link

nec286 commented Nov 9, 2022

If I understood the API correctly this should work:

import {mock} from 'intermock'
import fs from 'fs'

describe('generate mocked types', () => {
   it('should generate mocked types', () => {
     const mck = mock({
       files: [['./types.ts', fs.readFileSync('./types.ts')]],
       interfaces: ['MyType']
     })
     console.log(mck)
  }
})

For anyone reading this, you need to specify the file contents as a string:

files: [['./types.ts', fs.readFileSync('./types.ts').toString()]]

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

No branches or pull requests

5 participants