From 6290de84e159d3df8879b32581a551352cc69d66 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Sun, 30 Jun 2024 03:37:51 +0800 Subject: [PATCH] test: correct test cases --- .../.eslintrc.json | 4 -- .../__tests__/test.spec.ts | 44 ----------------- .../index.html | 13 ----- .../package.json | 26 ---------- .../src/main.ts | 8 --- .../src/text.ts | 1 - .../tsconfig.json | 19 ------- .../vite.config.js | 15 ------ .../__tests__/__snapshots__/test.spec.ts.snap | 11 +++++ .../__tests__/test.spec.ts | 47 +++++++++--------- .../__tests__/__snapshots__/test.spec.ts.snap | 8 +-- .../__tests__/test.spec.ts | 49 +++++++++++-------- pnpm-lock.yaml | 34 ------------- 13 files changed, 68 insertions(+), 211 deletions(-) delete mode 100644 playground/config-initialIsOpen-error-clean/.eslintrc.json delete mode 100644 playground/config-initialIsOpen-error-clean/__tests__/test.spec.ts delete mode 100644 playground/config-initialIsOpen-error-clean/index.html delete mode 100644 playground/config-initialIsOpen-error-clean/package.json delete mode 100644 playground/config-initialIsOpen-error-clean/src/main.ts delete mode 100644 playground/config-initialIsOpen-error-clean/src/text.ts delete mode 100644 playground/config-initialIsOpen-error-clean/tsconfig.json delete mode 100644 playground/config-initialIsOpen-error-clean/vite.config.js create mode 100644 playground/config-initialIsOpen-error-warnings/__tests__/__snapshots__/test.spec.ts.snap diff --git a/playground/config-initialIsOpen-error-clean/.eslintrc.json b/playground/config-initialIsOpen-error-clean/.eslintrc.json deleted file mode 100644 index cef19347..00000000 --- a/playground/config-initialIsOpen-error-clean/.eslintrc.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "root": true, - "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"] -} diff --git a/playground/config-initialIsOpen-error-clean/__tests__/test.spec.ts b/playground/config-initialIsOpen-error-clean/__tests__/test.spec.ts deleted file mode 100644 index e7a2c75d..00000000 --- a/playground/config-initialIsOpen-error-clean/__tests__/test.spec.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { describe, expect, it } from 'vitest' - -import { - editFile, - getHmrOverlayText, - isServe, - sleepForEdit, - sleepForServerReady, -} from '../../testUtils' - -describe('config-initialIsOpen-error-clean', () => { - describe.runIf(isServe)('serve', () => { - it('should not find overlay', async () => { - if (isServe) { - await sleepForServerReady() - await expect(getHmrOverlayText()).rejects.toThrow( - 'Invariant failed: .message-body is expected in shadow root' - ) - - console.log('-- badge appears, but overlay remains collapsed after error introduced --') - editFile('src/main.ts', (code) => code.replace('const hello', 'var hello')) - await sleepForEdit() - try { - await getHmrOverlayText() - } catch (e) { - expect((e as any).toString()).toContain( - 'Invariant failed: shadow dom is expected to be found, but got null' - ) - } - - console.log('-- badge remains, overlay remains collapsed after warning introduced --') - editFile('src/main.ts', (code) => code.replace(' as', '! as')) - await sleepForEdit() - try { - await getHmrOverlayText() - } catch (e) { - expect((e as any).toString()).toContain( - 'Invariant failed: shadow dom is expected to be found, but got null' - ) - } - } - }) - }) -}) diff --git a/playground/config-initialIsOpen-error-clean/index.html b/playground/config-initialIsOpen-error-clean/index.html deleted file mode 100644 index 5f4067d6..00000000 --- a/playground/config-initialIsOpen-error-clean/index.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - Vite App - - -
- - - diff --git a/playground/config-initialIsOpen-error-clean/package.json b/playground/config-initialIsOpen-error-clean/package.json deleted file mode 100644 index c45e2631..00000000 --- a/playground/config-initialIsOpen-error-clean/package.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "private": true, - "name": "@playground/config-initial-is-open-error-clean", - "type": "module", - "version": "0.0.0", - "scripts": { - "dev": "vite", - "build": "vite build", - "serve": "vite preview", - "lint": "eslint --ext .js,.ts ./src/**" - }, - "dependencies": { - "react": "^17.0.0", - "react-dom": "^17.0.0" - }, - "devDependencies": { - "@types/react": "^17.0.0", - "@types/react-dom": "^17.0.0", - "@typescript-eslint/eslint-plugin": "^5.59.0", - "@typescript-eslint/parser": "^5.59.0", - "eslint": "^8.11.0", - "typescript": "^5.0.4", - "vite": "^4.3.0", - "vite-plugin-checker": "workspace:*" - } -} diff --git a/playground/config-initialIsOpen-error-clean/src/main.ts b/playground/config-initialIsOpen-error-clean/src/main.ts deleted file mode 100644 index 7f871824..00000000 --- a/playground/config-initialIsOpen-error-clean/src/main.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { text } from './text' - -const hello = 'Hello' - -const rootDom = document.querySelector('#root') as HTMLElement -rootDom.innerHTML = hello + text - -export {} diff --git a/playground/config-initialIsOpen-error-clean/src/text.ts b/playground/config-initialIsOpen-error-clean/src/text.ts deleted file mode 100644 index e583889a..00000000 --- a/playground/config-initialIsOpen-error-clean/src/text.ts +++ /dev/null @@ -1 +0,0 @@ -export const text = 'Vanilla JS/TS' diff --git a/playground/config-initialIsOpen-error-clean/tsconfig.json b/playground/config-initialIsOpen-error-clean/tsconfig.json deleted file mode 100644 index 36f84d9e..00000000 --- a/playground/config-initialIsOpen-error-clean/tsconfig.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "compilerOptions": { - "target": "ESNext", - "lib": ["DOM", "DOM.Iterable", "ESNext"], - "types": ["vite/client"], - "allowJs": false, - "skipLibCheck": false, - "esModuleInterop": false, - "allowSyntheticDefaultImports": true, - "strict": true, - "forceConsistentCasingInFileNames": true, - "module": "ESNext", - "moduleResolution": "Node", - "resolveJsonModule": true, - "isolatedModules": true, - "noEmit": true - }, - "include": ["./src"] -} diff --git a/playground/config-initialIsOpen-error-clean/vite.config.js b/playground/config-initialIsOpen-error-clean/vite.config.js deleted file mode 100644 index ddab80f7..00000000 --- a/playground/config-initialIsOpen-error-clean/vite.config.js +++ /dev/null @@ -1,15 +0,0 @@ -import { defineConfig } from 'vite' -import checker from 'vite-plugin-checker' - -export default defineConfig({ - plugins: [ - checker({ - overlay: { - initialIsOpen: 'error', - }, - eslint: { - lintCommand: 'eslint ./src --ext .ts', - }, - }), - ], -}) diff --git a/playground/config-initialIsOpen-error-warnings/__tests__/__snapshots__/test.spec.ts.snap b/playground/config-initialIsOpen-error-warnings/__tests__/__snapshots__/test.spec.ts.snap new file mode 100644 index 00000000..719f9bfc --- /dev/null +++ b/playground/config-initialIsOpen-error-warnings/__tests__/__snapshots__/test.spec.ts.snap @@ -0,0 +1,11 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`config-initialIsOpen-error-warnings > serve > should not show overlay when only warning exists 1`] = ` +" 1 | import { text } from './text' + 2 | + > 3 | var rootDom = document.querySelector('#root')! as HTMLElement + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + 4 | rootDom.innerHTML = text + 5 | + 6 | export {}" +`; diff --git a/playground/config-initialIsOpen-error-warnings/__tests__/test.spec.ts b/playground/config-initialIsOpen-error-warnings/__tests__/test.spec.ts index f09c0514..9fd8f7b4 100644 --- a/playground/config-initialIsOpen-error-warnings/__tests__/test.spec.ts +++ b/playground/config-initialIsOpen-error-warnings/__tests__/test.spec.ts @@ -1,31 +1,34 @@ import { describe, expect, it } from 'vitest' -import { editFile, getHmrOverlayText, isServe, sleepForEdit, sleepForServerReady } from '../../testUtils' +import { + editFile, + getHmrOverlay, + getHmrOverlayText, + isServe, + pollingUntil, + sleepForEdit, + sleepForServerReady, +} from '../../testUtils' describe('config-initialIsOpen-error-warnings', () => { describe.runIf(isServe)('serve', () => { - it('should not find overlay', async () => { - if (isServe) { - await sleepForServerReady() - try { - await getHmrOverlayText() - } catch (e) { - expect((e as any).toString()).toContain( - 'Invariant failed: shadow dom is expected to be found, but got null' - ) - } - - console.log('-- overlay remains closed after introduced error --') - editFile('src/main.ts', (code) => code.replace('! as', '! is')) - await sleepForEdit() - try { - await getHmrOverlayText() - } catch (e) { - expect((e as any).toString()).toContain( - 'Invariant failed: shadow dom is expected to be found, but got null' - ) - } + it('should not show overlay when only warning exists', async () => { + await sleepForServerReady() + try { + await getHmrOverlayText() + } catch (e) { + expect((e as any).toString()).toContain( + 'Invariant failed: shadow dom is expected to be found, but got null' + ) } + + console.log('-- overlay appears after introduce an error --') + editFile('src/main.ts', (code) => code.replace('const rootDom', 'var rootDom')) + await sleepForEdit() + await getHmrOverlayText() + await pollingUntil(getHmrOverlay, (dom) => !!dom) + const [, , frame] = await getHmrOverlayText() + expect(frame).toMatchSnapshot() }) }) }) diff --git a/playground/config-initialIsOpen-error/__tests__/__snapshots__/test.spec.ts.snap b/playground/config-initialIsOpen-error/__tests__/__snapshots__/test.spec.ts.snap index f253558a..d93e1d9c 100644 --- a/playground/config-initialIsOpen-error/__tests__/__snapshots__/test.spec.ts.snap +++ b/playground/config-initialIsOpen-error/__tests__/__snapshots__/test.spec.ts.snap @@ -1,10 +1,10 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`config-initialIsOpen-error > serve > should find overlay 1`] = `"Unexpected var, use let or const instead. (no-var)"`; +exports[`config-initialIsOpen-error > serve > -- should find overlay with error -- 1`] = `"Unexpected var, use let or const instead. (no-var)"`; -exports[`config-initialIsOpen-error > serve > should find overlay 2`] = `"/playground-temp/config-initialIsOpen-error/src/main.ts:3:1"`; +exports[`config-initialIsOpen-error > serve > -- should find overlay with error -- 2`] = `"/playground-temp/config-initialIsOpen-error/src/main.ts:3:1"`; -exports[`config-initialIsOpen-error > serve > should find overlay 3`] = ` +exports[`config-initialIsOpen-error > serve > -- should find overlay with error -- 3`] = ` " 1 | import { text } from './text' 2 | > 3 | var hello = 'Hello' @@ -14,4 +14,4 @@ exports[`config-initialIsOpen-error > serve > should find overlay 3`] = ` 6 | rootDom.innerHTML = hello + text" `; -exports[`config-initialIsOpen-error > serve > should find overlay 4`] = `""`; +exports[`config-initialIsOpen-error > serve > -- should find overlay with error -- 4`] = `""`; diff --git a/playground/config-initialIsOpen-error/__tests__/test.spec.ts b/playground/config-initialIsOpen-error/__tests__/test.spec.ts index 20c44c57..69a301cf 100644 --- a/playground/config-initialIsOpen-error/__tests__/test.spec.ts +++ b/playground/config-initialIsOpen-error/__tests__/test.spec.ts @@ -1,31 +1,38 @@ import { describe, expect, it } from 'vitest' -import { editFile, getHmrOverlay, getHmrOverlayText, isServe, pollingUntil, sleep, sleepForEdit, sleepForServerReady } from '../../testUtils' +import { + editFile, + getHmrOverlay, + getHmrOverlayText, + isServe, + pollingUntil, + sleep, + sleepForEdit, + sleepForServerReady, +} from '../../testUtils' describe('config-initialIsOpen-error', () => { describe.runIf(isServe)('serve', () => { - it('should find overlay', async () => { - if (isServe) { - await sleepForServerReady() - const [message1, file1, frame1] = await getHmrOverlayText() - expect(message1).toMatchSnapshot() - expect(file1).toMatchSnapshot() - expect(frame1).toMatchSnapshot() + it('-- should find overlay with error --', async () => { + await sleepForServerReady() + const [message1, file1, frame1] = await getHmrOverlayText() + expect(message1).toMatchSnapshot() + expect(file1).toMatchSnapshot() + expect(frame1).toMatchSnapshot() - console.log('-- overlay remains after fix error --') - editFile('src/main.ts', (code) => code.replace('var hello', `const hello`)) - await sleepForEdit() - await pollingUntil(getHmrOverlay, (dom) => !!dom) - const [, , frame2] = await getHmrOverlayText() - expect(frame2).toMatchSnapshot() + console.log('-- overlay remains after fix error --') + editFile('src/main.ts', (code) => code.replace('var hello', `const hello`)) + await sleepForEdit() + await pollingUntil(getHmrOverlay, (dom) => !!dom) + const [, , frame2] = await getHmrOverlayText() + expect(frame2).toMatchSnapshot() - console.log('-- overlay dismiss after fix warning --') - editFile('src/main.ts', (code) => code.replace('! as', ` as`)) - await sleep(6000) - await expect(getHmrOverlayText()).rejects.toThrow( - 'Invariant failed: .message-body is expected in shadow root' - ) - } + console.log('-- overlay dismiss after fix warning --') + editFile('src/main.ts', (code) => code.replace('! as', ` as`)) + await sleep(6000) + await expect(getHmrOverlayText()).rejects.toThrow( + 'Invariant failed: .message-body is expected in shadow root' + ) }) }) }) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 44191cc7..db184aa5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -400,40 +400,6 @@ importers: specifier: workspace:* version: link:../../packages/vite-plugin-checker - playground/config-initialIsOpen-error-clean: - dependencies: - react: - specifier: ^17.0.0 - version: 17.0.2 - react-dom: - specifier: ^17.0.0 - version: 17.0.2(react@17.0.2) - devDependencies: - '@types/react': - specifier: ^17.0.0 - version: 17.0.80 - '@types/react-dom': - specifier: ^17.0.0 - version: 17.0.25 - '@typescript-eslint/eslint-plugin': - specifier: ^5.59.0 - version: 5.62.0(@typescript-eslint/parser@5.62.0(eslint@8.57.0)(typescript@5.5.2))(eslint@8.57.0)(typescript@5.5.2) - '@typescript-eslint/parser': - specifier: ^5.59.0 - version: 5.62.0(eslint@8.57.0)(typescript@5.5.2) - eslint: - specifier: ^8.11.0 - version: 8.57.0 - typescript: - specifier: ^5.0.4 - version: 5.5.2 - vite: - specifier: ^4.3.0 - version: 4.5.3(@types/node@16.18.101)(sass@1.77.6) - vite-plugin-checker: - specifier: workspace:* - version: link:../../packages/vite-plugin-checker - playground/config-initialIsOpen-error-warnings: dependencies: react: