Skip to content

Commit

Permalink
refactor(tests): create test site builder and refactor dev command tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah committed Jul 30, 2020
1 parent 333f82b commit 2da6794
Show file tree
Hide file tree
Showing 36 changed files with 1,942 additions and 1,181 deletions.
1,046 changes: 556 additions & 490 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
"@oclif/test": "^1.2.5",
"algoliasearch": "^3.34.0",
"auto-changelog": "^1.14.1",
"ava": "^1.4.1",
"ava": "^2.4.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^4.3.0",
"eslint-plugin-import": "^2.22.0",
Expand All @@ -169,7 +169,10 @@
"npm-run-all": "^4.1.5",
"nyc": "^14.1.1",
"prettier": "^1.18.2",
"strip-ansi": "^5.2.0"
"seedrandom": "^3.0.5",
"strip-ansi": "^5.2.0",
"tempy": "^0.3.0",
"tomlify-j0.4": "^3.0.0"
},
"ava": {
"files": [
Expand Down
87 changes: 51 additions & 36 deletions src/utils/detect-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ const test = require('ava')
const path = require('path')
const getPort = require('get-port')
const { loadDetector, serverSettings, chooseDefaultArgs } = require('./detect-server')
const sitePath = path.join(__dirname, '..', '..', 'tests', 'dummy-site')
const { createSiteBuilder } = require('../../tests/utils/siteBuilder')

process.chdir(sitePath)
test.before(async t => {
const builder = createSiteBuilder({ siteName: 'site-for-detecting-server' })
await builder.buildAsync()

t.context.cwd = process.cwd()

process.chdir(builder.directory)

t.context.builder = builder
t.context.sitePath = builder.directory
})

test('loadDetector: valid', t => {
const d = loadDetector('create-react-app.js')
Expand All @@ -19,27 +29,27 @@ test('loadDetector: invalid', t => {

test('serverSettings: minimal config', async t => {
const env = { ...process.env }
const settings = await serverSettings({ framework: '#auto' }, {}, sitePath, () => {})
const settings = await serverSettings({ framework: '#auto' }, {}, t.context.sitePath, () => {})
t.deepEqual(settings.env, env)
t.is(settings.framework, undefined)
})

test('serverSettings: "#static" as "framework"', async t => {
const settings = await serverSettings({ framework: '#static' }, {}, sitePath, () => {})
const settings = await serverSettings({ framework: '#static' }, {}, t.context.sitePath, () => {})
t.is(settings.framework, undefined)
})

test('serverSettings: throw if "port" not available', async t => {
const port = await getPort({ port: 1 })
await t.throwsAsync(async () => {
await serverSettings({ framework: '#auto', port }, {}, sitePath, () => {})
await serverSettings({ framework: '#auto', port }, {}, t.context.sitePath, () => {})
}, /Could not acquire required "port"/)
})

test('serverSettings: "command" override npm', async t => {
const env = { ...process.env }
const devConfig = { framework: '#custom', command: 'npm run dev', targetPort: 1234 }
const settings = await serverSettings(devConfig, {}, sitePath, () => {})
const settings = await serverSettings(devConfig, {}, t.context.sitePath, () => {})
t.is(settings.framework, devConfig.framework)
t.is(settings.command, devConfig.command.split(' ')[0])
t.deepEqual(settings.args, devConfig.command.split(' ').slice(1))
Expand All @@ -49,7 +59,7 @@ test('serverSettings: "command" override npm', async t => {
test('serverSettings: "command" override yarn', async t => {
const env = { ...process.env }
const devConfig = { framework: '#custom', command: 'yarn dev', targetPort: 1234 }
const settings = await serverSettings(devConfig, {}, sitePath, () => {})
const settings = await serverSettings(devConfig, {}, t.context.sitePath, () => {})
t.is(settings.framework, devConfig.framework)
t.is(settings.command, devConfig.command.split(' ')[0])
t.deepEqual(settings.args, devConfig.command.split(' ').slice(1))
Expand All @@ -58,8 +68,8 @@ test('serverSettings: "command" override yarn', async t => {

test('serverSettings: custom framework parameters', async t => {
const env = { ...process.env }
const devConfig = { framework: '#custom', command: 'yarn dev', targetPort: 3000, publish: sitePath }
const settings = await serverSettings(devConfig, {}, sitePath, () => {})
const devConfig = { framework: '#custom', command: 'yarn dev', targetPort: 3000, publish: t.context.sitePath }
const settings = await serverSettings(devConfig, {}, t.context.sitePath, () => {})
t.is(settings.framework, '#custom')
t.is(settings.command, devConfig.command.split(' ')[0])
t.deepEqual(settings.args, devConfig.command.split(' ').slice(1))
Expand All @@ -69,40 +79,40 @@ test('serverSettings: custom framework parameters', async t => {
})

test('serverSettings: set "framework" to "#custom" but no "command"', async t => {
const devConfig = { framework: '#custom', targetPort: 3000, publish: sitePath }
const devConfig = { framework: '#custom', targetPort: 3000, publish: t.context.sitePath }
await t.throwsAsync(async () => {
await serverSettings(devConfig, {}, sitePath, () => {})
await serverSettings(devConfig, {}, t.context.sitePath, () => {})
}, /"command" and "targetPort" properties are required when "framework" is set to "#custom"/)
})

test('serverSettings: set "framework" to "#custom" but no "targetPort"', async t => {
const devConfig = { framework: '#custom', command: 'npm run dev', publish: sitePath }
const devConfig = { framework: '#custom', command: 'npm run dev', publish: t.context.sitePath }
await t.throwsAsync(async () => {
await serverSettings(devConfig, {}, sitePath, () => {})
await serverSettings(devConfig, {}, t.context.sitePath, () => {})
}, /"command" and "targetPort" properties are required when "framework" is set to "#custom"/)
})

test('serverSettings: set "framework" to "#custom" but no "targetPort" or "command"', async t => {
const devConfig = { framework: '#custom', publish: sitePath }
const devConfig = { framework: '#custom', publish: t.context.sitePath }
await t.throwsAsync(async () => {
await serverSettings(devConfig, {}, sitePath, () => {})
await serverSettings(devConfig, {}, t.context.sitePath, () => {})
}, /"command" and "targetPort" properties are required when "framework" is set to "#custom"/)
})

test('serverSettings: "functions" config', async t => {
const devConfig = { framework: '#auto', functions: path.join(sitePath, 'functions') }
const settings = await serverSettings(devConfig, {}, sitePath, () => {})
const devConfig = { framework: '#auto', functions: path.join(t.context.sitePath, 'functions') }
const settings = await serverSettings(devConfig, {}, t.context.sitePath, () => {})
t.is(settings.functions, devConfig.functions)
})

test('serverSettings: "dir" flag', async t => {
const devConfig = {
framework: '#auto',
publish: path.join(sitePath, 'build'),
functions: path.join(sitePath, 'functions'),
publish: path.join(t.context.sitePath, 'build'),
functions: path.join(t.context.sitePath, 'functions'),
}
const flags = { dir: sitePath }
const settings = await serverSettings(devConfig, flags, sitePath, () => {})
const flags = { dir: t.context.sitePath }
const settings = await serverSettings(devConfig, flags, t.context.sitePath, () => {})
t.is(settings.functions, devConfig.functions)
t.is(settings.dist, flags.dir)
t.is(settings.framework, undefined)
Expand All @@ -114,39 +124,39 @@ test('serverSettings: "dir" flag and "command" as config param', async t => {
const devConfig = {
framework: '#auto',
command: 'npm start',
publish: path.join(sitePath, 'build'),
functions: path.join(sitePath, 'functions'),
publish: path.join(t.context.sitePath, 'build'),
functions: path.join(t.context.sitePath, 'functions'),
}
const flags = { dir: sitePath }
const settings = await serverSettings(devConfig, flags, sitePath, () => {})
const flags = { dir: t.context.sitePath }
const settings = await serverSettings(devConfig, flags, t.context.sitePath, () => {})
t.is(settings.command, undefined)
t.is(settings.noCmd, true)
t.is(settings.dist, flags.dir)
})

test('serverSettings: "dir" and "targetPort" flags', async t => {
const devConfig = { framework: '#auto', functions: path.join(sitePath, 'functions') }
const flags = { dir: sitePath, targetPort: 1234 }
const devConfig = { framework: '#auto', functions: path.join(t.context.sitePath, 'functions') }
const flags = { dir: t.context.sitePath, targetPort: 1234 }
await t.throwsAsync(async () => {
await serverSettings(devConfig, flags, sitePath, () => {})
await serverSettings(devConfig, flags, t.context.sitePath, () => {})
}, /"targetPort" option cannot be used in conjunction with "dir" flag/)
})

test('serverSettings: "dir" and "command" flags', async t => {
const devConfig = { framework: '#auto', functions: path.join(sitePath, 'functions') }
const flags = { dir: sitePath, command: 'ding' }
const devConfig = { framework: '#auto', functions: path.join(t.context.sitePath, 'functions') }
const flags = { dir: t.context.sitePath, command: 'ding' }
await t.throwsAsync(async () => {
await serverSettings(devConfig, flags, sitePath, () => {})
await serverSettings(devConfig, flags, t.context.sitePath, () => {})
}, /"command" option cannot be used in conjunction with "dir" flag/)
})

test('serverSettings: when no framework is detected', async t => {
const devConfig = {
framework: '#auto',
publish: path.join(sitePath, 'build'),
functions: path.join(sitePath, 'functions'),
publish: path.join(t.context.sitePath, 'build'),
functions: path.join(t.context.sitePath, 'functions'),
}
const settings = await serverSettings(devConfig, {}, sitePath, () => {})
const settings = await serverSettings(devConfig, {}, t.context.sitePath, () => {})
t.is(settings.functions, devConfig.functions)
t.is(settings.dist, devConfig.publish)
t.is(settings.framework, undefined)
Expand All @@ -156,8 +166,8 @@ test('serverSettings: when no framework is detected', async t => {

test('serverSettings: no config', async t => {
const devConfig = { framework: '#auto' }
const settings = await serverSettings(devConfig, {}, sitePath, () => {})
t.is(settings.dist, sitePath)
const settings = await serverSettings(devConfig, {}, t.context.sitePath, () => {})
t.is(settings.dist, t.context.sitePath)
t.is(settings.framework, undefined)
t.is(settings.cmd, undefined)
t.truthy(settings.port)
Expand All @@ -170,3 +180,8 @@ test('chooseDefaultArgs', t => {
const args = chooseDefaultArgs(possibleArgsArrs)
t.deepEqual(args, possibleArgsArrs[0])
})

test.after(async t => {
process.chdir(t.context.cwd)
await t.context.builder.cleanupAsync()
})
59 changes: 40 additions & 19 deletions src/utils/env.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,52 @@
const test = require('ava')
const path = require('path')
const { getEnvSettings } = require('./env')
const { withSiteBuilder } = require('../../tests/utils/siteBuilder')

const contextSite = path.join(__dirname, '..', '..', 'tests', 'context-site')
const dummySite = path.join(__dirname, '..', '..', 'tests', 'dummy-site')
const craSite = path.join(__dirname, '..', '..', 'tests', 'site-cra')
test('should return empty object for a site with no .env file', async t => {
await withSiteBuilder('site-without-env-file', async builder => {
await builder.buildAsync()

test('no .env files', async t => {
const vars = await getEnvSettings(contextSite)
t.deepEqual(vars, {})
const vars = await getEnvSettings(builder.directory)
t.deepEqual(vars, {})
})
})

test('.env.development file', async t => {
const vars = await getEnvSettings(dummySite)
t.deepEqual(vars, {
file: path.resolve(dummySite, '.env.development'),
vars: {
EASY_VAR: 'true',
DUMMY_VAR: 'false',
},
test('should read env vars from .env.development file', async t => {
await withSiteBuilder('site-with-envs-file', async builder => {
builder
.withEnvFile({
path: '.env',
env: { TEST: 'FROM_ENV' },
})
.withEnvFile({
path: '.env.development',
env: { TEST: 'FROM_DEVELOPMENT_ENV' },
})
await builder.buildAsync()

const vars = await getEnvSettings(builder.directory)
t.deepEqual(vars, {
file: path.resolve(builder.directory, '.env.development'),
vars: {
TEST: 'FROM_DEVELOPMENT_ENV',
},
})
})
})

test('.env file', async t => {
const vars = await getEnvSettings(craSite)
t.deepEqual(vars, {
file: path.resolve(craSite, '.env'),
vars: {},
test('should handle empty .env file', async t => {
await withSiteBuilder('site-with-empty-env-file', async builder => {
builder.withEnvFile({
path: '.env',
})

await builder.buildAsync()

const vars = await getEnvSettings(builder.directory)
t.deepEqual(vars, {
file: path.resolve(builder.directory, '.env'),
vars: {},
})
})
})
38 changes: 25 additions & 13 deletions src/utils/get-functions.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
const test = require('ava')
const path = require('path')
const { withSiteBuilder } = require('../../tests/utils/siteBuilder')
const { getFunctions } = require('./get-functions.js')
const { findModuleDir } = require('./finders')
const sitePath = path.join(__dirname, '..', '..', 'tests', 'dummy-site')

test('pass empty string', t => {
test('should return empty object when an empty string is provided', t => {
const f = getFunctions('')
t.deepEqual(f, {})
})

test('pass directory with no *.js files', t => {
const f = getFunctions(sitePath)
t.deepEqual(f, {})
test('should return an empty object for a directory with no js files', async t => {
await withSiteBuilder('site-without-functions', async builder => {
await builder.buildAsync()

const f = getFunctions(builder.directory)
t.deepEqual(f, {})
})
})

test('pass dummy repository with *.js files', t => {
const sitePath = path.join(__dirname, '..', '..', 'tests', 'dummy-repo')
const f = getFunctions(sitePath)
t.deepEqual(f, {
index: {
functionPath: path.join(sitePath, 'index.js'),
moduleDir: findModuleDir(sitePath),
},
test('should return object with function details for a directory with js files', async t => {
await withSiteBuilder('site-without-functions', async builder => {
builder.withFunction({
path: 'index.js',
handler: '',
})
await builder.buildAsync()

const functions = path.join(builder.directory, 'functions')
const f = getFunctions(functions)
t.deepEqual(f, {
index: {
functionPath: path.join(functions, 'index.js'),
moduleDir: findModuleDir(functions),
},
})
})
})

0 comments on commit 2da6794

Please sign in to comment.