Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: vitest tsconfig should include all files undersrc#56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
haoqunjiang merged 2 commits intovuejs:mainfromhaoqunjiang:fix-vitest-tsconfig
Feb 16, 2022

Conversation

haoqunjiang
Copy link
Member

@haoqunjianghaoqunjiang commentedFeb 15, 2022
edited
Loading

Credit to@xiaoxiangmoe

Fixes#55

Fixes the case that a.spec.ts file inside__test__/ imports a
module outside of__test__/ (a cross-(typescript-)project reference).

Note that VSCode language service looks forinclude patterns inreferences
top-down, sotsconfig.app.json must come beforetsconfig.vitest.json
so that thesrc/** modules can get the most-accurate type hints.

The previous tsconfig only works for.vue imports in.spec.ts, which
seems to be a Volar bug. We shouldn't rely on that.
This fix is a more accurate configuration.

Vitest TypeScript projects created prior tocreate-vue 3.1.6 can apply the following patch to the projects:

From a37c33facf0e5db767bee33a71ad9e9fcd3cf373 Mon Sep 17 00:00:00 2001From: Haoqun Jiang <haoqunjiang@gmail.com>Date: Tue, 15 Feb 2022 21:50:07 +0800Subject: [PATCH] fix: vitest tsconfig--- package.json         |  2 +- tsconfig.app.json    | 12 ++++++++++++ tsconfig.json        | 14 ++++---------- tsconfig.vitest.json |  9 +++------ 4 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 tsconfig.app.jsondiff --git a/package.json b/package.jsonindex ad9e16d..5138df6 100644--- a/package.json+++ b/package.json@@ -6,7 +6,7 @@     "build": "vue-tsc --noEmit && vite build",     "preview": "vite preview --port 5050",     "test:unit": "vitest --environment jsdom",-    "typecheck": "vue-tsc --noEmit && vue-tsc --noEmit -p tsconfig.vitest.json --composite false",+    "typecheck": "vue-tsc --noEmit -p tsconfig.vitest.json --composite false",     "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore"   },   "dependencies": {diff --git a/tsconfig.app.json b/tsconfig.app.jsonnew file mode 100644index 0000000..cdbea1d--- /dev/null+++ b/tsconfig.app.json@@ -0,0 +1,12 @@+{+  "extends": "@vue/tsconfig/tsconfig.web.json",+  "include": ["env.d.ts", "src/**/*", "src/**/*.vue"],+  "exclude": ["src/**/__tests__/*"],+  "compilerOptions": {+    "composite": true,+    "baseUrl": ".",+    "paths": {+      "@/*": ["./src/*"]+    }+  }+}diff --git a/tsconfig.json b/tsconfig.jsonindex 2551bf2..24f21b0 100644--- a/tsconfig.json+++ b/tsconfig.json@@ -1,18 +1,12 @@ {-  "extends": "@vue/tsconfig/tsconfig.web.json",-  "include": ["env.d.ts", "src/**/*", "src/**/*.vue"],-  "exclude": ["src/**/__tests__/*"],-  "compilerOptions": {-    "baseUrl": ".",-    "paths": {-      "@/*": ["./src/*"]-    }-  },-+  "files": [],   "references": [     {       "path": "./tsconfig.vite-config.json"     },+    {+      "path": "./tsconfig.app.json"+    },     {       "path": "./tsconfig.vitest.json"     }diff --git a/tsconfig.vitest.json b/tsconfig.vitest.jsonindex 5667568..d080d61 100644--- a/tsconfig.vitest.json+++ b/tsconfig.vitest.json@@ -1,12 +1,9 @@ {-  "extends": "@vue/tsconfig/tsconfig.node.json",-  "include": ["src/**/__tests__/*"],+  "extends": "./tsconfig.app.json",+  "exclude": [],   "compilerOptions": {     "composite": true,-    "baseUrl": ".",-    "paths": {-      "@/*": ["./src/*"]-    },+    "lib": [],     "types": ["node", "jsdom"]   } }--2.35.1

xiaoxiangmoe reacted with thumbs up emoji
Fixesvuejs#55Fixes the case that a `.spec.ts` file inside `__test__/` imports amodule outside of `__test__/` (a cross-(typescript-)project reference).Note that TypeScript looks for `include` patterns in `references`top-down, so `tsconfig.app.json` must come before `tsconfig.vitest.json`so that the `src/**` modules can get the most-accurate type hints.The previous tsconfig only works for `.vue` imports in `.spec.ts`, whichseems to be a Volar bug. We shouldn't rely on that.This fix is a more accurate configuration.
@haoqunjiang
Copy link
MemberAuthor

Cc@cexbrayat I think you might be interested in this fix, too…

@cexbrayat
Copy link
Member

Hmm interesting, that's what we were missing.

One question though: where does ts know that.spec.ts are included now? they are excluded intsconfig.app.json and never included?

@haoqunjiang
Copy link
MemberAuthor

haoqunjiang commentedFeb 15, 2022
edited
Loading

One question though: where does ts know that.spec.ts are included now? they are excluded intsconfig.app.json and never included?

tsconfig.vitest.json now extends fromtsconfig.app.json and overwritesexclude to[]. So now the project (tsconfig.vitest.json) includes all files undersrc/**

So now:

  • When type-checkingsrc/utils.ts, VSCode language service will search project reference, and the file is matched by the first tsconfig in the array,tsconfig.app.json, which is the same behavior as previous;
  • When type-checkingsrc/__tests__/utils.spec.ts and it encounters the import for../utils.ts, TypeScript would also use the ts configuration ofutils.spec.ts for the imported module. Now thattsconfig.vitest.json includes all files undersrc, it no longer complains.
cexbrayat, xiaoxiangmoe, and do-adams reacted with thumbs up emoji

@xiaoxiangmoe
Copy link
Contributor

cypress-ct should also add vitest styletsconfig.app.json andtsconfig.cypress-ct.json.
If we don't add this:

  • filesrc/App.vue and filesrc/__tests__/utils.spec.ts will be matched in same tsconfig.
  • src/__tests__/utils.spec.ts have codeimport { mount } from '@cypress/vue', it will introduce global variables likeit,describe and so on.
  • src/App.vue will be able to use global variableit,describe.

So we should split tsconfig to prevent global variables pollution.

@paparent
Copy link

Regarding global variable pollution, I managed to do this in mytsconfig.vitest.json:

{"extends":"@vue/tsconfig/tsconfig.node.json","include": ["test-setup.ts","src/**/__tests__/*"],"compilerOptions": {"composite":true,"baseUrl":".","paths": {"@/*": ["./src/*"]    },"types": ["node","jsdom","vitest/globals"]  }}

havingvitest/globals in thetypes.

I like having different tsconfig files, but it gets confusing!

@haoqunjiang
Copy link
MemberAuthor

cypress-ct should also add vitest styletsconfig.app.json andtsconfig.cypress-ct.json.

Makes sense. Let me merge this PR and release a patch first. And I'll fix the Cypress-CT issue in another PR. At least it is still working…

@haoqunjianghaoqunjiang merged commitfaabf60 intovuejs:mainFeb 16, 2022
@haoqunjianghaoqunjiang deleted the fix-vitest-tsconfig branchFebruary 16, 2022 06:34
haoqunjiang added a commit to haoqunjiang/create-vue that referenced this pull requestFeb 18, 2022
do-adams added a commit to do-adams/talkee that referenced this pull requestApr 16, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Projects must list all files or use an 'include' pattern.ts(6307)
4 participants
@haoqunjiang@cexbrayat@xiaoxiangmoe@paparent

[8]ページ先頭

©2009-2025 Movatter.jp