diff --git a/cli/tests/integration/inspector_tests.rs b/cli/tests/integration/inspector_tests.rs index 520a5b4f78..e901e26331 100644 --- a/cli/tests/integration/inspector_tests.rs +++ b/cli/tests/integration/inspector_tests.rs @@ -519,3 +519,76 @@ async fn inspector_connect_non_ws() { child.kill().unwrap(); child.wait().unwrap(); } + +#[tokio::test] +async fn inspector_break_on_first_line_in_test() { + let script = util::testdata_path().join("inspector_test.js"); + let mut child = util::deno_cmd() + .arg("test") + .arg(inspect_flag_with_unique_port("--inspect-brk")) + .arg(script) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + let stderr = child.stderr.as_mut().unwrap(); + let mut stderr_lines = + std::io::BufReader::new(stderr).lines().map(|r| r.unwrap()); + let ws_url = extract_ws_url_from_stderr(&mut stderr_lines); + + let (socket, response) = tokio_tungstenite::connect_async(ws_url) + .await + .expect("Can't connect"); + assert_eq!(response.status(), 101); // Switching protocols. + + let (mut socket_tx, socket_rx) = socket.split(); + let mut socket_rx = + socket_rx.map(|msg| msg.unwrap().to_string()).filter(|msg| { + let pass = !msg.starts_with(r#"{"method":"Debugger.scriptParsed","#); + futures::future::ready(pass) + }); + + let stdout = child.stdout.as_mut().unwrap(); + let mut stdout_lines = + std::io::BufReader::new(stdout).lines().map(|r| r.unwrap()); + + use TestStep::*; + let test_steps = vec![ + WsSend(r#"{"id":1,"method":"Runtime.enable"}"#), + WsSend(r#"{"id":2,"method":"Debugger.enable"}"#), + WsRecv( + r#"{"method":"Runtime.executionContextCreated","params":{"context":{"id":1,"#, + ), + WsRecv(r#"{"id":1,"result":{}}"#), + WsRecv(r#"{"id":2,"result":{"debuggerId":"#), + WsSend(r#"{"id":3,"method":"Runtime.runIfWaitingForDebugger"}"#), + WsRecv(r#"{"id":3,"result":{}}"#), + WsRecv(r#"{"method":"Debugger.paused","#), + WsSend( + r#"{"id":4,"method":"Runtime.evaluate","params":{"expression":"Deno.core.print(\"hello from the inspector\\n\")","contextId":1,"includeCommandLineAPI":true,"silent":false,"returnByValue":true}}"#, + ), + WsRecv(r#"{"id":4,"result":{"result":{"type":"undefined"}}}"#), + StdOut("hello from the inspector"), + WsSend(r#"{"id":5,"method":"Debugger.resume"}"#), + WsRecv(r#"{"id":5,"result":{}}"#), + StdOut("running 1 test from"), + StdOut("test has finished running"), + ]; + + for step in test_steps { + match step { + StdOut(s) => assert!( + &stdout_lines.next().unwrap().contains(s), + "Doesn't contain {}", + s + ), + WsRecv(s) => assert!(socket_rx.next().await.unwrap().starts_with(s)), + WsSend(s) => socket_tx.send(s.into()).await.unwrap(), + _ => unreachable!(), + } + } + + child.kill().unwrap(); + child.wait().unwrap(); +} diff --git a/cli/tests/testdata/inspector_test.js b/cli/tests/testdata/inspector_test.js new file mode 100644 index 0000000000..86cd48854c --- /dev/null +++ b/cli/tests/testdata/inspector_test.js @@ -0,0 +1,3 @@ +Deno.test("basic test", () => { + console.log("test has finished running"); +}); diff --git a/runtime/worker.rs b/runtime/worker.rs index 545128dffb..087185729f 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -229,6 +229,7 @@ impl MainWorker { module_specifier: &ModuleSpecifier, ) -> Result<(), AnyError> { let id = self.preload_module(module_specifier, false).await?; + self.wait_for_inspector_session(); self.evaluate_module(id).await }