1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-15 21:18:02 +00:00

Compare commits

..

6 Commits

Author SHA1 Message Date
Irbaz Ahmed
3f8994ee62 Merge 0044cb1381 into 25b5677260 2024-09-05 10:42:23 -03:00
jbranchaud
25b5677260 Add Open Dashboard For Specific Add-On as Heroku TIL 2024-08-23 13:29:47 -05:00
jbranchaud
df3492d4ef Add Configure Tests To Run In Random Order as an RSpec TIL 2024-08-22 11:19:50 -05:00
jbranchaud
bd49b31bb0 Add Multi-Line Comments as a Ruby TIL 2024-08-22 11:12:28 -05:00
jbranchaud
4ff1a381d1 Add Avoid Accidentally Disabling Pry as an rspec TIL 2024-08-14 10:09:07 -05:00
jbranchaud
0bfeb0e236 Add Avoid Conflicting Files as a Next.js TIL 2024-07-16 10:43:03 -05:00
6 changed files with 170 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1434 TILs and counting..._
_1439 TILs and counting..._
---
@@ -392,6 +392,7 @@ _1434 TILs and counting..._
- [Deploy A Review App To A Different Stack](heroku/deploy-a-review-app-to-a-different-stack.md)
- [Diagnose Problems In A Heroku Postgres Database](heroku/diagnose-problems-in-a-heroku-postgres-database.md)
- [Open Dashboard For Specific Add-On](heroku/open-dashboard-for-specific-add-on.md)
- [Run SQL Against Remote Postgres Database](heroku/run-sql-against-remote-postgres-database.md)
- [Set And Show Heroku Env Variables](heroku/set-and-show-heroku-env-variables.md)
- [SSH Into Heroku Server Hosting App](heroku/ssh-into-heroku-server-hosting-app.md)
@@ -634,6 +635,7 @@ _1434 TILs and counting..._
### Next.js
- [Avoid Conflicting Files](nextjs/avoid-conflicting-files.md)
- [Create Files And Directories For Dynamic Routes](nextjs/create-files-and-directories-for-dynamic-routes.md)
- [Define URL Redirects In The Next Config](nextjs/define-url-redirects-in-the-next-config.md)
- [Fetch Does Not Work In API Serverless Function](nextjs/fetch-does-not-work-in-api-serverless-function.md)
@@ -1098,7 +1100,9 @@ _1434 TILs and counting..._
### RSpec
- [Avoid Accidentally Disabling Pry](rspec/avoid-accidentally-disabling-pry.md)
- [Check Specific Arguments To Received Method](rspec/check-specific-arguments-to-received-method.md)
- [Configure Tests To Run In Random Order](rspec/configure-tests-to-run-in-random-order.md)
- [Find Minimal Set Of Tests Causing A Flicker](rspec/find-minimal-set-of-tests-causing-a-flicker.md)
- [Format Test Results As A JSON File](rspec/format-test-results-as-a-json-file.md)
- [Run Tests With Documentation Formatting](rspec/run-tests-with-documentation-formatting.md)
@@ -1178,6 +1182,7 @@ _1434 TILs and counting..._
- [Map With Index Over An Array](ruby/map-with-index-over-an-array.md)
- [Mock Method Chain Calls With RSpec](ruby/mock-method-chain-calls-with-rspec.md)
- [Mocking Requests With Partial URIs Using Regex](ruby/mocking-requests-with-partial-uris-using-regex.md)
- [Multi-Line Comments](ruby/multi-line-comments.md)
- [Named Regex Captures Are Assigned To Variables](ruby/named-regex-captures-are-assigned-to-variables.md)
- [Navigate Back In The Browser With Capybara](ruby/navigate-back-in-the-browser-with-capybara.md)
- [Next And Previous Floats](ruby/next-and-previous-floats.md)

View File

@@ -0,0 +1,17 @@
# Open Dashboard For Specific Add-On
The number of times I've needed to check the papertrail logs for my
Heroku-hosted Rails app is a lot. I open a browser tab, go through several
layers of navigation to get to my app's dashboard, and then click the
papertrail link under _Add-ons_.
There is a much quicker way using the Heroku CLI.
```bash
$ heroku addons:open papertrail -a my-app-name
Opening https://addons-sso.heroku.com/apps/abc123/addons/efg456...
```
It sends you to an add-ons SSO link in the browser which authenticates you and
drops you into the dashboard for that specific add-on. You just need to specify
the add-on name and the app name.

