This repository has been archived by the owner on Mar 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
58 lines (51 loc) · 1.55 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {runInNewContext} from 'vm';
import test from 'ava';
import * as babel from '@babel/core';
import throwsHelper from '@ava/babel-plugin-throws-helper';
import empower from 'empower-core';
import buildPreset from '.';
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'
// "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])
.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('./'), {powerAssert: false}]]
});
t.is(code, expected);
});