Skip to content

Commit

Permalink
adding jest.setup.ts env.test setup to armory project as well
Browse files Browse the repository at this point in the history
  • Loading branch information
mattschoch committed Mar 4, 2024
1 parent 1948e09 commit ed4753f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
11 changes: 8 additions & 3 deletions apps/armory/.env.test.default
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# IMPORTANT: The variables defined here will override other variables.
# See `./apps/armory/jest.setup.ts`.

NODE_ENV=test

PORT=3005

ARMORY_DATABASE_URL="postgresql://postgres:postgres@localhost:5432/armory_test?schema=public"

REDIS_HOST=localhost
REDIS_PORT=6379

PRICE_FEED_PRIVATE_KEY="0xc7a1b8ba040a238e36058fc5693f801d129aca9f10ed30d0133878f1b9147c01"
HISTORICAL_TRANSFER_FEED_PRIVATE_KEY="0xf5c8f17cc09215c5038f6b8d5e557c0d98d341236307fe831efdcdd7faeef134"
26 changes: 14 additions & 12 deletions apps/armory/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ import fs from 'fs'
import nock from 'nock'

const testEnvFile = `${__dirname}/.env.test`
const envFile = `${__dirname}/.env`

// Ensure a test environment variable file exists because of the override config
// loading mechanics below.
if (!fs.existsSync(testEnvFile)) {
throw new Error('No .env.test file found. Please create one by running "make armory/copy-default-env".')
throw new Error('No .env.test file found. Please create one by running "make policy-engine/copy-default-env".')
}

// By default, dotenv always loads .env and then you can override with .env.test
// But this is confusing, because then you have to look in multiple files to know which envs are loaded
// So we will clear all envs and then load .env.test
// NOTE: This will also override any CLI-declared envs (e.g. `MY_ENV=test jest`)
for (const prop in process.env) {
if (Object.prototype.hasOwnProperty.call(process.env, prop)) {
delete process.env[prop]
}
}

// We don't want to have two dotenv files that are exactly the same, so we
// override the default with .env.test.
//
// If a .env.test file is not found, the DATABASE_URL will fallback to the
// default. Consequently, you'll lose your development database during the
// integration tests teardown. Hence, the check above.
dotenv.config({ path: envFile })
dotenv.config({ path: testEnvFile, override: true })

// Disable outgoing HTTP requests to avoid flaky tests.
nock.disableNetConnect()

// Enable local outgoing HTTP request to allow E2E tests with supertestwith
// supertest to work.
nock.enableNetConnect(/127.0.0.1|localhost/)
// Enable outgoing HTTP requests to 127.0.0.1 to allow E2E tests with
// supertestwith supertest to work.
nock.enableNetConnect('127.0.0.1')
13 changes: 4 additions & 9 deletions apps/policy-engine/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@ if (!fs.existsSync(testEnvFile)) {
throw new Error('No .env.test file found. Please create one by running "make policy-engine/copy-default-env".')
}

// We don't want to have two dotenv files that are exactly the same, so we
// override the default with .env.test.
//
// If a .env.test file is not found, the DATABASE_URL will fallback to the
// default. Consequently, you'll lose your development database during the
// integration tests teardown. Hence, the check above.

// We also don't even want any .env values; just kill them during tests.
// Clear process.env
// By default, dotenv always loads .env and then you can override with .env.test
// But this is confusing, because then you have to look in multiple files to know which envs are loaded
// So we will clear all envs and then load .env.test
// NOTE: This will also override any CLI-declared envs (e.g. `MY_ENV=test jest`)
for (const prop in process.env) {
if (Object.prototype.hasOwnProperty.call(process.env, prop)) {
delete process.env[prop]
Expand Down

0 comments on commit ed4753f

Please sign in to comment.