2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-07-28 15:08:13 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
assertThrowsAsync,
|
|
|
|
} from "../../std/testing/asserts.ts";
|
2020-01-08 09:17:44 -05:00
|
|
|
|
2020-06-18 09:06:48 -04:00
|
|
|
Deno.test({
|
2020-12-31 16:43:54 -05:00
|
|
|
name: "Deno.emit() - sources provided",
|
2020-06-18 09:06:48 -04:00
|
|
|
async fn() {
|
2020-12-31 16:43:54 -05:00
|
|
|
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
|
|
|
"/foo.ts",
|
|
|
|
{
|
|
|
|
sources: {
|
|
|
|
"/foo.ts": `import * as bar from "./bar.ts";\n\nconsole.log(bar);\n`,
|
|
|
|
"/bar.ts": `export const bar = "bar";\n`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
assertEquals(diagnostics.length, 0);
|
|
|
|
assert(!ignoredOptions);
|
|
|
|
assertEquals(stats.length, 12);
|
|
|
|
const keys = Object.keys(files).sort();
|
2020-11-01 21:51:56 -05:00
|
|
|
assert(keys[0].endsWith("/bar.ts.js"));
|
|
|
|
assert(keys[1].endsWith("/bar.ts.js.map"));
|
|
|
|
assert(keys[2].endsWith("/foo.ts.js"));
|
|
|
|
assert(keys[3].endsWith("/foo.ts.js.map"));
|
2020-06-18 09:06:48 -04:00
|
|
|
},
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
|
|
|
|
2020-06-18 09:06:48 -04:00
|
|
|
Deno.test({
|
2020-12-31 16:43:54 -05:00
|
|
|
name: "Deno.emit() - no sources provided",
|
2020-06-18 09:06:48 -04:00
|
|
|
async fn() {
|
2020-12-31 16:43:54 -05:00
|
|
|
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
|
|
|
"./subdir/mod1.ts",
|
|
|
|
);
|
|
|
|
assertEquals(diagnostics.length, 0);
|
|
|
|
assert(!ignoredOptions);
|
|
|
|
assertEquals(stats.length, 12);
|
|
|
|
const keys = Object.keys(files).sort();
|
2020-06-18 09:06:48 -04:00
|
|
|
assertEquals(keys.length, 6);
|
2020-11-01 21:51:56 -05:00
|
|
|
assert(keys[0].endsWith("cli/tests/subdir/mod1.ts.js"));
|
|
|
|
assert(keys[1].endsWith("cli/tests/subdir/mod1.ts.js.map"));
|
2020-06-18 09:06:48 -04:00
|
|
|
},
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
|
|
|
|
2020-06-18 09:06:48 -04:00
|
|
|
Deno.test({
|
2020-12-31 16:43:54 -05:00
|
|
|
name: "Deno.emit() - compiler options effects emit",
|
2020-06-18 09:06:48 -04:00
|
|
|
async fn() {
|
2020-12-31 16:43:54 -05:00
|
|
|
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
2020-06-18 09:06:48 -04:00
|
|
|
"/foo.ts",
|
|
|
|
{
|
2020-12-31 16:43:54 -05:00
|
|
|
compilerOptions: {
|
|
|
|
module: "amd",
|
|
|
|
sourceMap: false,
|
|
|
|
},
|
|
|
|
sources: { "/foo.ts": `export const foo = "foo";` },
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-18 09:06:48 -04:00
|
|
|
);
|
2020-12-31 16:43:54 -05:00
|
|
|
assertEquals(diagnostics.length, 0);
|
|
|
|
assert(!ignoredOptions);
|
|
|
|
assertEquals(stats.length, 12);
|
|
|
|
const keys = Object.keys(files);
|
2020-11-01 21:51:56 -05:00
|
|
|
assertEquals(keys.length, 1);
|
|
|
|
const key = keys[0];
|
|
|
|
assert(key.endsWith("/foo.ts.js"));
|
2020-12-31 16:43:54 -05:00
|
|
|
assert(files[key].startsWith("define("));
|
2020-06-18 09:06:48 -04:00
|
|
|
},
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
|
|
|
|
2020-06-18 09:06:48 -04:00
|
|
|
Deno.test({
|
2020-12-31 16:43:54 -05:00
|
|
|
name: "Deno.emit() - pass lib in compiler options",
|
2020-06-18 09:06:48 -04:00
|
|
|
async fn() {
|
2020-12-31 16:43:54 -05:00
|
|
|
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
2020-11-01 21:51:56 -05:00
|
|
|
"file:///foo.ts",
|
2020-06-18 09:06:48 -04:00
|
|
|
{
|
2020-12-31 16:43:54 -05:00
|
|
|
compilerOptions: {
|
|
|
|
lib: ["dom", "es2018", "deno.ns"],
|
|
|
|
},
|
|
|
|
sources: {
|
|
|
|
"file:///foo.ts": `console.log(document.getElementById("foo"));
|
|
|
|
console.log(Deno.args);`,
|
|
|
|
},
|
2020-06-18 09:06:48 -04:00
|
|
|
},
|
2020-12-31 16:43:54 -05:00
|
|
|
);
|
|
|
|
assertEquals(diagnostics.length, 0);
|
|
|
|
assert(!ignoredOptions);
|
|
|
|
assertEquals(stats.length, 12);
|
|
|
|
const keys = Object.keys(files).sort();
|
|
|
|
assertEquals(keys, ["file:///foo.ts.js", "file:///foo.ts.js.map"]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Deno.emit() - import maps",
|
|
|
|
async fn() {
|
|
|
|
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
|
|
|
"file:///a.ts",
|
2020-06-18 09:06:48 -04:00
|
|
|
{
|
2020-12-31 16:43:54 -05:00
|
|
|
importMap: {
|
|
|
|
imports: {
|
|
|
|
"b": "./b.ts",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
importMapPath: "file:///import-map.json",
|
|
|
|
sources: {
|
|
|
|
"file:///a.ts": `import * as b from "b"
|
|
|
|
console.log(b);`,
|
|
|
|
"file:///b.ts": `export const b = "b";`,
|
|
|
|
},
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-18 09:06:48 -04:00
|
|
|
);
|
2020-12-31 16:43:54 -05:00
|
|
|
assertEquals(diagnostics.length, 0);
|
|
|
|
assert(!ignoredOptions);
|
|
|
|
assertEquals(stats.length, 12);
|
|
|
|
const keys = Object.keys(files).sort();
|
2020-11-01 21:51:56 -05:00
|
|
|
assertEquals(
|
2020-12-31 16:43:54 -05:00
|
|
|
keys,
|
|
|
|
[
|
|
|
|
"file:///a.ts.js",
|
|
|
|
"file:///a.ts.js.map",
|
|
|
|
"file:///b.ts.js",
|
|
|
|
"file:///b.ts.js.map",
|
|
|
|
],
|
2020-09-09 07:37:22 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-18 09:06:48 -04:00
|
|
|
Deno.test({
|
2020-12-31 16:43:54 -05:00
|
|
|
name: "Deno.emit() - no check",
|
2020-06-18 09:06:48 -04:00
|
|
|
async fn() {
|
2020-12-31 16:43:54 -05:00
|
|
|
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
|
|
|
"/foo.ts",
|
|
|
|
{
|
|
|
|
check: false,
|
|
|
|
sources: {
|
|
|
|
"/foo.ts": `export enum Foo { Foo, Bar, Baz };\n`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
assertEquals(diagnostics.length, 0);
|
|
|
|
assert(!ignoredOptions);
|
|
|
|
assertEquals(stats.length, 3);
|
|
|
|
const keys = Object.keys(files).sort();
|
|
|
|
assert(keys[0].endsWith("/foo.ts.js"));
|
|
|
|
assert(keys[1].endsWith("/foo.ts.js.map"));
|
|
|
|
assert(files[keys[0]].startsWith("export var Foo;"));
|
2020-06-18 09:06:48 -04:00
|
|
|
},
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
|
|
|
|
2020-06-18 09:06:48 -04:00
|
|
|
Deno.test({
|
2020-12-31 16:43:54 -05:00
|
|
|
name: "Deno.emit() - no check - config effects emit",
|
2020-06-18 09:06:48 -04:00
|
|
|
async fn() {
|
2020-12-31 16:43:54 -05:00
|
|
|
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
|
|
|
"/foo.ts",
|
2020-06-18 09:06:48 -04:00
|
|
|
{
|
2020-12-31 16:43:54 -05:00
|
|
|
check: false,
|
|
|
|
compilerOptions: { removeComments: true },
|
|
|
|
sources: {
|
|
|
|
"/foo.ts":
|
|
|
|
`/** This is JSDoc */\nexport enum Foo { Foo, Bar, Baz };\n`,
|
|
|
|
},
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-18 09:06:48 -04:00
|
|
|
);
|
2020-12-31 16:43:54 -05:00
|
|
|
assertEquals(diagnostics.length, 0);
|
|
|
|
assert(!ignoredOptions);
|
|
|
|
assertEquals(stats.length, 3);
|
|
|
|
const keys = Object.keys(files).sort();
|
|
|
|
assert(keys[0].endsWith("/foo.ts.js"));
|
|
|
|
assert(keys[1].endsWith("/foo.ts.js.map"));
|
|
|
|
assert(!files[keys[0]].includes("This is JSDoc"));
|
2020-06-18 09:06:48 -04:00
|
|
|
},
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
|
|
|
|
2020-06-18 09:06:48 -04:00
|
|
|
Deno.test({
|
2020-12-31 16:43:54 -05:00
|
|
|
name: "Deno.emit() - bundle esm - with sources",
|
2020-06-18 09:06:48 -04:00
|
|
|
async fn() {
|
2020-12-31 16:43:54 -05:00
|
|
|
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
|
|
|
"/foo.ts",
|
|
|
|
{
|
|
|
|
bundle: "esm",
|
|
|
|
sources: {
|
|
|
|
"/foo.ts": `export * from "./bar.ts";\n`,
|
|
|
|
"/bar.ts": `export const bar = "bar";\n`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
assertEquals(diagnostics.length, 0);
|
|
|
|
assert(!ignoredOptions);
|
|
|
|
assertEquals(stats.length, 12);
|
|
|
|
assertEquals(Object.keys(files), ["deno:///bundle.js"]);
|
2021-01-26 11:00:40 -05:00
|
|
|
assert(files["deno:///bundle.js"].includes(`const bar1 = "bar"`));
|
2020-06-18 09:06:48 -04:00
|
|
|
},
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
|
|
|
|
2020-06-18 09:06:48 -04:00
|
|
|
Deno.test({
|
2020-12-31 16:43:54 -05:00
|
|
|
name: "Deno.emit() - bundle esm - no sources",
|
2020-06-18 09:06:48 -04:00
|
|
|
async fn() {
|
2020-12-31 16:43:54 -05:00
|
|
|
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
|
|
|
"./subdir/mod1.ts",
|
|
|
|
{
|
|
|
|
bundle: "esm",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
assertEquals(diagnostics.length, 0);
|
|
|
|
assert(!ignoredOptions);
|
|
|
|
assertEquals(stats.length, 12);
|
|
|
|
assertEquals(Object.keys(files), ["deno:///bundle.js"]);
|
|
|
|
assert(files["deno:///bundle.js"].length);
|
2020-06-18 09:06:48 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2020-12-31 16:43:54 -05:00
|
|
|
name: "Deno.emit() - bundle esm - include js modules",
|
2020-06-18 09:06:48 -04:00
|
|
|
async fn() {
|
2020-12-31 16:43:54 -05:00
|
|
|
const { diagnostics, files, ignoredOptions, stats } = await Deno.emit(
|
|
|
|
"/foo.js",
|
|
|
|
{
|
|
|
|
bundle: "esm",
|
|
|
|
sources: {
|
|
|
|
"/foo.js": `export * from "./bar.js";\n`,
|
|
|
|
"/bar.js": `export const bar = "bar";\n`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
assertEquals(diagnostics.length, 0);
|
|
|
|
assert(!ignoredOptions);
|
|
|
|
assertEquals(stats.length, 12);
|
|
|
|
assertEquals(Object.keys(files), ["deno:///bundle.js"]);
|
2021-01-26 11:00:40 -05:00
|
|
|
assert(files["deno:///bundle.js"].includes(`const bar1 = "bar"`));
|
2020-06-18 09:06:48 -04:00
|
|
|
},
|
2020-03-02 16:18:27 -05:00
|
|
|
});
|
|
|
|
|
2020-06-18 09:06:48 -04:00
|
|
|
Deno.test({
|
2020-12-31 16:43:54 -05:00
|
|
|
name: "Deno.emit() - generates diagnostics",
|
2020-06-18 09:06:48 -04:00
|
|
|
async fn() {
|
2020-12-31 16:43:54 -05:00
|
|
|
const { diagnostics, files } = await Deno.emit(
|
|
|
|
"/foo.ts",
|
|
|
|
{
|
|
|
|
sources: {
|
|
|
|
"/foo.ts": `document.getElementById("foo");`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
assertEquals(diagnostics.length, 1);
|
|
|
|
const keys = Object.keys(files).sort();
|
|
|
|
assert(keys[0].endsWith("/foo.ts.js"));
|
|
|
|
assert(keys[1].endsWith("/foo.ts.js.map"));
|
2020-06-18 09:06:48 -04:00
|
|
|
},
|
2020-02-07 01:54:05 -05:00
|
|
|
});
|
2020-07-28 15:08:13 -04:00
|
|
|
|
|
|
|
// See https://github.com/denoland/deno/issues/6908
|
|
|
|
Deno.test({
|
2020-12-31 16:43:54 -05:00
|
|
|
name: "Deno.emit() - invalid syntax does not panic",
|
2020-07-28 15:08:13 -04:00
|
|
|
async fn() {
|
|
|
|
await assertThrowsAsync(async () => {
|
2020-12-31 16:43:54 -05:00
|
|
|
await Deno.emit("/main.js", {
|
|
|
|
sources: {
|
|
|
|
"/main.js": `
|
|
|
|
export class Foo {
|
|
|
|
constructor() {
|
|
|
|
console.log("foo");
|
|
|
|
}
|
|
|
|
export get() {
|
|
|
|
console.log("bar");
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
},
|
2020-07-28 15:08:13 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2020-11-09 14:50:33 -05:00
|
|
|
|
|
|
|
Deno.test({
|
2020-12-31 16:43:54 -05:00
|
|
|
name: 'Deno.emit() - allows setting of "importsNotUsedAsValues"',
|
2020-11-09 14:50:33 -05:00
|
|
|
async fn() {
|
2020-12-31 16:43:54 -05:00
|
|
|
const { diagnostics } = await Deno.emit("/a.ts", {
|
|
|
|
sources: {
|
|
|
|
"/a.ts": `import { B } from "./b.ts";
|
|
|
|
const b: B = { b: "b" };`,
|
|
|
|
"/b.ts": `export interface B {
|
|
|
|
b:string;
|
|
|
|
};`,
|
|
|
|
},
|
|
|
|
compilerOptions: {
|
|
|
|
importsNotUsedAsValues: "error",
|
|
|
|
},
|
2020-11-09 14:50:33 -05:00
|
|
|
});
|
|
|
|
assert(diagnostics);
|
|
|
|
assertEquals(diagnostics.length, 1);
|
|
|
|
assert(diagnostics[0].messageText);
|
|
|
|
assert(diagnostics[0].messageText.includes("This import is never used"));
|
|
|
|
},
|
|
|
|
});
|