Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

Implement preset #1

Merged
merged 3 commits into from
Jan 20, 2017
Merged
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
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
indent_style = space
indent_size = 2
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.nyc_output
coverage
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: node_js
node_js:
- '7'
- '6'
- '4'
cache:
directories:
- $HOME/.npm
- node_modules
before_install: 'npm install --global npm'
before_script: 'npm prune'
script: 'npm run coverage'
after_script: 'cat ./coverage/lcov.info | ./node_modules/.bin/coveralls'
26 changes: 26 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
environment:
matrix:
- nodejs_version: '7'
- nodejs_version: '6'
- nodejs_version: '4'
install:
- ps: Install-Product node $env:nodejs_version
- set CI=true
- set AVA_APPVEYOR=true
- git config core.symlinks true
- git reset --hard
- npm install --global npm
- set PATH=%APPDATA%\npm;%PATH%
- npm install
- npm prune
matrix:
fast_finish: true
build: off
clone_depth: 1
cache:
- node_modules
- '%APPDATA%\npm-cache'
test_script:
- node --version
- npm --version
- npm test
12 changes: 12 additions & 0 deletions espower-patterns.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
"t.truthy(value, [message])",
"t.falsy(value, [message])",
"t.true(value, [message])",
"t.false(value, [message])",
"t.is(value, expected, [message])",
"t.not(value, expected, [message])",
"t.deepEqual(value, expected, [message])",
"t.notDeepEqual(value, expected, [message])",
"t.regex(contents, regex, [message])",
"t.notRegex(contents, regex, [message])"
]
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
const ESPOWER_PATTERNS = require('./espower-patterns.json');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add 'use strict'; above here.


function buildPreset(context, options) {
const plugins = [];
if (!options || options.powerAssert !== false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The option should be documented in the readme.

plugins.push(require('babel-plugin-espower/create')(context, {
embedAst: true,
patterns: ESPOWER_PATTERNS
}));
}
plugins.push(require('babel-plugin-ava-throws-helper'));

return {plugins};
}
module.exports = buildPreset;
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Mark Wubben <[email protected]> (novemberborn.net)

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.
8 changes: 8 additions & 0 deletions package-hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
const packageHash = require('package-hash');

module.exports = packageHash.sync([
require.resolve('./package.json'),
require.resolve('babel-plugin-espower/package.json'),
require.resolve('babel-plugin-ava-throws-helper/package.json')
]);
54 changes: 54 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@ava/babel-preset-transform-test-files",
"version": "0.0.0",
"description": "Test file transforms as used by AVA",
"main": "index.js",
"files": [
"index.js",
"espower-patterns.json",
"package-hash.js"
],
"scripts": {
"test": "ava",
"posttest": "xo",
"coverage": "nyc npm test"
},
"engines": {
"node": ">=4"
},
"repository": {
"type": "git",
"url": "git+https://github.com/avajs/babel-preset-transform-test-files.git"
},
"keywords": [
"ava",
"babel-preset"
],
"author": "Mark Wubben (https://novemberborn.net/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/avajs/babel-preset-transform-test-files/issues"
},
"homepage": "https://github.com/avajs/babel-preset-transform-test-files#readme",
"devDependencies": {
"ava": "^0.17.0",
"babel-core": "^6.21.0",
"coveralls": "^2.11.15",
"empower-core": "^0.6.1",
"nyc": "^10.1.2",
"proxyquire": "^1.7.10",
"xo": "^0.17.1"
},
"dependencies": {
"babel-plugin-ava-throws-helper": "^1.0.0",
"babel-plugin-espower": "^2.3.2",
"package-hash": "^1.2.0"
},
"nyc": {
"reporter": [
"html",
"lcov",
"text"
]
}
}
34 changes: 34 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# @ava/babel-preset-transform-test-files

A [Babel] preset for use with [AVA] test files.

Currently contains:

* [`babel-plugin-espower`](https://github.com/power-assert-js/babel-plugin-espower)
and the patterns that should be enhanced
* [`babel-plugin-ava-throws-helper`](https://github.com/avajs/babel-plugin-ava-throws-helper/)

## Install

```console
$ npm install --save @ava/babel-preset-transform-test-files
```

## Usage

Add `@ava/transform-test-files` to your [Babel] presets. You can disable
`babel-plugin-espower` by setting the `powerAssert` option to `false`:

```json
{
"presets": [
["@ava/transform-test-files", {"powerAsssert": false}]
]
}
```

Require `@ava/babel-preset-transform-test-files/package-hash` to get a combined
hash for the installed version of the preset, as well as the plugins used.

[AVA]: https://ava.li
[Babel]: https://babeljs.io/
88 changes: 88 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {runInNewContext} from 'vm';

import test from 'ava';
import * as babel from 'babel-core';
import throwsHelper from 'babel-plugin-ava-throws-helper';
import empower from 'empower-core';
import proxyquire from 'proxyquire';

import buildPreset from './index';

const ESPOWER_PATTERNS = require('./espower-patterns.json');

test('throws-helper is included', t => {
const {plugins} = buildPreset(babel);
t.true(plugins.indexOf(throwsHelper) > -1);
});

test('resulting preset transforms assertion patterns', t => {
const {code} = babel.transform(`
const value = 'value'
const expected = 'expected'
const contents = 'contents'
const regex = /regex/

// "Execute" the patterns. Hardcode them here, otherwise it's cheating.
t.truthy(value)
t.falsy(value)
t.true(value)
t.false(value)
t.is(value, expected)
t.not(value, expected)
t.deepEqual(value, expected)
t.notDeepEqual(value, expected)
t.regex(contents, regex)
t.notRegex(contents, regex)
`, {
ast: false,
babelrc: false,
presets: [buildPreset]
});

const appliedPatterns = [];
// Create a stub assertion object that can be enhanced using empower-core.
const assert = ESPOWER_PATTERNS
.map(p => /^t\.(.+)\(/.exec(p)[1])
.reduce((assert, name) => {
assert[name] = () => {};
return assert;
}, {});

runInNewContext(code, {
t: empower(assert, {
onSuccess({matcherSpec: {pattern}, powerAssertContext}) {
if (powerAssertContext) { // Only available if the empower plugin transformed the assertion.
appliedPatterns.push(pattern);
}
},
patterns: ESPOWER_PATTERNS
})
});
t.deepEqual(appliedPatterns, ESPOWER_PATTERNS);
});

test('the espower plugin can be disabled', t => {
const expected = 't.true(value);';
const {code} = babel.transform(expected, {
ast: false,
babelrc: false,
presets: [[require.resolve('./'), {powerAssert: false}]]
});
t.true(code === expected);
});

test('computes correct package hash', t => {
t.plan(2);

proxyquire('./package-hash', {
'package-hash': {
sync([preset, ...plugins]) {
t.true(preset === require.resolve('./package.json'));
t.deepEqual(plugins, [
require.resolve('babel-plugin-espower/package.json'),
require.resolve('babel-plugin-ava-throws-helper/package.json')
]);
}
}
});
});