Open bogdan-calapod opened 3 years ago
I've also tried running it in CLI and received:
Unsupported Primitive type 173
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.
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)
}
})
Ah, so I need to actually read the file contents - didn't think of that, will test and get back with the result
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.
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.
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()]]
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;
};
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: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 🤔