Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down
61 changes: 38 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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"

}
}
```
Expand All @@ -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",
}
}
```
Expand Down Expand Up @@ -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
}
}
```
Expand All @@ -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.<string, (c: Context, s: SnapshotContext)>} */
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
```

<!-- [![multiple-context](doc/multiple-context.gif)](https://artdecocode.bz) -->

### 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.
Expand Down Expand Up @@ -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"
Expand All @@ -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

Expand Down
20 changes: 15 additions & 5 deletions example/Zoroaster/test/context/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions example/Zoroaster/test/spec/async-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { equal } from 'assert'
import Zoroaster from '../../src'
import Context from '../context'

/** @type {Object.<string, (ctx: Context)>} */
/** @type {Object.<string, (c: Context)>} */
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)
},
}
Expand Down
8 changes: 7 additions & 1 deletion example/Zoroaster/test/spec/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
13 changes: 7 additions & 6 deletions example/Zoroaster/test/spec/multiple-context.js
Original file line number Diff line number Diff line change
@@ -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.<string, (ctx: Context, snapshotCtx: SnapshotContext)>} */
/** @type {Object.<string, (c: Context, s: SnapshotContext)>} */
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)
},
}

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down