asapach / babel-plugin-rewire-exports

Babel plugin for stubbing [ES6, ES2015] module exports
MIT License
66 stars 7 forks source link

support typescript #40

Closed dotennin closed 1 year ago

dotennin commented 1 year ago

'"@/store/modules/pages/home"' has no exported member named 'rewire$XXX'. Did you mean 'XXX'? (tsserver 2724)

Got this error when importing rewireXXX in the test file . Any good idea for Typescript support ?*excepts ts-ignore.

sergei-startsev commented 1 year ago
import * as bar from './bar';

(<any>bar).fooXXX();
asapach commented 1 year ago

I don't think a babel plugin can really do much to address this. I would suggest either following @sergei-startsev's example or adding a dummy function declaration to the source module to satisfy type checking, e.g.:

export declare function rewire$XXX(_: any): void;
export declare function restore(): void;
dotennin commented 1 year ago

I don't think a babel plugin can really do much to address this. I would suggest either following @sergei-startsev's example or adding a dummy function declaration to the source module to satisfy type checking, e.g.:

export declare function rewire$XXX(_: any): void;
export declare function restore(): void;

Yeah , In addition to trouble, but this is the safest. we need to add a corresponding declaration for each variable that needs to be mocked,

import * as bar from './bar';

(<any>bar).fooXXX();

It's also a good idea,

Actually I did the following way to resolve this issue.

const foo = require('path to file')
foo.rewire$XXX(() => mockXXX);

This is my current simpler way of avoiding any and ts-ignore etc.(but loses type checking 😢 ) Thanks for your advice.