-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inherit @ava/babel-preset-transform-test-files implementation
- Loading branch information
1 parent
b307b2a
commit 105a62f
Showing
6 changed files
with
85 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
"t.assert(value, [message])" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
}; |