Skip to content

Commit

Permalink
test: align fixtures to the README's examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ngryman committed Nov 20, 2020
1 parent 4b2f55a commit 6d0b84e
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 152 deletions.
62 changes: 34 additions & 28 deletions src/fauda.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,52 @@ describe('given a JSON schema', () => {
args: [],
cwd: '',
env: {
PASTA_COOKING_TIME: '200',
PASTA_SEASONING: "['Salt', 'Pepper', 'Tomato Sauce']",
MY_APP_PORT: '8080',
MY_APP_OPEN: 'true',
MY_APP_PUBLIC_PAGES: "['/home', '/about']",
NODE_ENV: 'development'
}
}
const configuration = await fauda(
'pasta',
'my-app',
'test/fixtures/schema.json',
options
)
expect(configuration).toMatchInlineSnapshot(`
Object {
"cookingTime": 200,
"seasoning": Array [
"['Salt', 'Pepper', 'Tomato Sauce']",
"mode": "development",
"open": true,
"port": 8080,
"publicPages": Array [
"['/home', '/about']",
],
"type": "Fettuccine",
}
`)
})

it('loads command-line arguments', async () => {
const options: FaudaOptions = {
args: [
'--cooking-time=200',
'--seasoning=Salt',
'--seasoning=Pepper',
"--seasoning='Tomato Sauce'"
'--port=8080',
'--open',
'--public-pages=/home',
'--public-pages=/about'
],
cwd: '',
env: {}
env: {
NODE_ENV: 'development'
}
}
const config = await fauda('pasta', 'test/fixtures/schema.json', options)
const config = await fauda('my-app', 'test/fixtures/schema.json', options)
expect(config).toMatchInlineSnapshot(`
Object {
"cookingTime": 200,
"seasoning": Array [
"Salt",
"Pepper",
"'Tomato Sauce'",
"mode": "development",
"open": true,
"port": 8080,
"publicPages": Array [
"/home",
"/about",
],
"type": "Fettuccine",
}
`)
})
Expand All @@ -61,18 +65,20 @@ describe('given a JSON schema', () => {
const options: FaudaOptions = {
args: [],
cwd: testProject.rootDir,
env: {}
env: {
NODE_ENV: 'development'
}
}
const config = await fauda('pasta', 'test/fixtures/schema.json', options)
const config = await fauda('my-app', 'test/fixtures/schema.json', options)
expect(config).toMatchInlineSnapshot(`
Object {
"cookingTime": 200,
"seasoning": Array [
"Salt",
"Pepper",
"Tomato Sauce",
"mode": "development",
"open": true,
"port": 8080,
"publicPages": Array [
"/home",
"/about",
],
"type": "Fettuccine",
}
`)
} finally {
Expand All @@ -83,7 +89,7 @@ describe('given a JSON schema', () => {

describe('given invalid arguments', () => {
it('throws an error if the schema does not exists', async () => {
await expect(() => fauda('pasta', '/the/void')).rejects
await expect(() => fauda('my-app', '/the/void')).rejects
.toThrowErrorMatchingInlineSnapshot(`
"load: Error loading schema
ENOENT: no such file or directory, open '/the/void'"
Expand Down
76 changes: 47 additions & 29 deletions src/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { promises as fs } from 'fs'
import { generateTypes } from './generator'

async function getSchema() {
return JSON.parse(await fs.readFile('test/fixtures/schema.json', 'utf8'))
return JSON.parse(
await fs.readFile('test/fixtures/required-schema.json', 'utf8')
)
}

describe('given a schema object', () => {
Expand All @@ -12,21 +14,25 @@ describe('given a schema object', () => {
expect(source).toMatchInlineSnapshot(`
"export interface Configuration {
/**
* Path to my pasta app's schema.
* Path to my app's schema.
*/
$schema?: string;
/**
* The type of pasta.
* The port the server listens to.
*/
type: \\"Fettuccine\\" | \\"Tagliatelle\\";
port?: number;
/**
* Cooking time in seconds.
* Open in a browser tab if true.
*/
cookingTime?: number;
open?: boolean;
/**
* A list of seasoning ingredients.
* Mode of the app.
*/
seasoning?: string[];
mode?: \\"development\\" | \\"test\\" | \\"production\\";
/**
* A list of public pages.
*/
publicPages: string[];
}
"
`)
Expand All @@ -42,21 +48,25 @@ describe('given a schema object', () => {
expect(source).toMatchInlineSnapshot(`
"export interface Configuration {
/**
* Path to my pasta app's schema.
* Path to my app's schema.
*/
$schema?: string
/**
* The type of pasta.
* The port the server listens to.
*/
port?: number
/**
* Open in a browser tab if true.
*/
type: \\"Fettuccine\\" | \\"Tagliatelle\\"
open?: boolean
/**
* Cooking time in seconds.
* Mode of the app.
*/
cookingTime?: number
mode?: \\"development\\" | \\"test\\" | \\"production\\"
/**
* A list of seasoning ingredients.
* A list of public pages.
*/
seasoning?: string[]
publicPages: string[]
}
"
`)
Expand All @@ -69,21 +79,25 @@ describe('given a schema file', () => {
expect(source).toMatchInlineSnapshot(`
"export interface Configuration {
/**
* Path to my pasta app's schema.
* Path to my app's schema.
*/
$schema?: string;
/**
* The type of pasta.
* The port the server listens to.
*/
type: \\"Fettuccine\\" | \\"Tagliatelle\\";
port?: number;
/**
* Cooking time in seconds.
* Open in a browser tab if true.
*/
cookingTime?: number;
open?: boolean;
/**
* A list of seasoning ingredients.
* Mode of the app.
*/
seasoning?: string[];
mode?: \\"development\\" | \\"test\\" | \\"production\\";
/**
* A list of public pages.
*/
publicPages?: string[];
}
"
`)
Expand All @@ -98,21 +112,25 @@ describe('given a schema file', () => {
expect(source).toMatchInlineSnapshot(`
"export interface Configuration {
/**
* Path to my pasta app's schema.
* Path to my app's schema.
*/
$schema?: string
/**
* The type of pasta.
* The port the server listens to.
*/
port?: number
/**
* Open in a browser tab if true.
*/
type: \\"Fettuccine\\" | \\"Tagliatelle\\"
open?: boolean
/**
* Cooking time in seconds.
* Mode of the app.
*/
cookingTime?: number
mode?: \\"development\\" | \\"test\\" | \\"production\\"
/**
* A list of seasoning ingredients.
* A list of public pages.
*/
seasoning?: string[]
publicPages?: string[]
}
"
`)
Expand Down
13 changes: 7 additions & 6 deletions src/loaders/loadFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { TestProject } from '../../test/utils/testProject'
import { loadFromFile, getDefaultSearchPlaces } from './loadFile'

const SEARCH_PLACES = getDefaultSearchPlaces('pasta')
const SEARCH_PLACES = getDefaultSearchPlaces('my-app')

const TEST_CASES: [string, string][] = [
['load in current directory', '.'],
['load in parent directory', 'upward']
]

const EXCEPTED = {
cookingTime: 200,
seasoning: ['Salt', 'Pepper', 'Tomato Sauce']
const EXPECTED = {
port: 8080,
open: true,
publicPages: ['/home', '/about']
}

describe.each(SEARCH_PLACES)('given a %s file', variant => {
Expand All @@ -20,8 +21,8 @@ describe.each(SEARCH_PLACES)('given a %s file', variant => {

test.each(TEST_CASES)('%s', async (_title, cwd) => {
await expect(
loadFromFile('pasta', testProject.resolvePath(cwd))
).resolves.toEqual(EXCEPTED)
loadFromFile('my-app', testProject.resolvePath(cwd))
).resolves.toEqual(EXPECTED)
})

afterAll(() => testProject.teardown())
Expand Down
43 changes: 12 additions & 31 deletions src/normalizer/normalize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,34 @@ async function getSchema() {

describe('given configuration', () => {
it('merges default configuration', async () => {
expect(normalize({ cookingTime: 200 }, await getSchema(), process.env))
expect(normalize({}, await getSchema(), process.env))
.toMatchInlineSnapshot(`
Object {
"cookingTime": 200,
"seasoning": Array [
"Salt",
"Pepper",
"Olive Oil",
"Pecorino",
"mode": "test",
"open": false,
"port": 3000,
"publicPages": Array [
"/hi",
],
"type": "Fettuccine",
}
`)
})

it('expands environment variables', async () => {
expect(
normalize({ cookingTime: '${NUMBER}' }, await getSchema(), {
NUMBER: '200'
normalize({ port: '${PORT}' }, await getSchema(), {
...process.env,
PORT: '8080'
})
).toMatchObject({ cookingTime: 200 })
).toMatchObject({ port: 8080 })
})

it('throws an error for invalid values', async () => {
await expect(async () =>
normalize({ cookingTime: 'nope' }, await getSchema(), process.env)
normalize({ port: 'nope' }, await getSchema(), process.env)
).rejects.toThrowErrorMatchingInlineSnapshot(`
"validate: Validation failed
.cookingTime should be number"
.port should be number"
`)
})
})

describe('given empty configuration', () => {
it('returns the default configuration', async () => {
expect(normalize({}, await getSchema(), process.env))
.toMatchInlineSnapshot(`
Object {
"cookingTime": 300,
"seasoning": Array [
"Salt",
"Pepper",
"Olive Oil",
"Pecorino",
],
"type": "Fettuccine",
}
`)
})
})
Loading

0 comments on commit 6d0b84e

Please sign in to comment.