Cypress: Stubbing Network Requests with Cy.Route()

I very much enjoy testing web apps by simulating their network requests via code. This allows me to visit websites, login, and replicate functionality, all without a browser, a slightly different sort of testing than many testers are accustomed to. I love to explore what’s happening under the hood when we click elements and submit forms, I like to play with cookies and payloads, I try to find out what bare minimum of data do I need to pass through HTTP requests to recreate a particular user behavior. People often do this kind of testing with Postman but I’ve been accustomed to implementing tests with Ruby and the rest-client gem. Recently though I looked at how Cypress plays with network requests, especially curious about how they take it further with their built-in request stubbing feature using cy.route() because I have never tried stubbing before.

First, some context on HTTP requests:

  • GET requests often simulate visiting (or redirecting to) a web page or retrieving a resource (like an image or another file)
  • POST requests often simulate a form submission, like logins or payments, and as such deals with passing inputted data in order to proceed to the next application state
  • There are other types of HTTP requests but mastering how these two work is enough at the start

And here is an example of how Cypress helps you perform a said GET request:

and for a POST request:

Pretty straightforward and easy to follow. Notice that POST requests have more information in them than GET requests, since we’re passing data – the body field is concerned with user inputs while the headers field is concerned with the user session, among other things. Of course, both requests need a url field, some place to send the request to.

And when we send a request, we receive a response. That response tells us about how a web application behaved after the request – was the user redirected to another page? was the user able to log in? did an expected web element got displayed or hidden? were we sent to an error page, perhaps?

Cypress takes network requests further by introducing routing to testing. Here’s an example:

