Skip to content

Commit

Permalink
Small tweaks to new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
af committed Oct 31, 2021
1 parent 94ea57a commit 086609b
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions tests/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,20 @@ test('choices field', () => {
expect(() => cleanEnv({ FOO: 'hi' }, { FOO: str({ choices: 123 }) }, makeSilent)).toThrow()
})

test('choices typed', () => {
type Spec = {
NODE_ENV: 'production' | 'test' | 'development'
}

const environment = {
NODE_ENV: 'test',
}
test('choices should refine the type of the field to a union', () => {
type NodeEnvType = 'production' | 'test' | 'development'

const spec: Spec = cleanEnv(environment, {
NODE_ENV: str({
choices: ['production', 'test', 'development'],
}),
const spec = cleanEnv({ NODE_ENV: 'test' }, {
NODE_ENV: str({ choices: ['production', 'test', 'development'] }),
})

expect(spec.NODE_ENV).toEqual('test')
// type of the output should be the union type, not the more generic `string`
const nodeEnv: NodeEnvType = spec.NODE_ENV

// @ts-expect-error specifying a type that doesn't match the choices union type should cause an error
const shouldFail: 'test' | 'wrong' = spec.NODE_ENV

expect(nodeEnv).toEqual('test')
})

test('misconfigured spec', () => {
Expand Down

0 comments on commit 086609b

Please sign in to comment.