Today I Learned

Mock HTTP Requests in Node.js with Undici

import { MockAgent, setGlobalDispatcher } from 'undici';
 
const mockAgent = new MockAgent();
setGlobalDispatcher(mockAgent);
 
const mockPool = mockAgent.get('http://localhost:3000');
 
mockPool
  .intercept({
    path: '/me',
    method: 'GET',
  })
  .reply(200, {
    id: '123',
    name: 'Johnie',
  });
 
fetch('http://localhost:3000/me')
  .then((res) => res.json())
  .then((user) => {
    console.dir(user); // { id: '123', name: 'Johnie' }
  });

Formatting Commit Messages with fmt

You can use the fmt command to format commit messages in Git for better readability. Here's a useful command:

fmt -n -p -w 80
  • -n: Preserves indented lines, making bullet points or code snippets in the message clear.
  • -p: Avoids formatting lines that already begin with indentation, useful for keeping lists and indented code unchanged.
  • -w 80: Wraps text at 80 characters, which is a common convention for commit message width.