Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it("jsfile", () => {
// With .js suffix in fileName, ts.transpileModule handle the input as JS, not TS.
let out = ts.transpileModule('let x: string = "hello"', {
fileName: "src.js",
reportDiagnostics: true
});
expect(out.diagnostics.length).toEqual(1);
expect(out.diagnostics[0].messageText).toEqual(
"'types' can only be used in a .ts file."
);
expect(out.outputText).toEqual('var x = "hello";\n');
expect(out.sourceMapText).toBeUndefined();
});
export function esModuleToCommonJSModule(
js: string,
target: ts.ScriptTarget
): string {
let out = ts.transpileModule(js, {
fileName: "custom.js",
compilerOptions: {
module: ts.ModuleKind.CommonJS,
esModuleInterop: true,
target,
newLine: ts.NewLineKind.LineFeed,
// Remove 'use strict' from outputs.
noImplicitUseStrict: true
}
}).outputText;
return out;
}
it("import to require", () => {
let out = ts.transpileModule(
[
'import {a, b} from "os";',
'import * as c from "vm";',
"let d = a() + b;",
"let e = x(y);",
"export {a, b, c, d, e}"
].join("\n"),
{
fileName: "src.js",
compilerOptions: {
noImplicitUseStrict: true,
module: ts.ModuleKind.CommonJS
}
}
);
expect(out.diagnostics).toEqual([]);