Skip to content

Commit

Permalink
feat: add test env support
Browse files Browse the repository at this point in the history
  • Loading branch information
test123456789012345 committed Mar 29, 2020
1 parent 7de2425 commit f271036
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ const ajv = new Ajv({ useDefaults: true })

export default {
config: () => {
const envPath = findUp.sync('.env.json')
const envPath = findUp.sync(process.env.NODE_ENV === 'test' ? '.test.env.json' : '.env.json')
const schemaPath = findUp.sync('.env.schema.json')
const testSchemaPath = process.env.NODE_ENV === 'test' ? findUp.sync('.test.env.schema.json') : undefined

const properties = {
...schemaPath ? require(schemaPath) : {},
...testSchemaPath ? require(testSchemaPath) : {},
}

const properties = schemaPath !== undefined ? require(schemaPath) : {}
const env = {
...envPath !== undefined ? require(envPath) : {},
...properties
Expand Down
42 changes: 42 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import outputFiles from 'output-files'
import { outputFile } from 'fs-extra'
import { omit } from '@dword-design/functions'

let oldEnv

export default {
beforeEach: () => process.env = { ...oldEnv, NODE_ENV: 'development' },
before: () => oldEnv = process.env,
after: () => process.env = oldEnv,
'existing variable': () => withLocalTmpDir(async () => {
process.env.FOO = 'bar'
await outputFile('.env.schema.json', { foo: { type: 'string' } } |> JSON.stringify)
Expand Down Expand Up @@ -73,6 +78,43 @@ export default {
expect(typeof process.env.FOO).toEqual('string')
expect(process.env.FOO |> JSON.parse).toEqual({ bar: 'baz' })
}),
'test env': () => withLocalTmpDir(async () => {
process.env.NODE_ENV = 'test'
await outputFiles({
'.env.schema.json': { foo: { type: 'string' } } |> JSON.stringify,
'.test.env.json': { foo: 'bar' } |> JSON.stringify,
})
self.config()
expect(process.env.FOO).toEqual('bar')
}),
'test env and .env.json': () => withLocalTmpDir(async () => {
process.env.NODE_ENV = 'test'
await outputFiles({
'.env.json': { foo: 'bar' } |> JSON.stringify,
'.env.schema.json': { foo: { type: 'string' } } |> JSON.stringify,
})
expect(self.config).toThrow('dotenv: data should have required property \'foo\'')
}),
'test env and .test.env.schema.json': () => withLocalTmpDir(async () => {
process.env.NODE_ENV = 'test'
await outputFiles({
'.test.env.json': { foo: 'bar' } |> JSON.stringify,
'.test.env.schema.json': { foo: { type: 'string' } } |> JSON.stringify,
})
self.config()
expect(process.env.FOO).toEqual('bar')
}),
'test env and both .env.schema.json and .test.env.schema.json': () => withLocalTmpDir(async () => {
process.env.NODE_ENV = 'test'
await outputFiles({
'.env.schema.json': { foo: { type: 'string' } } |> JSON.stringify,
'.test.env.json': { foo: 'bar', bar: 'baz' } |> JSON.stringify,
'.test.env.schema.json': { bar: { type: 'string' } } |> JSON.stringify,
})
self.config()
expect(process.env.FOO).toEqual('bar')
expect(process.env.BAR).toEqual('baz')
}),
'schema: defaults overwritten': () => withLocalTmpDir(async () => {
delete process.env.FOO
await outputFiles({
Expand Down

0 comments on commit f271036

Please sign in to comment.