From 85b0edf110a60d0d9dd4443f2b219d8c400c0650 Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 15 Jun 2018 14:02:08 +0100 Subject: [PATCH 1/3] update example --- example/Zoroaster/test/context/index.js | 20 ++++++++++++++----- .../snapshot/{Iran => country-of-origin.txt} | 0 example/Zoroaster/test/spec/async-context.js | 4 ++-- example/Zoroaster/test/spec/methods.js | 8 +++++++- .../Zoroaster/test/spec/multiple-context.js | 13 ++++++------ 5 files changed, 31 insertions(+), 14 deletions(-) rename example/Zoroaster/test/snapshot/{Iran => country-of-origin.txt} (100%) diff --git a/example/Zoroaster/test/context/index.js b/example/Zoroaster/test/context/index.js index 171a30a..f9a7ab3 100644 --- a/example/Zoroaster/test/context/index.js +++ b/example/Zoroaster/test/context/index.js @@ -3,20 +3,30 @@ import { resolve } from 'path' const SNAPSHOT_DIR = resolve(__dirname, '../snapshot') export default class Context { + /** + * An async set-up in which country is acquired. + */ async _init() { - // an async set-up - await new Promise(r => setTimeout(r, 50)) + /** @type {'Iran'} A country of origin */ + const country = await new Promise(r => setTimeout(() => r('Iran'), 50)) + this._country = country } /** * Returns country of origin. */ - async getCountry() { - return 'Iran' + getCountry() { + return this._country } + /** + * An async tear-down in which country is destroyed + */ async _destroy() { - // an async tear-down await new Promise(r => setTimeout(r, 50)) + this._country = null } + /** + * Directory in which to save snapshots. + */ get SNAPSHOT_DIR() { return SNAPSHOT_DIR } diff --git a/example/Zoroaster/test/snapshot/Iran b/example/Zoroaster/test/snapshot/country-of-origin.txt similarity index 100% rename from example/Zoroaster/test/snapshot/Iran rename to example/Zoroaster/test/snapshot/country-of-origin.txt diff --git a/example/Zoroaster/test/spec/async-context.js b/example/Zoroaster/test/spec/async-context.js index 2e559c9..7191f8c 100644 --- a/example/Zoroaster/test/spec/async-context.js +++ b/example/Zoroaster/test/spec/async-context.js @@ -2,12 +2,12 @@ import { equal } from 'assert' import Zoroaster from '../../src' import Context from '../context' -/** @type {Object.} */ +/** @type {Object.} */ const T = { context: Context, async 'returns correct country of origin'({ getCountry }) { const zoroaster = new Zoroaster() - const expected = await getCountry() + const expected = getCountry() equal(zoroaster.countryOfOrigin, expected) }, } diff --git a/example/Zoroaster/test/spec/methods.js b/example/Zoroaster/test/spec/methods.js index 291eaf8..3e07fc6 100644 --- a/example/Zoroaster/test/spec/methods.js +++ b/example/Zoroaster/test/spec/methods.js @@ -56,8 +56,14 @@ export const checkParadise = { const actual = zoroaster.checkParadise() ok(actual) }, - 'returns false when balance is less than 1000'() { + async 'returns false when balance is less than 1000'() { const zoroaster = new Zoroaster() + await Promise.all( + Array.from({ length: 500 }).map(async () => { + await zoroaster.side(Zoroaster.AHURA_MAZDA) + }) + ) + equal(zoroaster.balance, 500) const actual = zoroaster.checkParadise() ok(!actual) }, diff --git a/example/Zoroaster/test/spec/multiple-context.js b/example/Zoroaster/test/spec/multiple-context.js index 74028e0..bc9b2cd 100644 --- a/example/Zoroaster/test/spec/multiple-context.js +++ b/example/Zoroaster/test/spec/multiple-context.js @@ -1,19 +1,20 @@ import Zoroaster from '../../src' import Context from '../context' -import snapshotContext, { SnapshotContext } from 'snapshot-context' // eslint-disable-line no-unused-vars +import SnapshotContext from 'snapshot-context' -/** @type {Object.} */ +/** @type {Object.} */ const T = { context: [ Context, - snapshotContext, + SnapshotContext, ], - async 'returns correct country of origin'({ getCountry, SNAPSHOT_DIR }, { test, setDir }) { + async 'returns correct country of origin'( + { SNAPSHOT_DIR }, { setDir, test } + ) { setDir(SNAPSHOT_DIR) const zoroaster = new Zoroaster() - const expected = await getCountry() const actual = zoroaster.countryOfOrigin - await test(actual, expected) + await test('country-of-origin.txt', actual) }, } From 810f55397225e01a73f33e0bc03ab624bdd77e31 Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 15 Jun 2018 14:10:44 +0100 Subject: [PATCH 2/3] yarn example --- package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/package.json b/package.json index 75ae361..0b91ea7 100644 --- a/package.json +++ b/package.json @@ -5,12 +5,14 @@ "main": "build", "scripts": { "t": "node src/bin -b", + "tw": "node src/bin -b -w", "test": "yarn t test/spec", "test-build": "BABEL_ENV=test-build yarn t test/spec", "test-all": "yarn-s test test-build", "build": "babel src --out-dir build --ignore src/bin/index.js", "lint": "eslint .", "example/simple.js": "node src/bin example/simple.js -b", + "example/": "yarn example/Zoroaster/", "example/Zoroaster/": "node src/bin example/Zoroaster/test/spec -b" }, "files": [ From 1090e1087f7fafcf28f536df843fff71b97740e4 Mon Sep 17 00:00:00 2001 From: Anton Date: Fri, 15 Jun 2018 14:12:48 +0100 Subject: [PATCH 3/3] readme --- .vscode/launch.json | 2 +- README.md | 61 ++++++++++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 4a58efa..09b911e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "name": "Launch Zoroaster", "program": "${workspaceFolder}/src/bin/index.js", "args": [ - "test/spec/Context/constructors/TestSuite.js", + "example/Zoroaster/test/spec/multiple-context.js", "-b", "-w" ], diff --git a/README.md b/README.md index da98960..25ff302 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Zoroaster +# zoroaster [![npm version](https://badge.fury.io/js/zoroaster.svg)](https://badge.fury.io/js/zoroaster) [![Build Status](https://travis-ci.org/artdecocode/zoroaster.svg?branch=master)](https://travis-ci.org/artdecocode/zoroaster) @@ -387,7 +387,7 @@ yarn add -E -D \ @babel/core \ @babel/register \ @babel/plugin-syntax-object-rest-spread \ -@babel/plugin-transform-modules-commonjs \ +@babel/plugin-transform-modules-commonjs ``` When building the project, you're probably using `@babel/cli` as well. @@ -401,7 +401,6 @@ To be able to run `yarn test`, specify the test script in the `package.json` as "name": "test-package", "scripts": { "test": "zoroaster test/spec" - } } ``` @@ -414,7 +413,7 @@ Additional shorter scripts for `yarn` can be specified (`-b` is to require `@bab "t": "zoroaster -b", "tw": "zoroaster -b -w", "test": "yarn t test/spec", - "test-watch": "yarn test -w", + "test-watch": "yarn tw test/spec", } } ``` @@ -468,23 +467,35 @@ Context can be a class, and to initialise it, `_init` function will be called if ```js import { resolve } from 'path' +const SNAPSHOT_DIR = resolve(__dirname, '../snapshot') + export default class Context { + /** + * An async set-up in which country is acquired. + */ async _init() { - // an async set-up - await new Promise(r => setTimeout(r, 50)) + /** @type {'Iran'} A country of origin */ + const country = await new Promise(r => setTimeout(() => r('Iran'), 50)) + this._country = country } /** * Returns country of origin. */ - async getCountry() { - return 'Iran' + getCountry() { + return this._country } + /** + * An async tear-down in which country is destroyed + */ async _destroy() { - // an async tear-down await new Promise(r => setTimeout(r, 50)) + this._country = null } + /** + * Directory in which to save snapshots. + */ get SNAPSHOT_DIR() { - return resolve(__dirname, '../snapshot') + return SNAPSHOT_DIR } } ``` @@ -509,34 +520,35 @@ export default T ### Multiple Contexts -It is possible to specify multiple contexts by passing an array to the `context` property. +It is possible to specify multiple contexts by passing an array to the `context` property. Passing a string or anything else than `null` will also work. ```js import Zoroaster from '../../src' import Context from '../context' import SnapshotContext from 'snapshot-context' -import { resolve } from 'path' - -const SNAPSHOT_DIR = resolve(__dirname, '../snapshot') /** @type {Object.} */ const T = { context: [ - context, - snapshotContext, + Context, + SnapshotContext, ], - async 'returns correct country of origin'({ getCountry }, { test, setDir }) { + async 'returns correct country of origin'( + { SNAPSHOT_DIR }, + { setDir, test } + ) { setDir(SNAPSHOT_DIR) const zoroaster = new Zoroaster() - const expected = await getCountry() const actual = zoroaster.countryOfOrigin - await test(actual, expected) + await test('country-of-origin.txt', actual) }, } export default T ``` + + ### Function Context (deprecated as of 2.1) > THIS SHOULD NOT REALLY BE USED AS OF `2.1` WHICH INTRODUCED THE CLASS CONTEXT FEATURE BECAUSE IT'S EASIER TO DOCUMENT A CLASS WITHOUT HAVING TO HACK A DOCTYPE. @@ -619,16 +631,18 @@ See [`assert-throws` API documentation][5] to learn more about assertions. ## launch.json -The following snippet can be used when debugging tests. +The following snippet can be used when debugging tests. Because `-w` argument is also passed, the tests will automatically restart and repause at the breakpoints. ```json { "type": "node", "request": "launch", - "name": "Launch Program", - "program": "${workspaceFolder}/bin/zoroaster", + "name": "Launch Zoroaster", + "program": "${workspaceFolder}/.bin/zoroaster", "args": [ - "test/spec/integration.js" + "test/spec/integration.js", + "-b", + "-w" ], "env": { "ZOROASTER_TIMEOUT": "9999999" @@ -654,6 +668,7 @@ The following snippet can be used when debugging tests. 10. ~~Context object as an optional argument to test functions~~ 11. Pass path to a context file in CLI 12. Catch global errors +13. Make sure source maps are updated as well when running `-w` and `-b` mode to show the correct line. ### context-related todo