google / intermock

Mocking library to create mock objects with fake data for TypeScript interfaces
https://google.github.io/intermock/
Apache License 2.0
1.14k stars 71 forks source link

Using `intermock` in tests ? #53

Open bogdan-calapod opened 3 years ago

bogdan-calapod commented 3 years ago

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 commented 3 years ago

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

Unsupported Primitive type 173
bogdan-calapod commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

fluffyspace commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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()]]
phyzical commented 2 months ago

This is my current itteration for reusability

just replace const typesDir = join(process.cwd(), 'src', 'types'); with however your storing the type/interface definitions

const typesDir = join(process.cwd(), 'src', 'types');
const files = readdirSync(typesDir, { encoding: 'utf8', recursive: true })
    .filter(file => file.endsWith('.ts'))
    .map(file => [join(typesDir, file), readFileSync(join(typesDir, file), 'utf8')] as [string, string]);

// eslint-disable-next-line max-len
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/explicit-module-boundary-types
export const typeFactory = (interfaces: string[], isOptionalAlwaysEnabled: boolean = true) => {

    const mockedObjects = mock({
        files,
        interfaces,
        isOptionalAlwaysEnabled
    });

    return mockedObjects;
};