What the above code says is that we want to use Cypress as a server and we want to wait and listen for a POST request that’s going to the /login URL after a submit button (with an id of #submitButton) is clicked, after which we want to respond with a { success: false } result. This means that the actual response from our application from that url is going to be taken over by a fake response that we designed ourselves. This is what stubbing a network request looks like.

Now why would we want to do this? Some reasons:

  • We want to check how an application behaves for scenarios where a request fails to reach the application server. Do we redirect the user? Or do we show an error popup? Or does the application also work offline? To do this without stubbing, we would need some help from a programmer to shut down the app server at the right time after we perform the scenario.
  • We want to speed up tests by stubbing the response of some requests with less data than the actual responses deliver. We can even have the response data to be empty, if we don’t necessarily need that specific data for a test.
  • We want to see what happens to the application when it receives an incorrect response value from a request.

This is one thing I am loving about Cypress. Out of the box, they allow me to play with network requests alongside testing the user interface, and lets me tinker with it some more.


Notes from Scott James’ “Bony to Brawny: No B.S. Techniques To Stack On Slabs Of Lean Muscle Mass And Get Strong As Hell Regardless Of How Skinny You Are”

I have always been a skinny dude. And I have always thought that I won’t change, that it’s just the way things are for me. But keeping the same diet and sedentary lifestyle for many years has taken its toll in recent years and, although I have not been sickly, my energy levels just did not feel the same as it has been when I was younger. I tire easily now than before, even when I don’t really go out much. Perhaps it’s just age doing its thing, like we are supposed to get tired faster as we get older. But it is a nagging feeling, it does not feel good at all, and is something that I decided I want to find a sustainable way of correcting this year if possible.

Reading Scott James’ ‘Bony to Brawny‘ book (and actually changing diet and exercise habits) is taking multiple steps forward towards that goal.

Some takeaways from the book:

  • The funny thing is I have literally never regretted a workout – it just doesn’t happen. The release of endorphins and the feeling of knowing you’re making progress towards your goals while improving your health, mobility, and well-being is intense.
  • If you’ve been skinny all your life, chances are you haven’t been eating much in terms of calories. When you’re entering a bulking phase, the amount of food may seem overwhelming. Focus on liquid nutrition. Consuming two shakes (depending on how many calories you incorporate) plus two solid meals will without a doubt place you in a the caloric range you need to be hitting to pack on some solid lean muscle mass.
  • There is no food out there that’ll make you gain muscle at a quicker rate than another food. The food itself won’t build muscle – the calories and macro-nutrients it contains will. Try fish, chicken breast, eggs, cottage cheese, almonds, brown rice, milk, and sweet potatoes.
  • Eating fat will not make you fat at all. Fat is essential for regulating hormones. Excess calories make you fat – not any particular macro-nutrient itself (protein, carbohydrates, or fat).
  • For optimal performance in sports and resistance training, as well keeping your appetite in check, consumer at least 30% of your daily calories from protein, with the remaining 70% coming from a breakdown of carbs and fats. If you neglect your protein intake you will not be able to be able to build and retain lean muscle; if all you’re eating is carbohydrates and fat, you’ll be packing on the pounds in the form of stored body fat.
  • In order to build that muscle mass and shred that fat, you need to create the demand for your body to do so. After ongoing demand, your body begins to adapt, and adaption is growth. This demand is solely created inside the gym – i.e. lifting heavy weights! An exercise regime based on compound movements will force your body to adapt – heavy squats, deadlifts, bench presses and overhead presses in the low rep range will create the demand far better than any isolation based workout regime based around light dumbbells or machines.
  • Without a caloric surplus, you won’t pack on any muscle mass. At the same time, if lifting is neglected and dieting is honed in on to the utmost detail, you’re still on the road to fat gain as your body is not being forced to adapt and grow (using this caloric surplus to repair your body and build muscle)
  • As a newcomer, you will find the demand is quite easy as your body has never experienced such stress before. However, as time goes on, you must apply progressive overload in order to keep the demand going. Increasing the weight you’re lifting, increasing the time your muscles are under tension, adding in a few additional reps – these techniques are designed to place an increased load on your muscles each and every workout. In essence, the demand must become greater and greater with each workout. When you’re demanding the same thing from your muscles week after week (same weight, same reps, same time under tension), the demand to go above and beyond has vanished, and so will your progression in terms of size and strength). The guys in the gym repping the same weight week in week out won’t get far. The guy adding that extra 2.5 lb plate to the barbell each workout will. It adds up over time.
  • In order to get big you need to focus your energy on your heavy compound movements. Don’t major in minors, aka: spending your time floating around from isolation exercise to isolation exercise. This is a sure-fire way for a skinny guy to see sub-par size gains. Movements or exercise that do not give the muscles the required resistance, but are the kind that involve a great number of repetitions, never break down any tissue to speak of. These movements involve a forcing process that cause the blood to swell up the muscle, and simply pump them up. The insane, full, pumped feeling you get in your arms and chest when training is merely the increased blood flow tot the particular muscle group being worked. The pump does not equate to muscle growth.
  • If you’re new to low rep training, it’s worth noting that you will not get the immense pumps you may be used to from performing high repetition training. As a general rule, most things that are satisfying in the short term aren’t beneficial at all in the long run.
  • Once you are comfortably hitting the top end of the rep range, I recommend increasing the weight. You should be able to perform a minimum of four reps with the new weight. If you’re unable to perform four reps with correct form and range of motion, the weight is too heavy. Add 5 lbs/10 lbs to each dumbbell/barbell exercise once you are comfortably hitting the upper range of the prescribed repetitions.
  • Each and every repetition should be controlled and timed. 2 seconds down – 1 second hold – 2 seconds up. If you’re performing your repetitions any faster than this, your muscles won’t be under tension for particularly long. To ensure you get the maximum bang for the buck in terms of size and strength, focus on the 2:1:2 tempo. Rest for 2-3 minutes between sets of heavy compound exercises. Rest for 1-2 minutes maximum between sets of isolation exercises.
  • We don’t build muscle in the gym by banging and clanging around with heavy weights. We’re actually destroying our muscle fibers – which is telling our bodies that our current physique is insufficient. Resting after a workout and the nutrition components of your bulking phase are where the growth comes from. Lifting four times per week with adequate sleep and recovery will net you far greater results than training six times per week with minimal sleep and no downtime.
  • Sleep plays a key role in the following bodily processes: regulation of glucose in the body, promoting good blood pressure, the perfect way to recover and repair damaged muscles, promoting hormonal functions and processes, promotion cognitive functions and processes. Your sleep should be within the 7-9 hour  bracket per night, keeping in mind this is high quality, uninterrupted sleep.
  • If you’re not making any size gains, this will be the result of one of two things:
    • You’re not lifting heavy enough. In order to grow, we need to provide ongoing progressive overload during our workouts. Ensure you’re working in the prescribed rep ranges for your heavy compound exercises and give it your all. If you’re calling it quits on your set when it starts to feel uncomfortable, you’re not going to get too far.
    • If you’re lifting regime is solid, then the issue is likely to be related to your diet, or most specifically the lack of calories you’re consuming. A failure to build muscle mass is most commonly due to an insufficient number of calories coming in. Re-calculate your total daily energy expenditure and apply a more aggressive caloric surplus if necessary.
  • Pick a workout routine, follow it, eat well, and get adequate rest. Follow this consistently for six months. During the course of this time, track your weight, your body fat, your arm size, your chest size, how you feel after each workout, and from there you can reassess and make the necessary changes.
  • When your progress slows down or completely stops, whether this be in strength gains, shedding that unwanted body fat or gaining muscle mass, it’s time to take a step back and look at what you’re actually doing. Doing the same thing over and over again while expecting a different outcome is the definition of insanity.
  • Progress is the ultimate motivator. It’s the positive feedback loop of putting in effort, seeing results and therefore continuing to grind away day after day because you know what you’re doing is making a difference.

On 100% Coverage

