From adeb199a172106b4617c48b55c261f66f7e84c4b Mon Sep 17 00:00:00 2001 From: ntwcklng Date: Sat, 31 Dec 2016 13:50:31 +0100 Subject: [PATCH] tests --- test/create-test.js | 30 ++++++++++++++++------- test/install-test.js | 57 +++++++++++++++++++++++++------------------- 2 files changed, 54 insertions(+), 33 deletions(-) diff --git a/test/create-test.js b/test/create-test.js index e2642fa..32e0553 100644 --- a/test/create-test.js +++ b/test/create-test.js @@ -10,6 +10,19 @@ import {VERSIONS} from '../build/constants' let origCwd let tmpDir +const run = args => Promise.resolve() +.then(async () => { + try { + await cli(args) + } catch (err) { + if (err.userError) { + console.error(`> ${err.message}`) + } else { + console.error(`> ${err.stack}`) + } + } +}) + test.before(() => { origCwd = process.cwd() tmpDir = temp.mkdirSync('frame-new') @@ -23,6 +36,7 @@ test('create a new react project without errors', async t => { t.is(pkg.dependencies.react, VERSIONS[proj.type]) t.is(proj.name, tmpDir) t.is(proj.type, 'react') + return }) }) @@ -33,6 +47,7 @@ test('create a new preact project without errors', async t => { t.is(pkg.dependencies.preact, VERSIONS[proj.type]) t.is(proj.name, tmpDir) t.is(proj.type, 'preact') + return }) }) @@ -43,18 +58,17 @@ test('create a new Next.js project without errors', async t => { t.is(pkg.dependencies.next, VERSIONS[proj.type]) t.is(proj.name, tmpDir) t.is(proj.type, 'next') + return }) }) test('should exit when an invalid project type is passed', async t => { - await cli({_: ['invalid', 'tmp-no-exist']}).catch(err => { - if (err) { - const firstLine = err.message.split('\n')[0] - t.is(firstLine, 'invalid is not a valid FRAME project type') - } else { - t.fail('No errors?') - } - }) + try { + await run({_: ['invalid', 'tmp-no-exist']}) + } catch (err) { + const firstLine = err.message.split('\n')[0] + t.is(firstLine, 'invalid is not a valid FRAME project type') + } }) test.after.always('cleanup', () => { diff --git a/test/install-test.js b/test/install-test.js index 2da03a6..a1aa2ac 100644 --- a/test/install-test.js +++ b/test/install-test.js @@ -8,26 +8,27 @@ import temp from 'temp' import cli from '../build/' import {VERSIONS} from '../build/constants' -function run(opts = {}) { - return new Promise((resolve, reject) => { - cli({_: [opts.type, opts.targetDir], 'skip-git': true, silent: true}).then(() => { - const cwd = path.resolve(process.cwd(), `./${opts.targetDir}`) - const pkg = require(path.join(cwd, 'package.json')) - exec('npm run build', {cwd, stdio: 'ignore'}, () => { - glob('**', { - dot: true, - cwd, - ignore: opts.ignore - }, (err, matches) => { - if (err) { - reject(err) - } - resolve({matches, pkg}) - }) +const run = opts => new Promise(async (resolve, reject) => { + try { + await cli({_: [opts.type, opts.targetDir], 'skip-git': true, silent: false}) + const cwd = path.resolve(process.cwd(), `./${opts.targetDir}`) + const pkg = require(path.join(cwd, 'package.json')) + await exec('npm run build', {cwd, stdio: 'inherit'}, () => { + glob('**', { + dot: true, + cwd, + ignore: opts.ignore + }, (err, matches) => { + if (err) { + throw new Error(err) + } + return resolve({matches, pkg}) }) }) - }) -} + } catch (err) { + reject(err) + } +}) let origCwd let tmpDir @@ -97,6 +98,7 @@ test('SKIP_WATCH create a new Preact Project, install dependencies and build', a }) t.deepEqual(result.matches, shouldMatch) t.is(result.pkg.dependencies[type], VERSIONS[type]) + return }) /** @@ -113,13 +115,18 @@ test('SKIP_WATCH create a new Next.js Project, install dependencies and build', 'pages', 'pages/index.js' ] - const result = await run({ - type, - targetDir, - ignore: ['node_modules/**', '.next/bundles/**', '.next/dist/**'] - }) - t.deepEqual(result.matches, shouldMatch) - t.is(result.pkg.dependencies[type], VERSIONS[type]) + try { + const result = await run({ + type, + targetDir, + ignore: ['node_modules/**', '.next/bundles/**', '.next/dist/**'] + }) + t.deepEqual(result.matches, shouldMatch) + t.is(result.pkg.dependencies[type], VERSIONS[type]) + return + } catch (err) { + throw new Error(err) + } }) test.after.always('cleanup', () => {