-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
In-program compiling for unit tests. preparePluginsFromCompilerOptions
discards transformProgram
?
#151
Comments
Neeevermind. Figured it out. Turns out I could just use the SourceTransformers and didn't need the ProgramTransformers. When calling Updated code: import { MatcherState } from "@vitest/expect";
import Path from "node:path";
import { expect } from "vitest";
import * as ts from "typescript";
import { getTsPackage } from "ts-patch/ts-package.js";
import * as tspatch from "ts-patch/patch/get-patched-source.js";
import * as tsmodule from "ts-patch/module/ts-module.js";
const tsPackage = getTsPackage(Path.join(process.cwd(), "./node_modules/typescript/"));
const tsModule = tsmodule.getTsModule(tsPackage, "typescript.js" as any);
const tspSource = tspatch.getPatchedSource(tsModule, { log: console.log.bind(console) });
const tsp: typeof ts = eval(tspSource.js);
const myTransformer = await import("../Transformers/MyTransformer.js" as any);
const extensions = {
toCompileTo(this: MatcherState, received: string, expected: string): {message: () => string, pass: boolean} {
let result = tsp.transpileModule(received, {
compilerOptions: {
module: ts.ModuleKind.ES2022,
target: ts.ScriptTarget.ES2022,
strict: true,
skipLibCheck: true,
skipDefaultLibCheck: true,
noErrorTruncation: true,
noEmitOnError: false,
experimentalDecorators: true,
},
transformers: {after: [myTransformer.default()] }
} as any);
let resultMessage = { message: () => `expected raw code to compile into provided sample (view diff)`, actual: result.outputText.trim(), expected: expected.trim() };
return {
...resultMessage,
pass: result.outputText.trim() === expected.trim()
};
}
};
expect.extend(extensions); The cool thing is that this code can also be used to create your own custom build script and avoid needing to add your transformers to every project (woo). Would it be possible to get some API or something for this? Even if it's just a cleaned up version of my hacky stuff that I've written added to the docs on the main page so that others know how to use ts-patch inline. |
Oh also!
|
Hello!
I've been using ts-patch for quite a while now (it's very good) and I've decided to upgrade my usage by creating unit tests for the transformers so that I can verify that they work when new versions of the compiler release.
I am using vitest (though that's not particularly important) and am trying to run the
typescript
file as an imported script, but I've run into a snag.It looks like calling the
transpileModule
function directly in the patchedtypescript
doesn't allow for transformers to run. This appears to be becausepreparePluginsFromCompilerOptions
adds all transformers with a stripped-down config (either just{ transform: string }
or{ transform: string, after: true }
butTspPlugin
's constructor wants{ transform: string, transformProgram: true }
.As
preparePluginsFromCompilerOptions
is called immediately beforenew tsp.PluginCreator
in tsp'screateProgram
I don't see a way to change the property of the plugin to allow for program transformers so that my transformers can operate when callingtranspileModule
.This is the entire code that I have so far in attempting to do this:
If you have vitest configured it can be called like so:
The text was updated successfully, but these errors were encountered: