Olaf Bohlen
2023-02-06 4441766567f5f2402d662c29fd24cd0be7060075
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { exec } from "child_process";
 
const cmd = "node dist/index.js";
// const cmd = "exchange";
 
describe("functional tests", () => {
  describe("miscellaneous flags", () => {
    it("should print help", async () => {
      // GIVEN the command is available on PATH
 
      // WHEN the command is run with the help flag
      const res = await runCommand(`${cmd} --help`);
 
      // THEN the CLI should print help info
      expect(res.stdout).toContain("Convert from one currency to another");
    });
 
    it("should check for missing flags", async () => {
      // GIVEN the command is available on PATH
 
      // WHEN the command is run with no arguments
      try {
        await runCommand(cmd);
        fail();
      } catch (e) {
        expect(e).toContain("Missing required flags");
      }
 
      // THEN the CLI should print missing args
    });
  });
 
  describe("conversions", () => {
    it('should do a simple "conversion"', async () => {
      // GIVEN the command is available on PATH
 
      // WHEN the command is run with `exchange -i usd -o usd 3.5`
      const res = await runCommand(`${cmd} -i usd -o usd 3.5`);
 
      // THEN the CLI should convert from USD to USD
      expect(res.stdout).toMatch("3.5 USD -> 3.5 USD");
    });
 
    it("should convert different currencies", async () => {
      // GIVEN the command is available on PATH
 
      // WHEN the command is run with `exchange -i usd -o jpy 3.5`
      const res = await runCommand(`${cmd} -i usd -o jpy 3.5`);
 
      // THEN the CLI should convert between two different currencies
      expect(res.stdout).toMatch("3.5 USD -> 364.6 JPY");
    });
  });
 
  async function runCommand(
    cmd: string
  ): Promise<{ stdout: string; stderr: string }> {
    return new Promise((resolve, reject) => {
      exec(cmd, (err, stdout, stderr) => {
        if (err) {
          reject(stderr);
        } else {
          resolve({ stdout, stderr });
        }
      });
    });
  }
});