diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2018-10-31 14:50:40 +1100 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-10-30 20:50:40 -0700 |
commit | 2422e526257d971ba382bb9195e0ee527b918672 (patch) | |
tree | 0c328ab748c070d0b165f859fabf105d0b2829e9 /js/compiler_test.ts | |
parent | 0fbee30f050bc27b9a687c2580c89cb9dc5d7fec (diff) |
Add ability to load JSON as modules (#1065)
Diffstat (limited to 'js/compiler_test.ts')
-rw-r--r-- | js/compiler_test.ts | 72 |
1 files changed, 68 insertions, 4 deletions
diff --git a/js/compiler_test.ts b/js/compiler_test.ts index d7a5c877c..729d6b4a7 100644 --- a/js/compiler_test.ts +++ b/js/compiler_test.ts @@ -6,7 +6,7 @@ import * as ts from "typescript"; // We use a silly amount of `any` in these tests... // tslint:disable:no-any -const { DenoCompiler } = (deno as any)._compiler; +const { DenoCompiler, jsonAmdTemplate } = (deno as any)._compiler; interface ModuleInfo { moduleName: string | undefined; @@ -118,6 +118,11 @@ const fooBazTsOutput = `define(["require", "exports", "./bar.ts"], function (req // This is not a valid map, just mock data const fooBazTsSourcemap = `{"version":3,"file":"baz.js","sourceRoot":"","sources":["file:///root/project/foo/baz.ts"],"names":[],"mappings":""}`; + +const loadConfigSource = `import * as config from "./config.json"; +console.log(config.foo.baz); +`; +const configJsonSource = `{"foo":{"bar": true,"baz": ["qat", 1]}}`; // tslint:enable:max-line-length const moduleMap: { @@ -148,6 +153,14 @@ const moduleMap: { "console.log();", null, null + ), + "loadConfig.ts": mockModuleInfo( + "/root/project/loadConfig.ts", + "/root/project/loadConfig.ts", + MediaType.TypeScript, + loadConfigSource, + null, + null ) }, "/root/project/foo/baz.ts": { @@ -166,6 +179,16 @@ const moduleMap: { "/root/project/modB.ts": { "./modA.ts": modAModuleInfo }, + "/root/project/loadConfig.ts": { + "./config.json": mockModuleInfo( + "/root/project/config.json", + "/root/project/config.json", + MediaType.Json, + configJsonSource, + null, + null + ) + }, "/moduleKinds": { "foo.ts": mockModuleInfo( "foo", @@ -280,7 +303,7 @@ const osMock = { return mockModuleInfo(null, null, null, null, null, null); }, exit(code: number): never { - throw new Error(`os.exit(${code})`); + throw new Error(`Unexpected call to os.exit(${code})`); } }; const tsMock = { @@ -289,9 +312,9 @@ const tsMock = { }, formatDiagnosticsWithColorAndContext( diagnostics: ReadonlyArray<ts.Diagnostic>, - host: ts.FormatDiagnosticsHost + _host: ts.FormatDiagnosticsHost ): string { - return ""; + return JSON.stringify(diagnostics.map(({ messageText }) => messageText)); } }; @@ -374,6 +397,23 @@ function teardown() { Object.assign(compilerInstance, originals); } +test(function testJsonAmdTemplate() { + let deps: string[]; + let factory: Function; + function define(d: string[], f: Function) { + deps = d; + factory = f; + } + + const code = jsonAmdTemplate(`{ "hello": "world", "foo": "bar" }`); + const result = eval(code); + assert(result == null); + assertEqual(deps && deps.length, 0); + assert(factory != null); + const factoryResult = factory(); + assertEqual(factoryResult, { hello: "world", foo: "bar" }); +}); + test(function compilerInstance() { assert(DenoCompiler != null); assert(DenoCompiler.instance() != null); @@ -479,6 +519,29 @@ test(function compilerRunCircularDependency() { teardown(); }); +test(function compilerLoadJsonModule() { + setup(); + const factoryStack: string[] = []; + const configJsonDeps: string[] = []; + const configJsonFactory = () => { + factoryStack.push("configJson"); + return JSON.parse(configJsonSource); + }; + const loadConfigDeps = ["require", "exports", "./config.json"]; + const loadConfigFactory = (_require, _exports, _config) => { + factoryStack.push("loadConfig"); + assertEqual(_config, JSON.parse(configJsonSource)); + }; + + mockDepsStack.push(configJsonDeps); + mockFactoryStack.push(configJsonFactory); + mockDepsStack.push(loadConfigDeps); + mockFactoryStack.push(loadConfigFactory); + compilerInstance.run("loadConfig.ts", "/root/project"); + assertEqual(factoryStack, ["configJson", "loadConfig"]); + teardown(); +}); + test(function compilerResolveModule() { setup(); const moduleMetaData = compilerInstance.resolveModule( @@ -544,6 +607,7 @@ test(function compilerGetCompilationSettings() { "checkJs", "module", "outDir", + "resolveJsonModule", "sourceMap", "stripComments", "target" |