-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement REPL interface to debug tests
- Loading branch information
Showing
15 changed files
with
736 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
"use strict"; | ||
|
||
module.exports = ["assert-view", "getConfig", "getPuppeteer", "setOrientation", "scrollIntoView", "openAndWait"]; | ||
module.exports = [ | ||
"assert-view", | ||
"getConfig", | ||
"getPuppeteer", | ||
"setOrientation", | ||
"scrollIntoView", | ||
"openAndWait", | ||
"switchToRepl", | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import repl from "node:repl"; | ||
import path from "node:path"; | ||
import { getEventListeners } from "node:events"; | ||
import chalk from "chalk"; | ||
import RuntimeConfig from "../../config/runtime-config"; | ||
import logger from "../../utils/logger"; | ||
import type { Browser } from "../types"; | ||
|
||
const REPL_LINE_EVENT = "line"; | ||
|
||
export = async (browser: Browser): Promise<void> => { | ||
const { publicAPI: session } = browser; | ||
|
||
const applyContext = (replServer: repl.REPLServer, ctx: Record<string, unknown> = {}): void => { | ||
if (!ctx.browser) { | ||
ctx.browser = session; | ||
} | ||
|
||
for (const [key, value] of Object.entries(ctx)) { | ||
Object.defineProperty(replServer.context, key, { | ||
configurable: false, | ||
enumerable: true, | ||
value, | ||
}); | ||
} | ||
}; | ||
|
||
const handleLines = (replServer: repl.REPLServer): void => { | ||
const lineEvents = getEventListeners(replServer, REPL_LINE_EVENT); | ||
replServer.removeAllListeners(REPL_LINE_EVENT); | ||
|
||
replServer.on(REPL_LINE_EVENT, cmd => { | ||
const trimmedCmd = cmd.trim(); | ||
const newCmd = trimmedCmd.replace(/(let |const )/g, "var "); | ||
|
||
for (const event of lineEvents) { | ||
event(newCmd); | ||
} | ||
}); | ||
}; | ||
|
||
session.addCommand("switchToRepl", async function (ctx: Record<string, unknown> = {}) { | ||
const { replMode } = RuntimeConfig.getInstance(); | ||
const { onReplMode } = browser.state; | ||
|
||
if (!replMode?.enabled) { | ||
throw new Error( | ||
'Command "switchToRepl" available only in REPL mode, which can be started using cli option: "--repl", "--repl-before-test" or "--repl-on-fail"', | ||
); | ||
} | ||
|
||
if (onReplMode) { | ||
logger.warn(chalk.yellow("Hermione is already in REPL mode")); | ||
return; | ||
} | ||
|
||
logger.log(chalk.yellow("You have entered to REPL mode via terminal")); | ||
|
||
const currCwd = process.cwd(); | ||
const testCwd = path.dirname(session.executionContext.ctx.currentTest.file!); | ||
process.chdir(testCwd); | ||
|
||
const replServer = repl.start({ prompt: "> " }); | ||
browser.applyState({ onReplMode: true }); | ||
|
||
applyContext(replServer, ctx); | ||
handleLines(replServer); | ||
|
||
return new Promise(resolve => { | ||
return replServer.on("exit", () => { | ||
process.chdir(currCwd); | ||
browser.applyState({ onReplMode: false }); | ||
resolve(undefined); | ||
}); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.