Skip to content

Commit

Permalink
Inherit @ava/babel-preset-transform-test-files implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed Jan 5, 2020
1 parent b307b2a commit 105a62f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

Translations: [Français](https://github.com/avajs/ava-docs/tree/master/fr_FR/related/babel/README.md)

Adds [Babel 7](https://babeljs.io) support to [AVA](https://avajs.dev) so you can use the latest JavaScript syntax in your tests. We do this by compiling test and helper files using our `@ava/babel/stage-4` preset. We also use a [second preset, `@ava/transform-test-files`](https://github.com/avajs/babel-preset-transform-test-files) to enable [enhanced assertion messages](https://github.com/avajs/ava/blob/master/docs/03-assertions.md#enhanced-assertion-messages) and detect improper use of `t.throws()` assertions.
Adds [Babel 7](https://babeljs.io) support to [AVA](https://avajs.dev) so you can use the latest JavaScript syntax in your tests. We do this by compiling test and helper files using our `@ava/babel/stage-4` preset. We also use a second preset, `@ava/babel/transform-test-files` to enable [enhanced assertion messages](https://github.com/avajs/ava/blob/master/docs/03-assertions.md#enhanced-assertion-messages) and detect improper use of `t.throws()` assertions.

By default our Babel pipeline is applied to test and helper files ending in `.cjs` and `.js`. If your project uses Babel then we'll automatically compile these files using your project's Babel configuration. The `@ava/transform-helper-files` preset is applied first, and the `@ava/babel/stage-4` last.
By default our Babel pipeline is applied to test and helper files ending in `.cjs` and `.js`. If your project uses Babel then we'll automatically compile these files using your project's Babel configuration. The `@ava/babel/transform-helper-files` preset is applied first, and the `@ava/babel/stage-4` last.

If you are using Babel for your source files then you must also [configure source compilation](#compile-sources).

Expand Down
3 changes: 3 additions & 0 deletions espower-patterns.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"t.assert(value, [message])"
]
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function createCompileFn({babelOptions, cacheDir, compileEnhancements, projectDi

const ensureStage4 = wantsStage4(babelOptions, projectDir);
const containsStage4 = makeValueChecker('./stage-4');
const containsTransformTestFiles = makeValueChecker('@ava/babel-preset-transform-test-files');
const containsTransformTestFiles = makeValueChecker('./transform-test-files');

const loadOptions = filename => {
const partialConfig = babel.loadPartialConfig({
Expand Down Expand Up @@ -193,7 +193,7 @@ function createCompileFn({babelOptions, cacheDir, compileEnhancements, projectDi

if (compileEnhancements && !partialOptions.presets.some(containsTransformTestFiles)) {
// Apply first.
partialOptions.presets.push(createConfigItem('@ava/babel-preset-transform-test-files', 'preset', {powerAssert: true}));
partialOptions.presets.push(createConfigItem('./transform-test-files', 'preset', {powerAssert: true}));
}

const hash = hashPartialConfig(partialConfig, projectDir, pluginAndPresetHashes);
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"node": ">=10.18.0 <11 || >=12.14.0 <13 || >=13.5.0"
},
"files": [
"espower-patterns.json",
"index.js",
"stage-4.js",
"transform-test-files.js",
"throws-helper.js",
"stage-4-plugins"
],
Expand All @@ -22,14 +24,14 @@
"test": "xo && nyc ava"
},
"dependencies": {
"@ava/babel-preset-transform-test-files": "^6.0.0",
"@babel/core": "^7.7.7",
"@babel/generator": "^7.7.7",
"@babel/plugin-proposal-async-generator-functions": "^7.7.4",
"@babel/plugin-proposal-dynamic-import": "^7.7.4",
"@babel/plugin-proposal-optional-catch-binding": "^7.7.4",
"@babel/plugin-transform-dotall-regex": "^7.7.7",
"@babel/plugin-transform-modules-commonjs": "^7.7.5",
"babel-plugin-espower": "^3.0.1",
"concordance": "^4.0.0",
"convert-source-map": "^1.7.0",
"dot-prop": "^5.2.0",
Expand Down
58 changes: 58 additions & 0 deletions test/transform-test-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const {runInNewContext} = require('vm');
const test = require('ava');
const babel = require('@babel/core');
const empower = require('empower-core');
const throwsHelper = require('../throws-helper');
const buildPreset = require('../transform-test-files');

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

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

test('resulting preset transforms assertion patterns', t => {
const {code} = babel.transform(`
const value = 'value'
// "Execute" the patterns. Hardcode them here, otherwise it's cheating.
t.assert(value)
`, {
ast: false,
babelrc: false,
filename: __filename,
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]) // eslint-disable-line prefer-named-capture-group
.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.assert(value);';
const {code} = babel.transform(expected, {
ast: false,
babelrc: false,
presets: [[require.resolve('../transform-test-files'), {powerAssert: false}]]
});
t.is(code, expected);
});
17 changes: 17 additions & 0 deletions transform-test-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
const ESPOWER_PATTERNS = require('./espower-patterns.json');

module.exports = (context, options) => {
const plugins = [];

if (!options || options.powerAssert !== false) {
plugins.push([require('babel-plugin-espower'), {
embedAst: true,
patterns: ESPOWER_PATTERNS
}]);
}

plugins.push(require('./throws-helper'));

return {plugins};
};

0 comments on commit 105a62f

Please sign in to comment.