From 9026f20b2dc0ee97bde3cc6ea8438dafbec5fad3 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 26 Oct 2023 21:22:15 -0400 Subject: fix(unstable/byonm): improve error messages (#20987) This improves the error messages when a specifier can't be resolved from a deno module into an npm package. --- cli/tests/integration/npm_tests.rs | 51 +++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'cli/tests/integration') diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs index c3fe2949c..5104036e3 100644 --- a/cli/tests/integration/npm_tests.rs +++ b/cli/tests/integration/npm_tests.rs @@ -2313,6 +2313,55 @@ console.log(getKind()); output.assert_exit_code(1); } +#[test] +pub fn byonm_package_specifier_not_installed_and_invalid_subpath() { + let test_context = TestContextBuilder::for_npm() + .env("DENO_UNSTABLE_BYONM", "1") + .use_temp_cwd() + .build(); + let dir = test_context.temp_dir(); + dir.path().join("package.json").write_json(&json!({ + "dependencies": { + "chalk": "4", + "@denotest/conditional-exports-strict": "1" + } + })); + dir.write( + "main.ts", + "import chalk from 'chalk'; console.log(chalk.green('hi'));", + ); + + // no npm install has been run, so this should give an informative error + let output = test_context.new_command().args("run main.ts").run(); + output.assert_matches_text( + r#"error: Could not resolve "chalk", but found it in a package.json. Deno expects the node_modules/ directory to be up to date. Did you forget to run `npm install`? + at file:///[WILDCARD]/main.ts:1:19 +"#, + ); + output.assert_exit_code(1); + + // now test for an invalid sub path after doing an npm install + dir.write( + "main.ts", + "import '@denotest/conditional-exports-strict/test';", + ); + + test_context + .new_command() + .name("npm") + .args("install") + .run() + .skip_output_check(); + + let output = test_context.new_command().args("run main.ts").run(); + output.assert_matches_text( + r#"error: [ERR_PACKAGE_PATH_NOT_EXPORTED] Package subpath './test' is not defined by "exports" in '[WILDCARD]' imported from '[WILDCARD]main.ts' + at file:///[WILDCARD]/main.ts:1:8 +"#, + ); + output.assert_exit_code(1); +} + #[test] pub fn byonm_package_npm_specifier_not_installed_and_invalid_subpath() { let test_context = TestContextBuilder::for_npm() @@ -2334,7 +2383,7 @@ pub fn byonm_package_npm_specifier_not_installed_and_invalid_subpath() { // no npm install has been run, so this should give an informative error let output = test_context.new_command().args("run main.ts").run(); output.assert_matches_text( - r#"error: Could not find '[WILDCARD]package.json'. Maybe run `npm install`? + r#"error: Could not find '[WILDCARD]package.json'. Deno expects the node_modules/ directory to be up to date. Did you forget to run `npm install`? at file:///[WILDCARD]/main.ts:1:19 "#, ); -- cgit v1.2.3