ts-auto-mock icon indicating copy to clipboard operation
ts-auto-mock copied to clipboard

[Feature request] CLI mode

Open gilbsgilbs opened this issue 4 years ago • 2 comments

Is your feature request related to a problem?

I don't want to migrate my project to ttypescript just for mocking stuff. Also, it's sometimes very inconvenient to setup ttypescript (for instance with create-react-app).

Describe the solution you'd like

I think a realistic solution would be to provide a CLI command that discovers the project's interfaces and output their corresponding mocks to plain .ts files that you can later import like normal. e.g. you put ts-auto-mock -i 'path/to/my/**/*.ts' -o '__fixtures__/gen/mocks/' in your prebuild script to use ts-auto-mock without the transforms magic.

Describe alternatives you've considered

  • Having this as an official third-party package (like the jasmine and jest integrations) might be nice (but isn't possible yet as the internals are not exposed by the module).

Additional context

Thanks for your work on this interesting project.

gilbsgilbs avatar Oct 16 '20 22:10 gilbsgilbs

Hi @gilbsgilbs!! Currently, ts-auto-mock is quite integrated with the test file you are using. The mocks are created inside the test files. I think it would be useful to use ts-auto-mock in the way you are describing it. I understand the scenario that you are providing, there is only one thing that I don't quite follow.

How would you then imports these mocks in your tests? Would use fixtures/gen/mocks?

uittorio avatar Oct 18 '20 17:10 uittorio

Hi @uittorio :wave: thanks for your reply.

How would you then imports these mocks in your tests? Would use fixtures/gen/mocks?

Well, I used __fixtures__/gen/mocks as an example but that's probably weird. What I really meant was that you would generate them anywhere you want, say src/gen/mocks/UserMock.ts. As they would be regular TS file, you would then import them normally:

// src/gen/mocks/UserMock.ts
// generated code, do not edit
const UserMock = {email: "", name: "", age: 0};
export {UserMock}

// src/something.test.ts
import {UserMock} from "./gen/mocks/UserMock";
it("mocks", () => {
  expect(UserMock).toMatchSnapshot();
})

// package.json
{
  "genmocks": "ts-auto-mock -i src/**/*.ts -o src/gen/",
  "test": "${npm_execpath} run genmocks && jest"
}

FYI, there's intermock that works a bit that way but focuses on mocking interfaces only and generating fake data.

gilbsgilbs avatar Oct 18 '20 18:10 gilbsgilbs