Skip to content

Commit

Permalink
feat: (power) assertion blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal committed Nov 29, 2017
0 parents commit 457f516
Show file tree
Hide file tree
Showing 22 changed files with 5,195 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": 6
}
}
],
"@babel/preset-typescript"
]
}
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]
indent_style = space
indent_size = 2

end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# node
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# generated folders
coverage/
dist/
tmp/

# editors / IDEs
.idea/
.vscode/
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: node_js
node_js:
- "node"
- "6"

cache: yarn

script:
- yarn run lint
- yarn run typecheck
- yarn run build
- yarn run test-once

before_install: yarn global add greenkeeper-lockfile@1
before_script: greenkeeper-lockfile-update
after_script: greenkeeper-lockfile-upload

after_success:
- yarn global add codecov && codecov
60 changes: 60 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Contributing

We use [yarn](https://yarnpkg.org) as our package manager. Run `yarn` to install
the dependencies.

## Note: Current babel version situation

At the moment, we need both babel 6 (peer/dev) and babel 7 beta (dev) installed
in the project directory.

Babel 6 is used by production code and tests, while babel 7 is used to compile
the production code and tests. The former cannot be upgraded because
`babel-plugin-espower` does not yet support babel 7, the latter cannot be
downgraded because we need `@babel/preset-typescript`, which is 7+ only.

Due to babel's change from regular to scoped packages, this is generally not a
problem, because the production code and tests can just import the regular
packages with version 6, while our `.babelrc` uses the scoped packages with
version 7.

However, `babel-jest` currently requires `babel-core@7.0.0-bridge.0` to be
installed in order to work with babel 7, so for `babel-core` we use yarn's alias
feature to install `babel-core@^6.0.0` as `babel-core-old` and use that module
name for imports in our production code and tests, while the bridge package for
Jest is installed under its regular name.

This workaround also makes yarn absolutely mandatory for hacking on this package
at the moment.

## Scripts

* `yarn all` - run all checks and build
* `yarn lint` - lint the code using
[tslint](https://palantir.github.io/tslint/)
* `yarn typecheck` - typecheck the code using the
[TypeScript](https://www.typescriptlang.org/) compiler
* `yarn test-once` - perform a single [Jest](https://facebook.github.io/jest/)
test run
* `yarn build` - assemble the build output in the dist folder
* `yarn test` - run the tests in watch mode and show the coverage stats in the
browser
* `yarn fix` - fix all auto-fixable issues and format the code
* `yarn clean` - removes generated distribution files and coverage stats

## Commits

We follow the
[Angular commit message conventions](https://github.com/angular/angular/blob/master/CONTRIBUTING.md).
This is also important for automatic generation of changelogs.

## Releasing

1. Check that CI ran successfully
2. Update the `CHANGELOG.md` using
[standard-version](https://github.com/conventional-changelog/standard-version)
3. `yarn clean && yarn all`
4. `cd dist`
5. Remove the `private: true` property from the `package.json` file
6. `npm publish`
7. `git push && git push --tags`
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Tim Seckinger

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
151 changes: 151 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# babel-plugin-spock

> Structured JS test cases, inspired by
> [Spock Framework](http://spockframework.org)
[![build status](https://img.shields.io/travis/jeysal/babel-plugin-spock/master.svg?style=flat-square)](https://travis-ci.org/jeysal/babel-plugin-spock)
[![AppVeyor build status](https://img.shields.io/appveyor/ci/jeysal/babel-plugin-spock/master.svg?style=flat-square&label=windows+build)](https://ci.appveyor.com/project/jeysal/babel-plugin-spock)
[![code coverage](https://img.shields.io/codecov/c/github/jeysal/babel-plugin-spock/master.svg?style=flat-square)](https://codecov.io/gh/jeysal/babel-plugin-spock)

[![npm package](https://img.shields.io/npm/v/babel-plugin-spock.svg?style=flat-square)](https://www.npmjs.com/package/babel-plugin-spock)
[![license](https://img.shields.io/github/license/jeysal/babel-plugin-spock.svg?style=flat-square)](https://github.com/jeysal/babel-plugin-spock/blob/master/LICENSE)

**Note:** This module is in an early stage of development and currently provides
only a small set of features.

## Example test case

```javascript
test('basic arithmetic', () => {
expect: {
1 + 2 === 3;
3 * 3 >= 4 * 4; // falsy
}
});
```

```
Expected value to be (operator: ==):
true
Received:
false
assert(3 * 3 >= 4 * 4)
| | |
| | 16
9 false
```

## Installation

npm install --save-dev babel-plugin-spock power-assert

This module is a [Babel](https://babeljs.io/) plugin that needs to transform
your test sources in order to generate assertions, so your test runner will need
support for Babel. Babel integrates quite nicely into most modern test runners.
Check the documentation of your test runner for instructions on how to configure
Babel (e.g. for
[Jest](https://facebook.github.io/jest/docs/en/getting-started.html#using-babel),
[AVA](https://github.com/avajs/ava#es2017-support) etc.) or consult Babel's own
[documentation](http://babeljs.io/docs/setup/).

Once Babel is set up for your test files, simply add `"babel-plugin-spock"` to
the `plugins` array in your babel configuration and you're good to go.

**Note:** The Babel 7 beta is not yet supported, but may work if you disable
power assertions (see [Configuration](#Configuration)).

## Usage

### Assertion blocks

**Note:** At the moment, your test files need to `import assert from
'power-assert';` for this to work. In the future, the plugin will be able to
handle this for you automatically.

Inside of a block labeled with `expect:` or `then:`, all statements will be
considered assertions and evaluated to check for truth:

```javascript
expect: {
1 < 2;
}
```

`when`-`then` blocks can be particularly useful and expressive for code with
side effects:

```javascript
// The 'when' label here does not have a special meaning
// It is used simply to make the test more structured
when: {
abc.setXyz(1);
}

then: {
abc.getXyz() === 1;
}
```

Single labeled statements are also possible:

```javascript
expect: 'a' + 'b' === 'ab';
```

Note that these blocks can only contain statements that can be evaluated as
expressions. For example, an if statement would not be valid:

```javascript
// BAD
expect: {
if (x < 1) x === 0.5;
else x === 2;
}
```

However, you can nest an assertion block into other structures:

```javascript
// GOOD
if (x < 1) expect: x === 0.5;
else expect: x === 2;
```

If you want to perform more complicated checks, it might be helpful to look for
small helper libraries on npm. For example,
[deep-strict-equal](https://github.com/sindresorhus/deep-strict-equal) can help
perform deep equality checks on object structures. In the future, this plugin
might provide special syntax for such use cases.

### Linters

The test you will write using this plugin often employ syntax that is otherwise
uncommon in JavaScript code. For this reason, if you use a linter such as
[ESLint](https://eslint.org/), you will likely see annoying warnings all over
your tests. To work around this, most linters will give you multiple options:

* Disable the problematic rules with special annotations in your test files.
This can be a hassle because it needs to be done for every file.
* Completely disable the rules in the configuration. This means that they will
no longer apply to production code either.
* Create a separate config for tests that extends the base config, but disables
the rules.

## Configuration

You can configure this plugin using Babel's regular
[plugin configuration mechanism](https://babeljs.io/docs/plugins/#plugin-preset-options).
The following options are available:

**`powerAssert`**

The plugin can seamlessly generate assertions that produce detailed mismatch
messages to help you figure out what exactly about the assertion went wrong.
Turning this feature off could be useful if you're tests appear to run slowly or
you are experiencing other issues with your assertions.

This feature is powered by the awesome project
[power-assert](https://github.com/power-assert-js/power-assert).

default: `true`
20 changes: 20 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
environment:
matrix:
- nodejs_version: "Stable"
- nodejs_version: "6"

install:
- ps: Install-Product node $env:nodejs_version x64
- yarn

test_script:
- node --version
- yarn --version
- yarn run lint
- yarn run typecheck
- yarn run build
- yarn run test-once

build: off
shallow_clone: true
clone_depth: 1
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
coverageDirectory: 'coverage',
testEnvironment: 'node',
moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json'],
testRegex: '.*\\.(test|spec)\\.(j|t)sx?$',
transform: {
'^.+\\.(j|t)sx?$': 'babel-jest',
},
};

0 comments on commit 457f516

Please sign in to comment.