Yes, we need to write tests because it is something that we think will help us in the long term, even though it may be more work for us in the short run. If written with care and with the end in mind, tests serve as living documentation, living because they change as much as the application code changes, and they help us refer back to what some feature does and doesn’t, in as much detail as we want. Tests let us know which areas of the application matters to us, and every time they run they remind us of where our bearings currently are.

Tests may be user journeys in the user interface, a simulation of requests and response through the app’s API, or small tests within the application’s discrete units, most likely a combination of all these types of tests, perhaps more. What matters is that we find some value in whatever test we write, and that value merits its cost of writing and maintenance. What’s important is asking ourselves whether the test is actually significant enough to add to the test suite.

It is valuable to build a good enough suite of tests. It makes sense to add more tests as we find more key scenarios to exercise. It also makes sense to remove tests that were necessary in the past but aren’t anymore. However, I don’t think it is particularly helpful to advocate for 100% test coverage, because that brings the focus on a numbers game, similar to how measuring likes or stars isn’t really the point. I believe it is better when we discuss among ourselves, in the context we’re in, which tests are relevant and which are just diving into minutiae. If our test suite helps us deploy our apps with confidence, if our tests allows us to be effective in the performance of our testing, if we are continuously able to serve our customers as best as we can, then the numbers really doesn’t amount to much.

One Room, Working Together

For the past few weeks a number of programmers and myself have been tasked to build an initial prototype for a system rewrite project, handed to us by management. The merit of such project is a matter of discussion for another day; for now it is enough to say that the team has been given a difficult challenge, but at the same time excited about the lessons we knew we will gain from such an adventure.

There’s been several takeaways already in terms of technology know-how – dockerized applications, front-end development with Vue, repositories as application vendor dependency, microservices – just several of the things we’ve never done before.

But the great takeaway so far is the joy of literally working together, inside a room away from distractions, the team working on one task at a time, focused, taking turns writing application or test code on a single machine, continuously discussing options and experimenting until a problem is solved or until it is time to take a break. We instantly become aligned at what we want to achieve, we immediately help teammates move forward, we learn from each other’s skills and mistakes, we have fun. It’s a wonder why we’ve never done much of this before. Perhaps it’s because of working in cubicles. Perhaps it’s because there’s nearly not enough available rooms for such software development practice. Perhaps it’s because we’ve never heard anything about mob programming until recently.

I’m sure it won’t be everyday since we have remote work schedules, but I imagine the team spending more days working together like this from here on.

Doing Things Right, And Doing The Right Things

In testing software, automation is a tool that helps us re-run whatever repeatable checks we have on an application under test. We automate because we never have enough time to re-test everything by hand, and exploring the unknown parts of the apps we test is a far better use of our testing skills than following scripts. To automate is doing one thing right, within context, if it provides us the feedback we need. And that feedback we think we need from automation depends on what suites of tests are best repeated again and again, as well as what sort of tests costs more than the value they give.

There’s also tons of tools that helps us build good quality software. Even though we build more complex applications now than before, we have frameworks, libraries, intelligent IDEs, and other tools to help us spin up apps on a whim now too, ready to be modified as we see fit. Choosing the proper tools for the job is doing another thing right. But before we write any code, we need to be sure about the actual problem we are solving for our customer.

Yes, we need to do things right, from the get go if possible. They help us progress from one point to another faster than otherwise. However, I think it’s more important that we continuously take the necessary time to review whether we are doing the right things too, more important to actually get feedback and solve problems than merely adding tests and features.

Takeaways from Dale Carnegie’s “How To Win Friends and Influence People”

Dale Carnegie’s “How to Win Friends and Influence People” is a classic, and I did not understand what that meant until now, after reading the book. It has survived the test of time, and the lessons in it still applies to all of us today. It really is a treasure trove of actionable advice about forging friendships and leading people, and I’ve come to see why some of my way of doing things have worked for me all this time. Better, I’ve found places where I could use more practice and improve.

