Skip to content

Instantly share code, notes, and snippets.

View jukkatupamaki's full-sized avatar

Jukka Tupamäki jukkatupamaki

  • Tampere, Finland
View GitHub Profile
@jukkatupamaki
jukkatupamaki / jest-async-error.md
Last active April 29, 2024 06:25
Asserting async errors with Jest

Asserting async errors with Jest

Question: How to test what kind of error an async function throws and how to apply matchers to the rejected value?

Answer: You need to await the expect call and then inspect the rejects property of the returned Jest object.

async function doSomethingAsync(value?: string) {
  if (!value) throw new MyFancyError('Some error text');
 return value;
@jukkatupamaki
jukkatupamaki / auth0-lock.md
Last active April 29, 2021 19:23
How to make Auth0 Lock and Cross-Origin Authentication work in a single page app e.g. React / Angular / Ember etc.

How to make Auth0 Lock and Cross-Origin Authentication work in a single page app e.g. React / Angular / Ember etc.

3rd party cookies have been disabled in most browsers and it affects apps that use the Auth0 Lock.

Without proper configuration (which is pretty hard to find from Auth0's documentation) the app won't let users log in. This took me a hours of work to figure out how the changes should be implemented.

So what next

To fix the problem you need the following:

@jukkatupamaki
jukkatupamaki / heroku-postgres-dump-to-plain.md
Created July 10, 2020 10:22
How to convert a Heroku Postgres dump to plain text

How to convert a Heroku Postgres dump to plain text

Sometimes you need to search within backups but don't want to restore the dump to a real database.

Backup files produced by Heroku Postgres are compressed. So they need to be converted to plain text.

  1. Download a backup file and name it e.g. input.dump
  2. Convert input.dump to plain text using pg_restore:
@jukkatupamaki
jukkatupamaki / knex-pg-check.md
Last active January 18, 2022 21:43
How to use check constraints with Knex.js and Postgres

How to use check constraints with Knex.js and Postgres

Check constraints are useful for validating INSERT and UPDATE queries. With Knex.js, you have to use raw calls to add or drop constraints.

Add a constraint to check that my_column in my_table is at least 0:

knex.schema.raw(`
  ALTER TABLE
    my_table
 ADD CONSTRAINT
@jukkatupamaki
jukkatupamaki / psql-csv-import-export.md
Last active April 27, 2021 13:04
How to import/export data from/to a CSV file in psql CLI (PostgreSQL)

Open psql and connect to your database.

Export a query to a CSV file

Use the \copy command to export data from a remote or local database to a local CSV file.

\copy (select id, name from products) to 'my-local-file.csv' csv header
@jukkatupamaki
jukkatupamaki / macos-commandline-copy-paste.md
Last active February 20, 2020 11:36
MacOS command-line copy and paste

Commands:

  • pbcopy - Copy to clipboard
  • pbpaste - Paste from clipboard

Example: Pipe filenames to clipboard

$ ls -1 | pbcopy 

These commands are useful if copied text should be 100% the same as the output. Selecting and copying text in terminal always has more whitespace than needed or one or two characters are missing. So this is a perfect solution to avoid those.

@jukkatupamaki
jukkatupamaki / vscode-react-fragment-fix.md
Last active February 19, 2020 10:35
Visual Studio Code won't recognize React Fragment syntax

If <></> triggers a syntax error in Visual Studio Code, try the following:

  1. Switch file's language mode to TypeScript React
  2. Switch file's language mode back to JavaScript React

The error should be gone.

@jukkatupamaki
jukkatupamaki / 20190417131115_test-setup.ts
Last active November 9, 2024 17:31
How to use Knex.js in a TypeScript project
import { Knex } from 'knex'
export async function up(knex: Knex): Promise<any> {
await knex.schema.createTable('test_setup', (table: Knex.TableBuilder) => {
table.integer('foobar');
});
}
export async function down(knex: Knex): Promise<any> {
await knex.schema.dropTable('test_setup');
@jukkatupamaki
jukkatupamaki / package.json
Last active May 16, 2016 06:44
An example package.json with Windows specific changes for simple-react-app project
{
"name": "react-example-app",
"version": "1.0.0",
"description": "Demonstrates usage of React, React Router and Express.",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"ensure-dirs": "mkdirp frontend\\dist && mkdirp frontend\\dist\\scripts && mkdirp frontend\\dist\\styles && mkdirp frontend\\dist\\assets",
"prewatch": "npm run ensure-dirs && npm run copy-assets && npm run copy-index",