Skip to content

Commit

Permalink
test: refactor build tests (#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah authored Aug 3, 2020
1 parent e256d96 commit 6bb0969
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 83 deletions.
70 changes: 0 additions & 70 deletions tests/build.test.js

This file was deleted.

127 changes: 127 additions & 0 deletions tests/command.build.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const path = require('path')
const test = require('ava')
const execa = require('execa')
const { withSiteBuilder } = require('./utils/siteBuilder')

const cliPath = require('./utils/cliPath')

// Runs `netlify build ...flags` then verify:
// - its exit code is `exitCode`
// - that its output contains `output`
// The command is run in the fixture directory `fixtureSubDir`.
const runBuildCommand = async function(t, cwd, { exitCode: expectedExitCode = 0, output, flags = [], env } = {}) {
const { all, exitCode } = await execa(cliPath, ['build', ...flags], {
reject: false,
cwd,
env: { FORCE_COLOR: '1', NETLIFY_AUTH_TOKEN: 'test', ...env },
all: true,
})

if (exitCode !== expectedExitCode) {
console.error(all)
}

t.true(all.includes(output))
t.is(exitCode, expectedExitCode)
}

test('should print output for a successful command', async t => {
await withSiteBuilder('success-site', async builder => {
builder.withNetlifyToml({ config: { build: { command: 'echo testCommand' } } })

await builder.buildAsync()

await runBuildCommand(t, builder.directory, { output: 'testCommand' })
})
})

test('should print output for a failed command', async t => {
await withSiteBuilder('failure-site', async builder => {
builder.withNetlifyToml({ config: { build: { command: 'doesNotExist' } } })

await builder.buildAsync()

await runBuildCommand(t, builder.directory, { exitCode: 1, output: 'doesNotExist' })
})
})

test('should set the build mode to cli', async t => {
await withSiteBuilder('success-site', async builder => {
builder.withNetlifyToml({ config: { build: { command: 'echo testCommand' } } })

await builder.buildAsync()

await runBuildCommand(t, builder.directory, { output: 'mode: cli' })
})
})

test('should run in dry mode when the --dry flag is set', async t => {
await withSiteBuilder('success-site', async builder => {
builder.withNetlifyToml({ config: { build: { command: 'echo testCommand' } } })

await builder.buildAsync()

await runBuildCommand(t, builder.directory, { flags: ['--dry'], output: 'If this looks good to you' })
})
})

test('should run the staging context command when the --context option is set to staging', async t => {
await withSiteBuilder('context-site', async builder => {
builder.withNetlifyToml({
config: {
build: { command: 'echo testCommand' },
context: { staging: { command: 'echo testStaging' } },
},
})

await builder.buildAsync()

await runBuildCommand(t, builder.directory, { flags: ['--context=staging'], output: 'testStaging' })
})
})

test('should print debug information when the --debug flag is set', async t => {
await withSiteBuilder('success-site', async builder => {
builder.withNetlifyToml({ config: { build: { command: 'echo testCommand' } } })

await builder.buildAsync()

await runBuildCommand(t, builder.directory, { flags: ['--debug'], output: 'Resolved config' })
})
})

test('should use root directory netlify.toml when runs in subdirectory', async t => {
await withSiteBuilder('subdir-site', async builder => {
builder
.withNetlifyToml({ config: { build: { command: 'echo testCommand' } } })
.withContentFile({ path: path.join('subdir', '.gitkeep'), content: '' })

await builder.buildAsync()

await runBuildCommand(t, path.join(builder.directory, 'subdir'), { output: 'testCommand' })
})
})

test('should error when using invalid netlify.toml', async t => {
await withSiteBuilder('wrong-config-site', async builder => {
builder.withNetlifyToml({ config: { build: { command: false } } })

await builder.buildAsync()

await runBuildCommand(t, builder.directory, { exitCode: 1, output: 'Invalid syntax' })
})
})

test('should error when a site id is missing', async t => {
await withSiteBuilder('success-site', async builder => {
builder.withNetlifyToml({ config: { build: { command: 'echo testCommand' } } })

await builder.buildAsync()

await runBuildCommand(t, builder.directory, {
exitCode: 1,
output: 'Could not find the site ID',
env: { NODE_ENV: '' },
})
})
})
5 changes: 0 additions & 5 deletions tests/context-site/netlify.toml

This file was deleted.

2 changes: 0 additions & 2 deletions tests/failure-site/netlify.toml

This file was deleted.

2 changes: 0 additions & 2 deletions tests/subdir-site/netlify.toml

This file was deleted.

Empty file removed tests/subdir-site/subdir/.gitkeep
Empty file.
2 changes: 0 additions & 2 deletions tests/success-site/netlify.toml

This file was deleted.

2 changes: 0 additions & 2 deletions tests/wrong-config-site/netlify.toml

This file was deleted.

0 comments on commit 6bb0969

Please sign in to comment.