Skip to content

Commit

Permalink
test: update win32 test serializer to work on any win32 machine (#313)
Browse files Browse the repository at this point in the history
* test: update win32 test serializer to work on any win32 machine

* fix: one more take on cwd normalization

* fix: add original replace

* fix: replace jest-serializer-path and  normalizeWindowsLogSerializer with simpler serializer

* fix: remove console logs
  • Loading branch information
armano2 authored Jun 29, 2024
1 parent 0747729 commit 230095f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 66 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 16 additions & 32 deletions playground/serializers.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
import os from '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 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, '/'), '<PROJECT_ROOT>')
function createResult(val: string) {
const cwd = normalizePaths(process.cwd())

if (doesUseDoubleSlashAsPath(result)) {
result = result.replaceAll(
`//a//vite-plugin-checker//vite-plugin-checker//playground-temp`,
'<PROJECT_ROOT>/playground-temp'
)
result = result.split('//').join('/')
}
}
return normalizePaths(val)
.replaceAll(cwd, '<PROJECT_ROOT>')
.replace(/\/r\/n/gim, '/n')
}

return serialize(result)
export const normalizeLogSerializer: SnapshotSerializer = {
print(val: string, print) {
return print(createResult(val))
},
test(val) {
if (typeof val !== 'string') return false

if (
(os.platform() === 'win32' &&
(val.includes(process.cwd().replace(winSepReg, '/')) || winNewLineReg.test(val))) ||
doesUseDoubleSlashAsPath(val)
) {
return true
}

return false
return typeof val === 'string' && val && createResult(val) !== val
},
}
6 changes: 2 additions & 4 deletions playground/vitestSetup.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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, '../')

Expand Down
29 changes: 0 additions & 29 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 230095f

Please sign in to comment.