Here are some favourite lines from the book:

  • Let’s realise that criticisms are like homing pigeons. They always return home. Let’s realise that the person we are going to correct and condemn will probably justify himself or herself, and condemn us in return.
  • There is only one way under high heaven to get anybody to do anything. And that us by making the other person want to do it. The only way I can get you to do anything is giving you what you want.
  • We nourish the bodies of our children and friends and employees, but how seldom do we nourish their self-esteem? We provide them with roast beef and tomatoes to build energy, but we neglect to give them kind words of appreciation that would sing in their memories for years like the music of the morning stars.
  • If there is any secret to success, it likes in the ability to get the other person’s point of view and see things from that person’s angle as well as your own.
  • Keep in your mind on the great and splendid things you would like to do, and then, as the days go gliding away, you will find yourself unconsciously seizing upon the opportunities that are required for the fulfilment of your desire. Picture in your mind the able, earnest, useful person you desire to be, and the thought you hold is hourly transforming you into that particular individual.
  • He had wanted merely a friendly, sympathetic listener to whom he could unburden himself. That’s what we all want when we are in trouble. That is frequently all the irritated customer wants, and the dissatisfied employee or the hurt friend.
  • So if you aspire to be a good conversationalist, be an attentive listener. To be interesting, be interested. Ask questions that other persons will enjoy answering. Encourage them to talk about themselves and their accomplishments. Remember that the people you are talking to are a hundred times more interested in themselves and their wants and problems than they are in you and your problems.
  • Anyone who takes the time to disagree with you is interested in the same things you are. Think of them as people who really want to help you, and you may turn your opponents to friends.
  • You can tell people they are wrong by a look or an intonation or a gesture just as eloquently as you can in words – and if you tell them they are wrong, do you make them want to agree with you? Never! For you have struck a direct blow at their intelligence, judgment, pride, and self-respect. That will make them want to strike back. But it will never make them want to change their minds. You may then hurl at them all the logic of a Plato or an Immanuel Kant, but you will not alter their opinions, for you have hurt their feelings.
  • If you are going to prove anything, don’t let anybody know it. Do it so subtly, so adroitly, that no one will feel that you are doing it. You cannot teach a man anything; you can only help him to find it within himself.
  • I am convinced now that nothing good is accomplished and a lot of damage can be done if you tell a person straight out that he or she is wrong. You only succeed in stripping that person of self-dignity and making yourself an unwelcome part of any discussion.
  • If a man’s heart is rankling with discord and ill feeling toward you, you can’t win him to your way of thinking with all the logic in Christendom. Scolding parents and domineering bosses and husbands and nagging wives ought to realize that people don’t want to change their minds. They can’t be forced or driven to agree with you or me. But they may be possibly led to, if we are gentle and friendly, ever so gentle and ever so friendly.
  • No one likes to feel that he or she is being sold something or told to do a thing. We much prefer to feel that we are buying of our own accord or acting on our own ideas. We like to be consulted about our wishes, our wants, our thoughts.
  • What do you think he found to be the most motivating factor – the one facet of the jobs that was most stimulating? Money? Good working conditions? Fringe benefits? No – not any of those. The one major factor that motivated people was the work itself. If the work was exciting and interesting, the worker looked forward to doing it and was motivated to do a good job.
  • This is what every successful person loves: the game. The chance for self-expression. The chance to prove his or her worth, to excel, to win. That is what makes foot-races, and hog-calling, and pie-eating contests. The desire to excel. The desire for a feeling of importance.
  • He always gave people the opportunity to do things themselves; he never told his assistants to do things; he let them do them, let them learn from their mistakes. A technique like that makes it easy for a person to correct errors. A technique like that saves a person’s pride and gives him or her a feeling of importance. It encourages cooperation instead of rebellion.
  • If you want to improve a person in a certain aspect, act as though that particular trait were already one of his or her outstanding characteristics. Assume a virtue, if you have it not. Assume and state openly that other people have the virtue you want them to develop. Give them a fine reputation to live up to, and they will make prodigious efforts rather than see you disillusioned.
  • Tell your child, your spouse, or your employee that he or she is stupid or dumb at a certain thing, has no gift for it, and is doing it all wrong, and you have destroyed almost every incentive to try to improve. But use the opposite technique – be liberal with your encouragement, make the thing seem easy to do, let the other person know that you have faith in his ability to do it, that he has an undeveloped flair for it – and he will practice until the dawn comes in the window in order to excel.
  • It isn’t what you have or who you are or where you are or what you are doing that makes you happy or unhappy. It is what you think about it. There is nothing either good or bad, but thinking makes it so.

We Find Out What Works For Us As We Go, Together

A software development team is composed of people, a combination of of programmers, testers, designers, and product owners. One team is visibly different from another because they have different people working in them, although both teams work in the same organisation and share the same mission. One group may be dealing with passionate but inexperienced new hires, another may be led by an introverted and soft-spoken senior, another team could be a lot more experienced with automating things, one team is probably better with team communication than others. Each team has a life of its own, always growing up, always trying to find out what specific problems itself has and what values it can contribute to the organisation, evolving day after day.

We understand that everyone is similar yet different from each other. Why is it then that we often insist in managing them with a one-size-fits-all process? Or am I missing something? I understand that we want teams to get better at what they do, we want them to release software faster, with better quality, but do we really think that one particular process works for everyone? Instead of that, shouldn’t we just immerse ourselves in a team, one after another, observe, ask where they are and where they want to go, talk to them, share experiences, help them solve problems, care for them, let them grow into a family with us? It seems to me that every time I get to join a team I think about the people in it first, no particular fixed process in mind, and we find out what works for us as we go, together.