From d88ec577c101d0defc5dddf9a54a9cc751009a55 Mon Sep 17 00:00:00 2001 From: armano Date: Sat, 6 Apr 2024 14:00:41 +0200 Subject: [PATCH 1/5] test: update win32 test serializer to work on any win32 machine --- playground/serializers.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/playground/serializers.ts b/playground/serializers.ts index 984c58e2..459be669 100644 --- a/playground/serializers.ts +++ b/playground/serializers.ts @@ -1,21 +1,21 @@ -import os from 'os' +import os from 'node:os' +import { resolve } from 'node:path' -const winPathReg = /(\/)?(\D:\/)?\D\/vite-plugin-checker\/vite-plugin-checker\/playground-temp/im const winNewLineReg = /\/r\/n/gim function doesUseDoubleSlashAsPath(val: string) { return val.includes('//vite-plugin-checker//') } +const workspaceRoot = resolve(__dirname, '../').replace(/\\/g, '/') + export const normalizeWindowsLogSerializer = { print(val, serialize) { let result = val if (os.platform() === 'win32') { result = result.replace(winNewLineReg, '/n') - if (winPathReg.test(result)) { - result = result.replace(winPathReg, '/playground-temp') - } + result = result.replace(workspaceRoot, '') if (doesUseDoubleSlashAsPath(result)) { result = result.replace( @@ -32,7 +32,7 @@ export const normalizeWindowsLogSerializer = { if (typeof val !== 'string') return false if ( - (os.platform() === 'win32' && (winPathReg.test(val) || winNewLineReg.test(val))) || + (os.platform() === 'win32' && (val.includes(workspaceRoot) || winNewLineReg.test(val))) || doesUseDoubleSlashAsPath(val) ) { return true From 56761d092f4c80f321da96bb1bab755f10edc45c Mon Sep 17 00:00:00 2001 From: armano Date: Sat, 29 Jun 2024 10:51:19 +0200 Subject: [PATCH 2/5] fix: one more take on cwd normalization --- playground/serializers.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/playground/serializers.ts b/playground/serializers.ts index c1a646d7..60d2309c 100644 --- a/playground/serializers.ts +++ b/playground/serializers.ts @@ -7,12 +7,19 @@ function doesUseDoubleSlashAsPath(val: string) { return val.includes('//vite-plugin-checker//') } +function getNormalizedCwd() { + return process + .cwd() + .replace(/[a-zA-Z]:\\/g, '\\') + .replace(winSepReg, '/') +} + export const normalizeWindowsLogSerializer = { print(val: string, serialize) { let result = val if (os.platform() === 'win32') { result = result.replaceAll(winNewLineReg, '/n') - result = result.replaceAll(process.cwd().replace(winSepReg, '/'), '') + result = result.replaceAll(getNormalizedCwd(), '') if (doesUseDoubleSlashAsPath(result)) { result = result.replaceAll( @@ -30,7 +37,7 @@ export const normalizeWindowsLogSerializer = { if ( (os.platform() === 'win32' && - (val.includes(process.cwd().replace(winSepReg, '/')) || winNewLineReg.test(val))) || + (val.includes(getNormalizedCwd()) || winNewLineReg.test(val))) || doesUseDoubleSlashAsPath(val) ) { return true From 4bad9799563525ade808edfb0fe86e8342c0c730 Mon Sep 17 00:00:00 2001 From: armano Date: Sat, 29 Jun 2024 10:57:59 +0200 Subject: [PATCH 3/5] fix: add original replace --- playground/serializers.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/playground/serializers.ts b/playground/serializers.ts index 60d2309c..47919f44 100644 --- a/playground/serializers.ts +++ b/playground/serializers.ts @@ -19,6 +19,7 @@ export const normalizeWindowsLogSerializer = { let result = val if (os.platform() === 'win32') { result = result.replaceAll(winNewLineReg, '/n') + result = result.replaceAll(process.cwd().replace(winSepReg, '/'), '') result = result.replaceAll(getNormalizedCwd(), '') if (doesUseDoubleSlashAsPath(result)) { From 4b51c377d606afa61cdfb2af4399c3cf873deab8 Mon Sep 17 00:00:00 2001 From: armano Date: Sat, 29 Jun 2024 12:29:27 +0200 Subject: [PATCH 4/5] fix: replace jest-serializer-path and normalizeWindowsLogSerializer with simpler serializer --- package.json | 1 - playground/serializers.ts | 58 ++++++++++++--------------------------- playground/vitestSetup.ts | 6 ++-- pnpm-lock.yaml | 29 -------------------- 4 files changed, 20 insertions(+), 74 deletions(-) diff --git a/package.json b/package.json index 69e03173..41234757 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,6 @@ "fast-glob": "^3.2.7", "fast-json-stable-stringify": "^2.1.0", "fs-extra": "^11.1.0", - "jest-serializer-path": "^0.1.15", "klaw": "^4.0.1", "lint-staged": "^11.0.0", "minimist": "^1.2.5", diff --git a/playground/serializers.ts b/playground/serializers.ts index 47919f44..50c04e18 100644 --- a/playground/serializers.ts +++ b/playground/serializers.ts @@ -1,49 +1,27 @@ -import os from 'node:os' +import type { SnapshotSerializer } from 'vitest' -const winNewLineReg = /\/r\/n/gim -const winSepReg = /\\/g - -function doesUseDoubleSlashAsPath(val: string) { - return val.includes('//vite-plugin-checker//') -} - -function getNormalizedCwd() { - return process - .cwd() - .replace(/[a-zA-Z]:\\/g, '\\') - .replace(winSepReg, '/') +function normalizePaths(val: string) { + return val + .replace(/\\/gim, '/') // replace slashes + .replace(/\/\//gim, '/') // replace slashes + .replace(/[a-zA-Z]:\//gim, '/') // Remove win32 drive letters, C:\ -> \ } -export const normalizeWindowsLogSerializer = { - print(val: string, serialize) { - let result = val - if (os.platform() === 'win32') { - result = result.replaceAll(winNewLineReg, '/n') - result = result.replaceAll(process.cwd().replace(winSepReg, '/'), '') - result = result.replaceAll(getNormalizedCwd(), '') +function createResult(val: string) { + const cwd = normalizePaths(process.cwd()) - if (doesUseDoubleSlashAsPath(result)) { - result = result.replaceAll( - `//a//vite-plugin-checker//vite-plugin-checker//playground-temp`, - '/playground-temp' - ) - result = result.split('//').join('/') - } - } + return normalizePaths(val) + .replaceAll(cwd, '') + .replace(/\/r\/n/gim, '/n') +} - return serialize(result) +export const normalizeLogSerializer: SnapshotSerializer = { + print(val: string, print) { + console.log('normalizeLogSerializer: print') + return print(createResult(val)) }, test(val) { - if (typeof val !== 'string') return false - - if ( - (os.platform() === 'win32' && - (val.includes(getNormalizedCwd()) || winNewLineReg.test(val))) || - doesUseDoubleSlashAsPath(val) - ) { - return true - } - - return false + console.log('normalizeLogSerializer: test') + return typeof val === 'string' && val && createResult(val) !== val }, } diff --git a/playground/vitestSetup.ts b/playground/vitestSetup.ts index b0bcfeb3..b71e4c45 100644 --- a/playground/vitestSetup.ts +++ b/playground/vitestSetup.ts @@ -1,6 +1,5 @@ import execa from 'execa' import fs from 'fs-extra' -import pathSerializer from 'jest-serializer-path' import type * as http from 'node:http' import os from 'node:os' import path, { dirname, join, resolve } from 'node:path' @@ -10,14 +9,13 @@ import { createServer, mergeConfig } from 'vite' import { beforeAll, expect } from 'vitest' import type { Checker } from 'vite-plugin-checker/dist/Checker' -import { normalizeWindowsLogSerializer } from './serializers' +import { normalizeLogSerializer } from './serializers' import type { Browser, Page } from 'playwright-chromium' import type { InlineConfig, ResolvedConfig, ViteDevServer } from 'vite' import type { File } from 'vitest' -expect.addSnapshotSerializer(pathSerializer) -expect.addSnapshotSerializer(normalizeWindowsLogSerializer) +expect.addSnapshotSerializer(normalizeLogSerializer) export const workspaceRoot = resolve(__dirname, '../') diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3017bc75..5155f2d7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -79,9 +79,6 @@ importers: fs-extra: specifier: ^11.1.0 version: 11.2.0 - jest-serializer-path: - specifier: ^0.1.15 - version: 0.1.15 klaw: specifier: ^4.0.1 version: 4.1.0 @@ -4128,10 +4125,6 @@ packages: resolution: {integrity: sha512-JVYhQnN59LVPFCEcVa2C3CrEKYacvjRfqIQl+h8oi91aLYQVWRYbxjPcv1bUiUy/kLmQaANrYfNMCO3kuEDHfw==} engines: {node: '>=14'} - jest-serializer-path@0.1.15: - resolution: {integrity: sha512-sXb+Ckz9LK5bB5sRAXFeG/nwhGn1XBKayy9xhPpgOmAWaljJiS+VN/xJ3P4I19jZ+WejowvO/kES+A9HePTMvQ==} - engines: {node: '>=6.4.x', npm: '>=3.10.3'} - joycon@3.1.1: resolution: {integrity: sha512-34wB/Y7MW7bzjKRjUKTa46I2Z7eV62Rkhva+KkopW7Qvv/OSWBqvkSY7vusOPrNuZcUG3tApvdVgNB8POj3SPw==} engines: {node: '>=10'} @@ -4254,12 +4247,6 @@ packages: resolution: {integrity: sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==} engines: {node: '>=10'} - lodash.clonedeep@4.5.0: - resolution: {integrity: sha512-H5ZhCF25riFd9uB5UCkVKo61m3S/xZk1x4wA6yp/L3RFP6Z/eHH1ymQcGLo7J3GMPfm0V/7m1tryHuGVxpqEBQ==} - - lodash.iserror@3.1.1: - resolution: {integrity: sha512-eT/VeNns9hS7vAj1NKW/rRX6b+C3UX3/IAAqEE7aC4Oo2C0iD82NaP5IS4bSlQsammTii4qBJ8G1zd1LTL8hCw==} - lodash.kebabcase@4.1.1: resolution: {integrity: sha512-N8XRTIMMqqDgSy4VLKPnJ/+hpGZN+PHQiJnSenYqPaVV/NCqEogTnAdZLQiGKhxX+JCs8waWq2t1XHWKOmlY8g==} @@ -5054,10 +5041,6 @@ packages: sisteransi@1.0.5: resolution: {integrity: sha512-bLGGlR1QxBcynn2d5YmDX4MGjlZvy2MRBDRNHLJ8VI6l6+9FUiyTFNJ0IveOSP0bcXgVDPRcfGqA0pjaqUpfVg==} - slash@2.0.0: - resolution: {integrity: sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A==} - engines: {node: '>=6'} - slash@3.0.0: resolution: {integrity: sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==} engines: {node: '>=8'} @@ -8987,12 +8970,6 @@ snapshots: optionalDependencies: '@pkgjs/parseargs': 0.11.0 - jest-serializer-path@0.1.15: - dependencies: - lodash.clonedeep: 4.5.0 - lodash.iserror: 3.1.1 - slash: 2.0.0 - joycon@3.1.1: {} js-tokens@4.0.0: {} @@ -9116,10 +9093,6 @@ snapshots: dependencies: p-locate: 5.0.0 - lodash.clonedeep@4.5.0: {} - - lodash.iserror@3.1.1: {} - lodash.kebabcase@4.1.1: {} lodash.merge@4.6.2: {} @@ -9893,8 +9866,6 @@ snapshots: sisteransi@1.0.5: {} - slash@2.0.0: {} - slash@3.0.0: {} slice-ansi@3.0.0: From 2d869a0150455cf07b95c948f2d080a17771136c Mon Sep 17 00:00:00 2001 From: armano Date: Sat, 29 Jun 2024 12:30:38 +0200 Subject: [PATCH 5/5] fix: remove console logs --- playground/serializers.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/playground/serializers.ts b/playground/serializers.ts index 50c04e18..43fa7c8b 100644 --- a/playground/serializers.ts +++ b/playground/serializers.ts @@ -17,11 +17,9 @@ function createResult(val: string) { export const normalizeLogSerializer: SnapshotSerializer = { print(val: string, print) { - console.log('normalizeLogSerializer: print') return print(createResult(val)) }, test(val) { - console.log('normalizeLogSerializer: test') return typeof val === 'string' && val && createResult(val) !== val }, }