Giter VIP home page Giter VIP logo

jest-serializer-path's Introduction

jest-serializer-path

npm version Linux Build Status Windows Build Status Coverage Status Project Status: Active - The project has reached a stable, usable state and is being actively developed. bitHound Code

Remove absolute paths and normalize paths across all platforms in your Jest snapshots.

Quick Start

npm install --save-dev jest-serializer-path

Add this to your package.json Jest config:

"jest": {
  "snapshotSerializers": [
    "jest-serializer-path"
  ]
}

Or include only in individual tests:

const serializer = require("jest-serializer-path");

expect.addSnapshotSerializer(serializer);

All absolute paths will now be converted and saved in snapshots like so:

/path/to/my-proj/lib => <PROJECT_ROOT>/lib

/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

This project bundles the yarn executable and the npm/yarn dependencies offline in the .npm-packages-offline-cache directory for faster dependency installs and better dev/prod parity across including preventing failure if yarn/npm is offline.

# Install
npm run yarn

# Run tests
npm run test

jest-serializer-path's People

Contributors

alan-agius4 avatar chrisblossom avatar jakeginnivan avatar tribou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jest-serializer-path's Issues

Yarn workspace root path normalization

Hi there,

Thanks for this handy module. I've been using it within a shared build tool with great effect. We're looking into moving into a Yarn Workspace setup and I noticed that this serializer does not handle normalization of the yarn workspace root path.

For example, in a non-workspace setup a path that was ~/code/single-project/node_modules/foo/foo.js would correctly normalize to <PROJECT_ROOT>/node_modules/foo/foo.js

However, in a Yarn workspace this may resolve to to ~/code/workspace/node_modules/foo/foo.js (the project in this case lives at ~/code/workspace/packages/single-project), which gets normalized as <HOME_DIR>/code/workspace/node_modules/foo/foo.js. Ideally, this would be relative to the workspace root (something like <WORKSPACE_ROOT>/node_modules/foo/foo.js) so that our tests are not dependent on a specific project location.

I took a very quick pass at this in https://github.com/tribou/jest-serializer-path/compare/master...nason:yarn-workspace-support?expand=1. It seems to be working for me, but wanted to get your thoughts on this before going any further.

configure replacement of `\`

I use the \ in some of my snapshots. i never test on windows so i don't need the character to be replaced. would be nice to be able to turn this functionality off somehow

__dirname used over process.cwd()

Is there a reason __dirname is used to find the project root instead of process.cwd()? This is causing some issues with a tool I'm using and I was wondering if you'd accept a PR that switched to process.cwd()?

const dirname = __dirname.substring(0, __dirname.indexOf(path.normalize('/node_modules/')))
// Support self-testing
|| __dirname

Unexpected replace in the middle of a string

When an object contains a property with value foo/tmp/bar, the snapshot will have foo<TEMP_DIR>/bar, which is unexpected. I was expect that only strings starting with /tmp would get replaced.

Great Idea With Unexpected Result

Firstly, wonderful concept and glad that someone has taken the time to do this. Thank you for that.

Surprisingly though, using this has unexpected results as it modifies the object being tested after the expect has been executed. That's not at all optimal as objects can still be used down the pipe. If this project is going to be opinionated about that (not a bad thing necessarily) then baking some manual mechanism for restoring the object would be preferred.

This code illustrates the problem:

it('should test', () => {

  const result = getSomeObj({ ... }); // { path: '/Users/user/code/myproject/lib' }

  expect(result).toMatchSnapshot();

  // at this point the snapshot written is correct, and result.path is '<PROJECT_ROOT>/myproject/lib'
  // however, the result object has now been mutated, and silently no less

  // the following will now fail as the path has been mutated and does not exist
  fs.writeFileSync(path.join(result.path, 'myfile'), 'myfile data', 'utf-8');

});

I'd wager that this is a common problem. We're exploring using this module as a default for the webpack-contrib org, which means it'd be part of every official webpack loader and supporting development module. Unfortunately in it's current form, mutating the tested object is going block our use since we do quite a bit after a snapshot test for many modules.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.