Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

User defined naming of files #158

Closed
wants to merge 13 commits into from

Conversation

andrewluetgers
Copy link

@andrewluetgers andrewluetgers commented Sep 11, 2018

allows user to provide a uidFn option to define how to name mock files.

function mockName(method, url) {
  return (method + "|" + url).replace(/\//g, "_").replace("?", "__").replace(/&/g, "--") + ".txt";
}

let Replay = require('replay');
Replay.mode = 'record';
Replay.fixtures = myFixturesPath;
Replay.uidFn = req => mockName(req.method, req.url.host+req.url.path);

@andrewluetgers andrewluetgers changed the title User defined naming fo files User defined naming of files Sep 11, 2018
@djanowski
Copy link
Collaborator

Hi @andrewluetgers, thanks for your PR!

I don't think we're going to make this change, for now. However I would like to give you feedback anyway:

  • What's the motivation for this change? I don't see any linked issue or discussion, so it's hard for me to understand what problem you're trying to solve.
  • If we name the files based on some aspects of the request, what happens when we have two files with the same name?
  • The PR includes a bunch of changes which are unrelated to your proposed change, which makes it harder to review.

@andrewluetgers
Copy link
Author

andrewluetgers commented Dec 12, 2018

Sorry for the low quality PR.

The PR includes a bunch of changes which are unrelated to your proposed change

Its clear there are two things in here that I should have submitted separately.

1: Custom file names
motivation - now I can easily find a file based on the request if I want to edit it, also helps in understanding what mocks I have in place. Of course there are any number of naming schemes one could use which includes adding a random value to eliminate the risk duplicates if one chooses.

2: prettified JSON
motivation - for easy diffing and manual editing of JSON responses that are normally sent in a single line.

what happens when we have two files with the same name?

The file gets overwritten but you would have to commit a change and could view the diff and your tests may fail so the issue will rear its head quickly if it is an issue. You would then change the naming scheme and/or your tests.

So our front-end Puppeteer testing process looks like this:

  • Develop test as normal
  • Place server into record mode
  • Run tests
  • Now CI test runner can spin up local server without needing to touch a network connection db dependencies etc.
  • In the future: a change is made requiring a modification to a response
  • by manually updating the mock or re-running the test in record mode the mocks can be replaced and committed with a readable diff

@ericclemmons
Copy link

I'd like this as well (none of the libraries I've found seem to support it!):

  • Our GraphQL API calls pretty much a single host, but tons of different paths (think of how most REST APIs are structured), so I want to be able to easily go from request URL to file.
  • To avoid conflicts, there has to be some sort of hash specific to that request.

A good example of this problem is with OAUTH APIs:

  • The Authorization header changes often (in my case, hourly before it needs to be renewed/created).

  • I'd want to save a fixture as api.example.com/path/to/resource/{username}.{hash}.txt.

  • hash is the 5-character MD5 of the request properties (excluding Authorization since it's not idempotent)

  • username differs for each test suite.

@andrewluetgers
Copy link
Author

andrewluetgers commented Jan 30, 2019

@ericclemmons the way I implemented this you provide a function that returns a file name given the request object so this could be easily handled. However I believe nested folder paths would need more coding to support. In my case I just wanted a basic request path as the file name.

function mockName(method, url) {
    return (method + "|" + url).replace(/\//g, "_").replace("?", "__").replace(/&/g, "--");
}
	
Replay.uidFn = req => mockName(req.method, req.url.host+ req.url.path) + ".txt";

@capaj
Copy link

capaj commented Sep 9, 2021

this should be closed IMHO in favor of #162

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants