summaryrefslogtreecommitdiff
path: root/std/node/process_test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'std/node/process_test.ts')
-rw-r--r--std/node/process_test.ts71
1 files changed, 37 insertions, 34 deletions
diff --git a/std/node/process_test.ts b/std/node/process_test.ts
index 9b2c050a7..22cebf260 100644
--- a/std/node/process_test.ts
+++ b/std/node/process_test.ts
@@ -1,37 +1,12 @@
// deno-lint-ignore-file no-undef
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
+import "./global.ts";
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
+import { stripColor } from "../fmt/colors.ts";
import * as path from "../path/mod.ts";
-import * as all from "./process.ts";
-import { argv, env } from "./process.ts";
import { delay } from "../async/delay.ts";
-import "./global.ts";
-
-// NOTE: Deno.execPath() (and thus process.argv) currently requires --allow-env
-// (Also Deno.env.toObject() (and process.env) requires --allow-env but it's more obvious)
-
-Deno.test({
- name: "process exports are as they should be",
- fn() {
- // * should be the same as process, default, and globalThis.process
- // without the export aliases, and with properties that are not standalone
- const allKeys = new Set<string>(Object.keys(all));
- // without { process } for deno b/c
- allKeys.delete("process");
- // without esm default
- allKeys.delete("default");
- // with on, stdin, stderr, and stdout, which is not exported via *
- allKeys.add("on");
- allKeys.add("stdin");
- allKeys.add("stderr");
- allKeys.add("stdout");
- const allStr = Array.from(allKeys).sort().join(" ");
- assertEquals(Object.keys(all.default).sort().join(" "), allStr);
- assertEquals(Object.keys(all.process).sort().join(" "), allStr);
- assertEquals(Object.keys(process).sort().join(" "), allStr);
- },
-});
+import { env } from "./process.ts";
Deno.test({
name: "process.cwd and process.chdir success",
@@ -103,7 +78,7 @@ Deno.test({
Deno.test({
name: "process.on",
- fn() {
+ async fn() {
assertEquals(typeof process.on, "function");
assertThrows(
() => {
@@ -112,6 +87,33 @@ Deno.test({
Error,
"implemented",
);
+
+ let triggered = false;
+ process.on("exit", () => {
+ triggered = true;
+ });
+ process.emit("exit");
+ assert(triggered);
+
+ const cwd = path.dirname(path.fromFileUrl(import.meta.url));
+
+ const p = Deno.run({
+ cmd: [
+ Deno.execPath(),
+ "run",
+ "./process_exit_test.ts",
+ ],
+ cwd,
+ stdout: "piped",
+ });
+
+ const decoder = new TextDecoder();
+ const rawOutput = await p.output();
+ assertEquals(
+ stripColor(decoder.decode(rawOutput).trim()),
+ "1\n2",
+ );
+ p.close();
},
});
@@ -119,12 +121,14 @@ Deno.test({
name: "process.argv",
fn() {
assert(Array.isArray(process.argv));
- assert(Array.isArray(argv));
assert(
process.argv[0].match(/[^/\\]*deno[^/\\]*$/),
"deno included in the file name of argv[0]",
);
- // we cannot test for anything else (we see test runner arguments here)
+ assertEquals(
+ process.argv[1],
+ path.fromFileUrl(Deno.mainModule),
+ );
},
});
@@ -136,9 +140,8 @@ Deno.test({
assertEquals(typeof (process.env.HELLO), "string");
assertEquals(process.env.HELLO, "WORLD");
- // TODO(caspervonb) test the globals in a different setting (they're broken)
- // assertEquals(typeof env.HELLO, "string");
- // assertEquals(env.HELLO, "WORLD");
+ assertEquals(typeof env.HELLO, "string");
+ assertEquals(env.HELLO, "WORLD");
},
});