summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-04-15 17:50:52 -0400
committerGitHub <noreply@github.com>2024-04-15 17:50:52 -0400
commit6f278e5c40d101f0fb8e7b69e28d34b1c766a8fe (patch)
tree97d3edc0f0b527c3dc7f07ba71d5828cd2c77943 /tests
parent7e4ee02e2e37db8adfaf4a05aba3819838904650 (diff)
fix(lsp): improved cjs tracking (#23374)
Our cjs tracking was a bit broken. It was marking stuff as esm that was actually cjs leading to type checking errors.
Diffstat (limited to 'tests')
-rw-r--r--tests/Cargo.toml2
-rw-r--r--tests/integration/lsp_tests.rs49
-rw-r--r--tests/specs/mod.rs31
-rw-r--r--tests/specs/npm/cjs_internal_types_default_export/__test__.jsonc15
-rw-r--r--tests/specs/npm/cjs_internal_types_default_export/main.out5
-rw-r--r--tests/specs/npm/cjs_internal_types_default_export/main.ts3
-rw-r--r--tests/specs/npm/cjs_internal_types_default_export/package.json5
-rw-r--r--tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/add.d.ts3
-rw-r--r--tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/index.d.ts1
-rw-r--r--tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/index.js1
-rw-r--r--tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/package.json4
11 files changed, 105 insertions, 14 deletions
diff --git a/tests/Cargo.toml b/tests/Cargo.toml
index b0f9ff0af..5552f6f31 100644
--- a/tests/Cargo.toml
+++ b/tests/Cargo.toml
@@ -43,7 +43,7 @@ deno_lockfile.workspace = true
deno_terminal.workspace = true
deno_tls.workspace = true
fastwebsockets = { workspace = true, features = ["upgrade", "unstable-split"] }
-file_test_runner = "0.2.0"
+file_test_runner = "0.4.0"
flaky_test = "=0.1.0"
http.workspace = true
http-body-util.workspace = true
diff --git a/tests/integration/lsp_tests.rs b/tests/integration/lsp_tests.rs
index a3d2cf57e..76d6e22e1 100644
--- a/tests/integration/lsp_tests.rs
+++ b/tests/integration/lsp_tests.rs
@@ -12287,3 +12287,52 @@ fn lsp_uses_lockfile_for_npm_initialization() {
assert_eq!(skipping_count, 1);
client.shutdown();
}
+
+#[test]
+fn lsp_cjs_internal_types_default_export() {
+ let context = TestContextBuilder::new()
+ .use_http_server()
+ .use_temp_cwd()
+ .add_npm_env_vars()
+ .env("DENO_FUTURE", "1")
+ .build();
+ let temp_dir = context.temp_dir();
+ temp_dir.write("deno.json", r#"{}"#);
+ temp_dir.write(
+ "package.json",
+ r#"{
+ "dependencies": {
+ "@denotest/cjs-internal-types-default-export": "*"
+ }
+}"#,
+ );
+ context.run_npm("install");
+
+ let mut client = context.new_lsp_command().build();
+ client.initialize_default();
+ // this was previously being resolved as ESM and not correctly as CJS
+ let node_modules_index_d_ts = temp_dir.path().join(
+ "node_modules/@denotest/cjs-internal-types-default-export/index.d.ts",
+ );
+ client.did_open(json!({
+ "textDocument": {
+ "uri": node_modules_index_d_ts.uri_file(),
+ "languageId": "typescript",
+ "version": 1,
+ "text": node_modules_index_d_ts.read_to_string(),
+ }
+ }));
+ let main_url = temp_dir.path().join("main.ts").uri_file();
+ let diagnostics = client.did_open(
+ json!({
+ "textDocument": {
+ "uri": main_url,
+ "languageId": "typescript",
+ "version": 1,
+ "text": "import * as mod from '@denotest/cjs-internal-types-default-export';\nmod.add(1, 2);",
+ }
+ }),
+ );
+ // previously, diagnostic about `add` not being callable
+ assert_eq!(json!(diagnostics.all()), json!([]));
+}
diff --git a/tests/specs/mod.rs b/tests/specs/mod.rs
index 1028519cf..2040eea62 100644
--- a/tests/specs/mod.rs
+++ b/tests/specs/mod.rs
@@ -9,6 +9,10 @@ use std::sync::Arc;
use deno_core::anyhow::Context;
use deno_core::serde_json;
+use file_test_runner::collection::collect_tests_or_exit;
+use file_test_runner::collection::strategies::TestPerDirectoryCollectionStrategy;
+use file_test_runner::collection::CollectOptions;
+use file_test_runner::collection::CollectedTest;
use serde::Deserialize;
use test_util::tests_path;
use test_util::PathRef;
@@ -69,6 +73,7 @@ struct StepMetaData {
pub clean_deno_dir: bool,
pub args: VecOrString,
pub cwd: Option<String>,
+ pub command_name: Option<String>,
#[serde(default)]
pub envs: HashMap<String, String>,
pub output: String,
@@ -77,15 +82,13 @@ struct StepMetaData {
}
pub fn main() {
- let root_category =
- file_test_runner::collect_tests_or_exit(file_test_runner::CollectOptions {
- base: tests_path().join("specs").to_path_buf(),
- strategy: file_test_runner::FileCollectionStrategy::TestPerDirectory {
- file_name: MANIFEST_FILE_NAME.to_string(),
- },
- root_category_name: "specs".to_string(),
- filter_override: None,
- });
+ let root_category = collect_tests_or_exit(CollectOptions {
+ base: tests_path().join("specs").to_path_buf(),
+ strategy: Box::new(TestPerDirectoryCollectionStrategy {
+ file_name: MANIFEST_FILE_NAME.to_string(),
+ }),
+ filter_override: None,
+ });
if root_category.is_empty() {
return; // all tests filtered out
@@ -111,15 +114,13 @@ pub fn main() {
output.extend(panic_output);
file_test_runner::TestResult::Failed { output }
}
+ file_test_runner::TestResult::Steps(_) => unreachable!(),
}
}),
);
}
-fn run_test(
- test: &file_test_runner::CollectedTest,
- diagnostic_logger: Rc<RefCell<Vec<u8>>>,
-) {
+fn run_test(test: &CollectedTest, diagnostic_logger: Rc<RefCell<Vec<u8>>>) {
let metadata_path = PathRef::new(&test.path);
let metadata_value = metadata_path.read_jsonc_value();
// checking for "steps" leads to a more targeted error message
@@ -182,6 +183,10 @@ fn run_test(
Some(cwd) => command.current_dir(cwd),
None => command,
};
+ let command = match &step.command_name {
+ Some(command_name) => command.name(command_name),
+ None => command,
+ };
let output = command.run();
if step.output.ends_with(".out") {
let test_output_path = cwd.join(&step.output);
diff --git a/tests/specs/npm/cjs_internal_types_default_export/__test__.jsonc b/tests/specs/npm/cjs_internal_types_default_export/__test__.jsonc
new file mode 100644
index 000000000..dc8aabedb
--- /dev/null
+++ b/tests/specs/npm/cjs_internal_types_default_export/__test__.jsonc
@@ -0,0 +1,15 @@
+{
+ "tempDir": true,
+ "envs": {
+ "DENO_FUTURE": "1"
+ },
+ "steps": [{
+ "commandName": "npm",
+ "args": "install",
+ "output": "[WILDCARD]"
+ }, {
+ "args": "check main.ts",
+ "exitCode": 1,
+ "output": "main.out"
+ }]
+}
diff --git a/tests/specs/npm/cjs_internal_types_default_export/main.out b/tests/specs/npm/cjs_internal_types_default_export/main.out
new file mode 100644
index 000000000..66ec3f37b
--- /dev/null
+++ b/tests/specs/npm/cjs_internal_types_default_export/main.out
@@ -0,0 +1,5 @@
+Check file:///[WILDLINE]/main.ts
+error: TS2345 [ERROR]: Argument of type 'string' is not assignable to parameter of type 'number'.
+add(1, "test"); // should error
+ ~~~~~~
+ at file:///[WILDLINE]/main.ts:3:8
diff --git a/tests/specs/npm/cjs_internal_types_default_export/main.ts b/tests/specs/npm/cjs_internal_types_default_export/main.ts
new file mode 100644
index 000000000..339efcc59
--- /dev/null
+++ b/tests/specs/npm/cjs_internal_types_default_export/main.ts
@@ -0,0 +1,3 @@
+import { add } from "@denotest/cjs-internal-types-default-export";
+
+add(1, "test"); // should error
diff --git a/tests/specs/npm/cjs_internal_types_default_export/package.json b/tests/specs/npm/cjs_internal_types_default_export/package.json
new file mode 100644
index 000000000..f97b7b565
--- /dev/null
+++ b/tests/specs/npm/cjs_internal_types_default_export/package.json
@@ -0,0 +1,5 @@
+{
+ "dependencies": {
+ "@denotest/cjs-internal-types-default-export": "*"
+ }
+}
diff --git a/tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/add.d.ts b/tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/add.d.ts
new file mode 100644
index 000000000..0b38dc4fc
--- /dev/null
+++ b/tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/add.d.ts
@@ -0,0 +1,3 @@
+const _default: (a: number, b: number) => number;
+
+export default _default;
diff --git a/tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/index.d.ts b/tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/index.d.ts
new file mode 100644
index 000000000..bfde9725e
--- /dev/null
+++ b/tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/index.d.ts
@@ -0,0 +1 @@
+export { default as add } from './add'; \ No newline at end of file
diff --git a/tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/index.js b/tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/index.js
new file mode 100644
index 000000000..62c45aa26
--- /dev/null
+++ b/tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/index.js
@@ -0,0 +1 @@
+module.exports.add = (a, b) => a + b;
diff --git a/tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/package.json b/tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/package.json
new file mode 100644
index 000000000..57b3b9e4a
--- /dev/null
+++ b/tests/testdata/npm/registry/@denotest/cjs-internal-types-default-export/1.0.0/package.json
@@ -0,0 +1,4 @@
+{
+ "name": "@denotest/cjs-internal-types-default-export",
+ "version": "1.0.0"
+}