diff --git a/README.md b/README.md index 0bfde84..a202849 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,25 @@ Install the [ts-node](https://www.npmjs.com/package/ts-node) NPM package then us hayspec --require ts-node/register ``` +#### Project configuration + +Hayspec configuration options can be saved inside the `package.json` file under the the `hayspec` key. + +``` +{ + "hayspec": { + "require": [ + "ts-node/register" + ], + "match": [ + "./src/**/*.test.*" + ] + } +} +``` + +Note that these options can be overriden by providing CLI arguments. + ## Hayspec packages | Package | Description | Version diff --git a/packages/hayspec-cli/package.json b/packages/hayspec-cli/package.json index ccf692f..fa1bd82 100644 --- a/packages/hayspec-cli/package.json +++ b/packages/hayspec-cli/package.json @@ -1,6 +1,6 @@ { "name": "@hayspec/cli", - "version": "0.5.0", + "version": "0.7.0", "description": "CLI for Hayspec framework.", "main": "./dist/index.js", "types": "./dist/index.d.ts", @@ -11,7 +11,15 @@ "clean": "rm -Rf ./dist", "transpile": "npm run clean; tsc", "prepare": "npm run transpile", - "test": "npm run transpile && nyc ava" + "test": "npm run transpile && nyc ava --verbose" + }, + "hayspec": { + "require": [ + "ts-node/register" + ], + "match": [ + "./src/**/*.hay.*" + ] }, "ava": { "compileEnhancements": false, @@ -61,15 +69,14 @@ "ava": "1.0.0-beta.6", "nyc": "^12.0.2", "ts-node": "^7.0.1", - "typescript": "^3.0.1" + "typescript": "^3.1.1" }, "dependencies": { - "@hayspec/init": "^0.5.0", - "@hayspec/reporter": "^0.5.0", - "@hayspec/runner": "^0.5.0", - "@hayspec/spec": "^0.5.0", + "@hayspec/init": "^0.7.0", + "@hayspec/reporter": "^0.7.0", + "@hayspec/runner": "^0.7.0", + "@hayspec/spec": "^0.7.0", "inquirer": "^6.0.0", "yargs": "^11.0.0" - }, - "gitHead": "03535c698ebd35ec94c63869f18cdde2380f739f" + } } diff --git a/packages/hayspec-cli/src/commands/init.ts b/packages/hayspec-cli/src/commands/init.ts index 1c381a1..f480bde 100644 --- a/packages/hayspec-cli/src/commands/init.ts +++ b/packages/hayspec-cli/src/commands/init.ts @@ -1,12 +1,13 @@ import * as inquirer from 'inquirer'; import { Generator } from '@hayspec/init'; import { Printer } from '@hayspec/reporter'; +import { getConfig } from '../lib/env'; /** * Initializes project directory. */ export default async function (argv) { - const { name, description } = argv; + const { name, description } = getConfig(argv); const root = process.cwd(); const printer = new Printer(); @@ -52,8 +53,10 @@ export default async function (argv) { printer.colorize('gray', `$ npm test`) ); printer.end(); - + process.exit(0); + } catch (e) { console.error(e); + process.exit(2); } } diff --git a/packages/hayspec-cli/src/commands/test.ts b/packages/hayspec-cli/src/commands/test.ts index 12ffa7e..5a6b0b5 100644 --- a/packages/hayspec-cli/src/commands/test.ts +++ b/packages/hayspec-cli/src/commands/test.ts @@ -1,12 +1,13 @@ import { Runner } from '@hayspec/runner'; import { Spec, Stage } from '@hayspec/spec'; import { DefaultReporter } from '@hayspec/reporter'; +import { getConfig } from '../lib/env'; /** * Runs tests. */ export default async function (argv) { - const { match } = argv; + const { match } = getConfig(argv); const reporter = new DefaultReporter(); const stage = new Stage(reporter); const test = new Spec(stage); @@ -18,5 +19,11 @@ export default async function (argv) { test.spec(message, result.spec); }); - await test.perform(); + try { + await test.perform(); + process.exit(reporter.failedCount ? 1 : 0); + } catch (e) { + console.log(e); + process.exit(2); + } } diff --git a/packages/hayspec-cli/src/index.ts b/packages/hayspec-cli/src/index.ts index 2353ee2..829eed0 100644 --- a/packages/hayspec-cli/src/index.ts +++ b/packages/hayspec-cli/src/index.ts @@ -1,13 +1,14 @@ import * as yargs from 'yargs'; import initHandler from './commands/init'; import testHandler from './commands/test'; +import { getConfig } from './lib/env'; /** * Interface definition. */ const { argv } = yargs .usage('Usage: $0 --help') - .command('init', 'Initializes project directory.', (yargs) => yargs + .command('init', 'Initializes project directory.', (yargs) => yargs .option('name', { string: true, description: 'Project name', @@ -21,7 +22,6 @@ const { argv } = yargs .option('match', { array: true, description: 'Match pattern', - default: ['./src/cro**/*.test.*'], }) .option('require', { array: true, @@ -35,6 +35,4 @@ const { argv } = yargs /** * Upgrading environment. */ -if (Array.isArray(argv.require)) { - argv.require.forEach((v) => require(v)); -} +getConfig(argv).require.forEach((v) => require(v)); diff --git a/packages/hayspec-cli/src/lib/env.ts b/packages/hayspec-cli/src/lib/env.ts new file mode 100644 index 0000000..fc40f1b --- /dev/null +++ b/packages/hayspec-cli/src/lib/env.ts @@ -0,0 +1,26 @@ +import * as pt from 'path'; + +/** + * Returns package.json data. + */ +export function getPackage() { + try { + return require(pt.join(process.cwd(), 'package.json')) || {}; + } catch (e) { + return {}; + } +} + +/** + * Returns Hayspec options. + */ +export function getConfig(argv?: any) { + const defaults = getPackage()['hayspec'] || {}; + const custom = argv || {}; + return { + name: custom['name'] || defaults['name'] || '', + description: custom['description'] || defaults['description'] || '', + require: custom['require'] || defaults['require'] || [], + match: custom['match'] || defaults['match'] || [], + }; +} diff --git a/packages/hayspec-cli/src/tests/assets/foo.hay.ts b/packages/hayspec-cli/src/tests/assets/invalid.hay.ts similarity index 62% rename from packages/hayspec-cli/src/tests/assets/foo.hay.ts rename to packages/hayspec-cli/src/tests/assets/invalid.hay.ts index 195d7f5..c9a2859 100644 --- a/packages/hayspec-cli/src/tests/assets/foo.hay.ts +++ b/packages/hayspec-cli/src/tests/assets/invalid.hay.ts @@ -3,13 +3,12 @@ import { Spec } from '@hayspec/spec'; const spec = new Spec(); spec.test('foo', async (context) => { - context.is(true, true); + context.true(true); }); spec.test('bar', async (context) => { - context.is(true, true); - context.is(true, true); - context.is(true, false); + context.true(false); + context.false(true); }); export default spec; diff --git a/packages/hayspec-cli/src/tests/assets/valid.hay.ts b/packages/hayspec-cli/src/tests/assets/valid.hay.ts new file mode 100644 index 0000000..0a7baf3 --- /dev/null +++ b/packages/hayspec-cli/src/tests/assets/valid.hay.ts @@ -0,0 +1,14 @@ +import { Spec } from '@hayspec/spec'; + +const spec = new Spec(); + +spec.test('foo', async (context) => { + context.true(true); +}); + +spec.test('bar', async (context) => { + context.true(true); + context.true(true); +}); + +export default spec; diff --git a/packages/hayspec-cli/src/tests/index.test.ts b/packages/hayspec-cli/src/tests/index.test.ts index 84e144b..14ed135 100644 --- a/packages/hayspec-cli/src/tests/index.test.ts +++ b/packages/hayspec-cli/src/tests/index.test.ts @@ -4,16 +4,28 @@ import * as cproc from 'child_process'; const exec = util.promisify(cproc.exec); -test('runs tests', async (t) => { - const command = './bin/hayspec test --require ts-node/register --match ./src/tests/assets/**/*.hay.*'; +test('initializes current folder', async (t) => { + const command = `mkdir -p ./node_modules/.tmp/test; cd ./node_modules/.tmp/test; ../../../bin/hayspec init --name foo --description bar; echo code: $?`; const { stdout, stderr } = await exec(command); - t.true(stdout.indexOf('src/tests/assets/foo.hay.ts') !== -1); + t.true(stdout.indexOf('Continue by running the commands below:') !== -1); + t.true(stdout.indexOf('code: 0') !== -1); t.true(stderr === ''); }); -test('initializes current folder', async (t) => { - const command = `mkdir -p ./node_modules/.tmp/test; cd ./node_modules/.tmp/test; ../../../bin/hayspec init --name foo --description bar`; +test('runs valid tests', async (t) => { + const command = './bin/hayspec test --require ts-node/register --match ./src/tests/assets/**/valid.hay.*; echo code: $?'; const { stdout, stderr } = await exec(command); - t.true(stdout.indexOf('Continue by running the commands below:') !== -1); + t.true(stdout.indexOf('src/tests/assets/valid.hay.ts') !== -1); + t.true(stdout.indexOf('src/tests/assets/invalid.hay.ts') === -1); + t.true(stdout.indexOf('code: 0') !== -1); + t.true(stderr === ''); +}); + +test('runs invalid tests', async (t) => { + const command = './bin/hayspec test --require ts-node/register --match ./src/tests/assets/**/invalid.hay.*; echo code: $?'; + const { stdout, stderr } = await exec(command); + t.true(stdout.indexOf('src/tests/assets/valid.hay.ts') === -1); + t.true(stdout.indexOf('src/tests/assets/invalid.hay.ts') !== -1); + t.true(stdout.indexOf('code: 1') !== -1); t.true(stderr === ''); }); diff --git a/packages/hayspec-cli/src/tests/lib/env.test.ts b/packages/hayspec-cli/src/tests/lib/env.test.ts new file mode 100644 index 0000000..25e2062 --- /dev/null +++ b/packages/hayspec-cli/src/tests/lib/env.test.ts @@ -0,0 +1,24 @@ +import test from 'ava'; +import { getConfig } from '../../lib/env'; + +test('method `getConfig` returns package.json hayspec configuration', async (t) => { + t.deepEqual(getConfig(), { + name: '', + description: '', + require: ['ts-node/register'], + match: ['./src/**/*.hay.*'], + }); +}); + +test('method `getConfig` merges reveived configuration', async (t) => { + t.deepEqual(getConfig({ + extension: [], + require: ['bar'], + match: ['foo'], + }), { + name: '', + description: '', + require: ['bar'], + match: ['foo'], + }); +}); diff --git a/packages/hayspec-init/package.json b/packages/hayspec-init/package.json index 21eb69d..99ed4cb 100644 --- a/packages/hayspec-init/package.json +++ b/packages/hayspec-init/package.json @@ -1,6 +1,6 @@ { "name": "@hayspec/init", - "version": "0.5.0", + "version": "0.7.0", "description": "Project generator for Hayspec framework.", "main": "./dist/index.js", "types": "./dist/index.d.ts", @@ -62,7 +62,7 @@ "ava": "1.0.0-beta.6", "nyc": "^12.0.2", "ts-node": "^7.0.1", - "typescript": "^3.0.1" + "typescript": "^3.1.1" }, "dependencies": { "fs-extra": "^6.0.1" diff --git a/packages/hayspec-init/src/core/structure.ts b/packages/hayspec-init/src/core/structure.ts index 60f3bcf..cedab34 100644 --- a/packages/hayspec-init/src/core/structure.ts +++ b/packages/hayspec-init/src/core/structure.ts @@ -35,9 +35,17 @@ export const files = [ ` "version": "0.0.0",`, ` "description": "{{ description }}",`, ` "scripts": {`, - ` "transpile": "tsc",`, - ` "prepare": "npm run transpile",`, - ` "test": "hayspec test --require ts-node/register --match './src/tests/**/*.test.ts'"`, + ` "build": "tsc",`, + ` "prepare": "npm run build",`, + ` "test": "hayspec test"`, + ` },`, + ` "hayspec": {`, + ` "require": [`, + ` "ts-node/register"`, + ` ],`, + ` "match": [`, + ` "./src/tests/**/*.test.ts"`, + ` ]`, ` },`, ` "license": "MIT",`, ` "devDependencies": {`, diff --git a/packages/hayspec-reporter/package.json b/packages/hayspec-reporter/package.json index 006d6c0..5590512 100644 --- a/packages/hayspec-reporter/package.json +++ b/packages/hayspec-reporter/package.json @@ -1,6 +1,6 @@ { "name": "@hayspec/reporter", - "version": "0.5.0", + "version": "0.7.0", "description": "Reporter for Hayspec framework.", "main": "./dist/index.js", "types": "./dist/index.d.ts", @@ -62,11 +62,10 @@ "ava": "1.0.0-beta.6", "nyc": "^12.0.2", "ts-node": "^7.0.1", - "typescript": "^3.0.1" + "typescript": "^3.1.1" }, "dependencies": { - "@hayspec/spec": "^0.5.0", + "@hayspec/spec": "^0.7.0", "chalk": "^2.4.1" - }, - "gitHead": "03535c698ebd35ec94c63869f18cdde2380f739f" + } } diff --git a/packages/hayspec-reporter/src/reporters/default.ts b/packages/hayspec-reporter/src/reporters/default.ts index e3927df..d2329db 100644 --- a/packages/hayspec-reporter/src/reporters/default.ts +++ b/packages/hayspec-reporter/src/reporters/default.ts @@ -7,9 +7,9 @@ import { Printer } from '../lib/printer'; export class DefaultReporter extends Reporter { protected printer: Printer; protected assertionResults: boolean[] = []; - protected passedCount: number = 0; - protected skippedCount: number = 0; - protected failedCount: number = 0; + public passedCount: number = 0; + public skippedCount: number = 0; + public failedCount: number = 0; /** * diff --git a/packages/hayspec-runner/package.json b/packages/hayspec-runner/package.json index 2082617..a6b05b5 100644 --- a/packages/hayspec-runner/package.json +++ b/packages/hayspec-runner/package.json @@ -1,6 +1,6 @@ { "name": "@hayspec/runner", - "version": "0.5.0", + "version": "0.7.0", "description": "Tests runner for Hayspec framework.", "main": "./dist/index.js", "types": "./dist/index.d.ts", @@ -62,10 +62,10 @@ "ava": "1.0.0-beta.6", "nyc": "^12.0.2", "ts-node": "^7.0.1", - "typescript": "^3.0.1" + "typescript": "^3.1.1" }, "dependencies": { - "@hayspec/spec": "^0.5.0", + "@hayspec/spec": "^0.7.0", "fast-glob": "^2.2.2" }, "gitHead": "03535c698ebd35ec94c63869f18cdde2380f739f" diff --git a/packages/hayspec-spec/package.json b/packages/hayspec-spec/package.json index cb5a31a..1617b0c 100644 --- a/packages/hayspec-spec/package.json +++ b/packages/hayspec-spec/package.json @@ -1,6 +1,6 @@ { "name": "@hayspec/spec", - "version": "0.5.0", + "version": "0.7.0", "description": "Core logic for Hayspec framework.", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/packages/hayspec-spec/src/core/reporter.ts b/packages/hayspec-spec/src/core/reporter.ts index f3500a1..21e5ecc 100644 --- a/packages/hayspec-spec/src/core/reporter.ts +++ b/packages/hayspec-spec/src/core/reporter.ts @@ -50,7 +50,6 @@ export class Reporter { */ public end() { this.onEnd(); - this.reset(); } /**