|
| 1 | +import { assert } from "chai"; |
| 2 | + |
| 3 | +// Use require instead of import in order to replace the `spawn` method of child_process |
| 4 | +const childProcess = require("child_process"); |
| 5 | + |
| 6 | +import { SpawnOptions, ChildProcess } from "child_process"; |
| 7 | +import * as path from "path"; |
| 8 | +import { EventEmitter } from "events"; |
| 9 | + |
| 10 | +describe("preuninstall.js", () => { |
| 11 | + let isSpawnCalled = false; |
| 12 | + let argsPassedToSpawn: string[] = []; |
| 13 | + let optionsPassedToSpawn: any[]; |
| 14 | + let dataPassedToConsoleError: string[] = []; |
| 15 | + const originalSpawn = childProcess.spawn; |
| 16 | + const originalConsoleError = console.error; |
| 17 | + let eventEmitter = new EventEmitter(); |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + isSpawnCalled = false; |
| 21 | + argsPassedToSpawn = []; |
| 22 | + dataPassedToConsoleError = []; |
| 23 | + optionsPassedToSpawn = []; |
| 24 | + eventEmitter = new EventEmitter(); |
| 25 | + childProcess.spawn = (command: string, args?: string[], options?: SpawnOptions): ChildProcess => { |
| 26 | + isSpawnCalled = true; |
| 27 | + argsPassedToSpawn = args; |
| 28 | + optionsPassedToSpawn.push(options); |
| 29 | + return <any>eventEmitter; |
| 30 | + }; |
| 31 | + |
| 32 | + console.error = (data: string) => { |
| 33 | + dataPassedToConsoleError.push(data); |
| 34 | + }; |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + childProcess.spawn = originalSpawn; |
| 39 | + console.error = originalConsoleError; |
| 40 | + }); |
| 41 | + |
| 42 | + it("calls dev-preuninstall command of CLI and prints with console.error the error in case childProcess emits error event", () => { |
| 43 | + require(path.join(__dirname, "..", "preuninstall")); |
| 44 | + |
| 45 | + assert.isTrue(isSpawnCalled, "child_process.spawn must be called from preuninstall.js"); |
| 46 | + |
| 47 | + const expectedPathToCliExecutable = path.join(__dirname, "..", "bin", "tns"); |
| 48 | + |
| 49 | + assert.isTrue(argsPassedToSpawn.indexOf(expectedPathToCliExecutable) !== -1, `The spawned args must contain path to TNS. |
| 50 | + Expected path is: ${expectedPathToCliExecutable}, current args are: ${argsPassedToSpawn}.`); |
| 51 | + assert.isTrue(argsPassedToSpawn.indexOf("dev-preuninstall") !== -1, `The spawned args must contain the name of the preuninstall command. |
| 52 | + Expected path is: ${expectedPathToCliExecutable}, current args are: ${argsPassedToSpawn}.`); |
| 53 | + assert.deepEqual(optionsPassedToSpawn, [{ stdio: "inherit" }], "The stdio must be inherit as this way CLI's command can determine correctly if terminal is in interactive mode."); |
| 54 | + assert.deepEqual(dataPassedToConsoleError, []); |
| 55 | + |
| 56 | + const errMsg = "this is error message"; |
| 57 | + eventEmitter.emit("error", new Error(errMsg)); |
| 58 | + assert.deepEqual(dataPassedToConsoleError, [`Failed to complete all pre-uninstall steps. Error is ${errMsg}`]); |
| 59 | + }); |
| 60 | + |
| 61 | + it("ensure package.json has correct preuninstall script", () => { |
| 62 | + const packageJsonData = require("../package.json"); |
| 63 | + assert.equal(packageJsonData.scripts.preuninstall, "node preuninstall.js"); |
| 64 | + }); |
| 65 | +}); |
0 commit comments