View File

@@ -0,0 +1,36 @@
# Avoid Conflicting Files
When Next.js is bundling and building your project, it will get completely
tripped up by any instance of conflicting project files. What I mean by
conflicting project files are two JavaScript or TypeScript (or flavors of JSX
files) that would resolve to the same thing.
Here is one example where the extensions differ:
```
src/pages/welcome.tsx
src/pages/welcome.jsx
```
Here is another example where the paths differ but the bundled result would
conflict:
```
src/pages/welcome.tsx
src/pages/welcome/index.tsx
```
If you have any instances of these conflicting files, you'll be presented with
a beguiling and cryptic error message when trying to run the dev server.
```
TypeError [ERR_INVALID_ARG_TYPE]: The "to" argument must be of type string. Received undefined
at new NodeError (node:internal/errors:405:5)
at validateString (node:internal/validators:162:11)
at Object.relative (node:path:1191:5)
at Watchpack.<anonymous> (/my_app/node_modules/.pnpm/next@14.2.5_@babel+core@7.24.9_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/server/lib/router-utils/setup-dev-bundler.js:381:55) {
code: 'ERR_INVALID_ARG_TYPE'
}
```
One of those files needs to go. Remove one of them and you'll be good to go.

View File

@@ -0,0 +1,32 @@
# Avoid Accidentally Disabling Pry
I was recently working on a test that needed to mock an environment variable in
order to observe the behavior under test. I initially ended up with the
following lines in a `before` block.
```ruby
before do
allow(ENV).to receive(:[]).and_return("")
allow(ENV).to receive(:[]).with("API_SERVER_URL").and_return("localhost")
end
```
The idea was to create a "clean" `ENV` for the test and then layer in any
relevant environment variables from there.
This is a misguided approach for a couple reasons. One particular and
hard-to-debug issue is that this mocking of `ENV` accidentally disabled Pry
(i.e. `binding.pry`). So once I was having issues with my test, I couldn't even
inspect the code at runtime. It would skip right past any `binding.pry`
statement I added.
What happened is that [Pry (specifically `pry-rails`) looks for
`ENV['DISABLE_PRY_RAILS']`](https://github.com/pry/pry-rails/blob/d8d0c6d87a5b8a3e570e0c80910fb80068f3553c/lib/pry-rails.rb#L6)
and if that value is _truthy_ then it won't require the various `pry-rails`
modules.
The first `allow` has `ENV` responding with `""` which is truthy and will
result in Pry being disabled.
A more appropriate default would be `nil`. Also consider that there are many
ways to access `ENV`, such as `#fetch`.

View File

@@ -0,0 +1,33 @@
# Configure Tests To Run In Random Order
By default, an RSpec test suite is going to run in a predictable, sequential
order, every time.
When testing the parts of a complex Rails app that have all kinds of test data
that needs to be set up, I prefer to have my tests always run in a random
(repeatable with a seed) order. This way I'm more likely to catch sooner,
rather than later, bugs that are hidden by passing tests due to test data setup
that happens to work in a specific order.
RSpec can be configured to run tests in a random, seedable order in the
`spec_helper.rb` file.
```ruby
RSpec.configure do |config|
config.order = :random
end
```
Whenever you run your test suite, the first thing you'll see is a message like
this:
```
Randomized with seed 7011
```
That seed number can be used to re-run the suite in a repeatable order when you
need to do so to track down an order-dependent failing test.
```bash
$ be rspec --seed 7011
```

View File

@@ -0,0 +1,46 @@
# Multi-Line Comments
Ruby has an obscure syntax for creating multi-line comments.
In many languages, there is a multi-line comment syntax that looks something
like this:
```javascript
/*
* multi-line comment in javascript
*/
```
This gets used often in those languages.
In Ruby, the multi-line comment syntax is not something we see very often. It
is a departure from the single-line comment syntax and it also requires no
indentation.
```ruby
RSpec.configure do |config|
config.order = :random
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true
# ...
=end
end
```
Using the `=begin` and `=end` syntax (no indentation), we make everything
inbetween into a comment.
Though we don't see this too often, I did pull this example directly from the
`spec_helper.rb` file that RSpec generates.
[source](https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html)