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.8 to 0.1.9

150

lib/index_test.js

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

const normalizePaths = Serializer.normalizePaths
describe('serializer', () => {

@@ -19,3 +21,3 @@

const sut = `${process.cwd()}/src/somewhere`
const sut = path.resolve(process.cwd(), 'src/somewhere')
expect(sut).toMatchSnapshot()

@@ -25,6 +27,20 @@

it('replaces process.cwd with <PROJECT_ROOT> when inside string', () => {
const sut = `long string ${path.resolve(process.cwd(), 'src/somewhere')} path`
expect(sut).toMatchSnapshot()
})
it('handles path inside string without process.cwd', () => {
const sut = `long string ${path.resolve('/root/src/somewhere')} path`
expect(sut).toMatchSnapshot()
})
it('replaces process.cwd with <PROJECT_ROOT> in Object properties', () => {
const sut = {
myPath: `${process.cwd()}/src`,
myPath: path.resolve(process.cwd(), 'src'),
}

@@ -37,3 +53,3 @@ expect(sut).toMatchSnapshot()

const sut = [`${process.cwd()}/src`]
const sut = [path.resolve(process.cwd(), 'src')]
expect(sut).toMatchSnapshot()

@@ -45,3 +61,3 @@

const sut = new Error(`some error in ${process.cwd()}/a/path`)
const sut = new Error(`some error in ${path.resolve(process.cwd(), 'a/path')}`)
expect(sut).toMatchSnapshot()

@@ -53,3 +69,3 @@

const sut = `${process.cwd()}/path/with/trailing/slash/`
const sut = path.resolve(process.cwd(), 'path/with/trailing/slash/') + path.sep
expect(sut).toMatchSnapshot()

@@ -66,3 +82,3 @@

const sut = `${process.cwd()}/src/somewhere`
const sut = path.resolve(process.cwd(), 'src/somewhere')
expect(sut).toMatchSnapshot()

@@ -77,5 +93,5 @@

const sut = {
PATH: `${process.cwd()}/path:${process.cwd()}/another/path`,
script: `const myPath = ${process.cwd()}/path;
const mySecondPath = ${process.cwd()}/another/path;`,
PATH: `${path.resolve(process.cwd(), 'path')}:${path.resolve(process.cwd(), 'another/path')}`,
script: `const myPath = ${path.resolve(process.cwd(), 'path')};
const mySecondPath = ${path.resolve(process.cwd(), 'another/path')};`,
}

@@ -86,2 +102,17 @@ expect(sut).toMatchSnapshot()

it('handles an assortment of nested objects', () => {
const sut = {
nested: {
myPath: path.resolve(process.cwd(), 'src'),
arr: [
path.resolve(process.cwd(), 'arr'),
{ arrPath: path.resolve(process.cwd(), 'arrPath') },
],
},
}
expect(sut).toMatchSnapshot()
})
})

@@ -94,3 +125,3 @@

const val = `${process.cwd()}/a/path`
const val = path.resolve(process.cwd(), 'a/path')
const result = Serializer.test(val)

@@ -101,15 +132,7 @@ expect(result).toEqual(true)

it('returns false when val is a string and does not contain process.cwd', () => {
const val = '/a/path'
const result = Serializer.test(val)
expect(result).toEqual(false)
})
it('returns true when val is an object with a property that contains process.cwd', () => {
const val = {
property: `${process.cwd()}/a/path`,
property2: '/no/dirname',
property: path.resolve(process.cwd(), 'a/path'),
property2: path.resolve('/no/dirname'),
}

@@ -121,16 +144,5 @@ const result = Serializer.test(val)

it('returns false when val is an object with a property that does not contain process.cwd', () => {
const val = {
property: '/a/path/',
property2: '/no/dirname',
}
const result = Serializer.test(val)
expect(result).toEqual(false)
})
it('returns true when val is an array with a property that contains process.cwd', () => {
const val = [`${process.cwd()}/a/path`, '/no/dirname']
const val = [path.resolve(process.cwd(), 'a/path'), path.resolve('/no/dirname')]
const result = Serializer.test(val)

@@ -141,13 +153,5 @@ expect(result).toEqual(true)

it('returns false when val is an array with a property that does not contain process.cwd', () => {
const val = ['/a/path/', '/no/dirname']
const result = Serializer.test(val)
expect(result).toEqual(false)
})
it('returns true when val is an object with a property that contains process.cwd', () => {
const val = new Error(`some error in ${process.cwd()}/a/path`)
const val = new Error(`some error in ${path.resolve(process.cwd(), 'a/path')}`)
val.code = 'HAS_CODE'

@@ -159,6 +163,5 @@ const result = Serializer.test(val)

it('returns false when val is an object with a property that contains process.cwd', () => {
it('returns false when value does not change', () => {
const val = new Error(`some error in /a/path/`)
val.code = 'HAS_CODE'
const val = 'some random string'
const result = Serializer.test(val)

@@ -177,3 +180,3 @@ expect(result).toEqual(false)

const ser = jest.fn()
const val = `${process.cwd()}/a/path`
const val = path.resolve(process.cwd(), 'a/path')
const expected = '<PROJECT_ROOT>/a/path'

@@ -189,4 +192,4 @@ Serializer.print(val, ser)

const val = {
property: `${process.cwd()}/a/path`,
property2: '/no/dirname',
property: path.resolve(process.cwd(), 'a/path'),
property2: path.resolve('/no/dirname'),
}

@@ -205,3 +208,3 @@ const expected = {

const ser = jest.fn()
const val = [`${process.cwd()}/a/path`, '/no/dirname']
const val = [path.resolve(process.cwd(), 'a/path'), path.resolve('/no/dirname')]
const expected = ['<PROJECT_ROOT>/a/path', '/no/dirname']

@@ -216,3 +219,3 @@ Serializer.print(val, ser)

const ser = jest.fn()
const val = new Error(`some error in ${process.cwd()}/a/path`)
const val = new Error(`some error in ${path.resolve(process.cwd(), 'a/path')}`)
const expected = 'some error in <PROJECT_ROOT>/a/path'

@@ -225,1 +228,50 @@ Serializer.print(val, ser)

})
describe('normalizePaths', () => {
it('removes windows drive from path', () => {
const value = 'C:\\no\\dirname'
const normalized = normalizePaths(value)
expect(normalized).toEqual('/no/dirname')
})
it('removes windows drive from path inside string', () => {
const value = 'long C:\\no\\dirname string'
const normalized = normalizePaths(value)
expect(normalized).toEqual('long /no/dirname string')
})
it('removes windows drive from path inside string', () => {
const value = 'long C:\\no\\dirname string'
const normalized = normalizePaths(value)
expect(normalized).toEqual('long /no/dirname string')
})
it('removes multiple windows drives', () => {
const value = 'C:\\no\\dirname;C:\\other\\dirname'
const normalized = normalizePaths(value)
expect(normalized).toEqual('/no/dirname;/other/dirname')
})
it('handles non-strings', () => {
const value = 1
const normalized = normalizePaths(value)
expect(normalized).toEqual(1)
})
})

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

const slash = require('slash')
// Replace absolute file paths with <PROJECT_ROOT>

@@ -10,25 +12,19 @@ module.exports = {

const cwd = process.cwd()
if (val instanceof Error) {
if (isPath(val)) {
val.message = normalizePaths(val.message)
val = val.split(cwd).join('<PROJECT_ROOT>')
}
else if (val instanceof Error) {
else if (typeof val === 'object') {
val.message = val.message.split(cwd).join('<PROJECT_ROOT>')
}
else {
Object.keys(val).forEach(key => {
if (isPath(val[key])) {
val[key] = normalizePaths(val[key])
val[key] = val[key].split(cwd).join('<PROJECT_ROOT>')
})
}
}
else {
})
val = normalizePaths(val)

@@ -44,3 +40,3 @@ }

if (val instanceof Error && isPath(val.message)) {
if (val instanceof Error && shouldUpdate(val.message)) {

@@ -54,3 +50,3 @@ has = true

if (isPath(val[key])) {
if (shouldUpdate(val[key])) {

@@ -64,3 +60,3 @@ has = true

}
else if (isPath(val)) {
else if (shouldUpdate(val)) {

@@ -74,11 +70,34 @@ has = true

},
normalizePaths,
}
function isPath (value) {
/**
* Normalize paths across platforms.
* Filters must be ran on all platforms to guard against false positives
*/
function normalizePaths (value) {
if (typeof value !== 'string') {
return value
}
const cwd = process.cwd()
const replaceCwd = value.split(cwd).join('<PROJECT_ROOT>')
return typeof value === 'string'
&& value.indexOf(cwd) !== -1
// Remove win32 drive letters, C:\ -> \
const removeWin32Drives = replaceCwd.replace(/[a-zA-Z]:\\/g, '\\')
// Convert win32 backslash's to forward slashes, \ -> /
const useForwardSlashes = slash(removeWin32Drives)
return useForwardSlashes
}
function shouldUpdate (value) {
// return true if value is different from normalized value
return normalizePaths(value) !== value
}
{
"name": "jest-serializer-path",
"version": "0.1.8",
"version": "0.1.9",
"description": "Remove absolute paths from your Jest snapshots",

@@ -37,3 +37,5 @@ "author": "tribou",

},
"dependencies": {},
"dependencies": {
"slash": "^2.0.0"
},
"jest": {

@@ -40,0 +42,0 @@ "collectCoverage": true,

@@ -10,4 +10,7 @@ # jest-serializer-path

Remove absolute paths from your Jest snapshots.
Remove absolute paths and normalize paths across all platforms in your Jest snapshots.
> 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

@@ -14,0 +17,0 @@

Sorry, the diff of this file is not supported yet

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