Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-serializer-path

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-serializer-path - npm Package Compare versions

Comparing version 0.1.9 to 0.1.10

89

lib/index_test.js

@@ -5,2 +5,3 @@ 'use strict'

const Serializer = require('./')
const os = require('os')

@@ -110,2 +111,17 @@ const normalizePaths = Serializer.normalizePaths

it('replaces all roots', () => {
const homeDir = os.homedir()
const tempDir = os.tmpdir()
const cwd = process.cwd()
const sut = {
home: path.resolve(homeDir, 'nested/home'),
temp: path.resolve(tempDir, 'nested/temp'),
cwd: path.resolve(cwd, 'nested/cwd'),
}
expect(sut).toMatchSnapshot()
})
})

@@ -260,2 +276,75 @@

it('replaces home directory with HOME_DIR', () => {
const homeDir = os.homedir()
const value = path.resolve(homeDir, 'some/nested/dir')
const normalized = normalizePaths(value)
expect(normalized).toEqual('<HOME_DIR>/some/nested/dir')
})
it('replaces temp directory with TEMP_DIR', () => {
const tempDir = os.tmpdir()
const value = path.resolve(tempDir, 'some/nested/dir')
const normalized = normalizePaths(value)
expect(normalized).toEqual('<TEMP_DIR>/some/nested/dir')
})
it('use <PROJECT_ROOT> when nested inside <HOME_DIR>', () => {
const cwd = process.cwd()
const homeDir = os.homedir()
process.chdir(homeDir)
const value = path.resolve(homeDir, 'some/nested/dir')
const normalized = normalizePaths(value)
expect(normalized).toEqual('<PROJECT_ROOT>/some/nested/dir')
process.chdir(cwd)
})
it('use <TEMP_DIR> when nested inside <HOME_DIR>', () => {
const homeDir = os.homedir()
const tempDir = path.resolve(homeDir, 'temp-dir')
const tmpDirSpy = jest.spyOn(os, 'tmpdir')
.mockImplementation(() => tempDir)
const value = path.resolve(tempDir, 'some/nested/dir')
const normalized = normalizePaths(value)
expect(normalized).toEqual('<TEMP_DIR>/some/nested/dir')
tmpDirSpy.mockRestore()
})
it('use <HOME_DIR> when nested inside <TEMP_DIR>', () => {
const tempDir = os.tmpdir()
const homeDir = path.resolve(tempDir, 'home-dir')
const homeDirSpy = jest.spyOn(os, 'homedir')
.mockImplementation(() => homeDir)
const value = path.resolve(homeDir, 'some/nested/dir')
const normalized = normalizePaths(value)
expect(normalized).toEqual('<HOME_DIR>/some/nested/dir')
homeDirSpy.mockRestore()
})
})

35

lib/index.js

@@ -6,4 +6,5 @@ 'use strict'

const slash = require('slash')
const os = require('os')
const path = require('path')
// Replace absolute file paths with <PROJECT_ROOT>
module.exports = {

@@ -82,11 +83,31 @@ print (val, serialize) {

const cwd = process.cwd()
const replaceCwd = value.split(cwd).join('<PROJECT_ROOT>')
const homeDir = os.homedir()
const tempDir = os.tmpdir()
// Remove win32 drive letters, C:\ -> \
const removeWin32Drives = replaceCwd.replace(/[a-zA-Z]:\\/g, '\\')
// Convert win32 backslash's to forward slashes, \ -> /
const useForwardSlashes = slash(removeWin32Drives)
const homeRelativeToTemp = path.relative(tempDir, homeDir)
return useForwardSlashes
const runner = [
// Replace process.cwd with <PROJECT_ROOT>
val => val.split(cwd).join('<PROJECT_ROOT>'),
// Replace home directory with <TEMP_DIR>
val => val.split(tempDir).join('<TEMP_DIR>'),
// Replace home directory with <HOME_DIR>
val => val.split(homeDir).join('<HOME_DIR>'),
// handle HOME_DIR nested inside TEMP_DIR
val => val.split(`<TEMP_DIR>${path.sep + homeRelativeToTemp}`).join('<HOME_DIR>'),
// Remove win32 drive letters, C:\ -> \
val => val.replace(/[a-zA-Z]:\\/g, '\\'),
// Convert win32 backslash's to forward slashes, \ -> /
val => slash(val),
]
let result = value
runner.forEach(current => {
result = current(result)
})
return result
}

@@ -93,0 +114,0 @@

{
"name": "jest-serializer-path",
"version": "0.1.9",
"version": "0.1.10",
"description": "Remove absolute paths from your Jest snapshots",

@@ -47,3 +47,4 @@ "author": "tribou",

"./"
]
],
"resetModules": true
},

@@ -50,0 +51,0 @@ "scripts": {

12

README.md

@@ -12,5 +12,2 @@ # jest-serializer-path

> NOTE: All single backslashes ("\\") will be replaced by a forward slash ("/").
Also, any string that looks like a Windows drive letter ("C:\\") will be replaced by a forward slash ("/").
#### Quick Start

@@ -44,2 +41,11 @@

``/path/to/os-temp/nested/temp`` => ``<TEMP_DIR>/nested/temp``
``/path/to/user-home/nested/home`` => ``<HOME_DIR>/nested/home``
#### Caveats
* All single backslashes (`\`) will be replaced by a forward slash (`/`).
* Any string that looks like a Windows drive letter (`C:\`) will be replaced by a forward slash (`/`).
#### Build

@@ -46,0 +52,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc