-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
test.js
162 lines (131 loc) · 3.98 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* eslint-env mocha */
'use strict';
require('chai').should();
const Plugin = require('.');
describe('babel-brunch', function suite() {
let plugin;
this.timeout(10000);
beforeEach(() => {
plugin = new Plugin({paths: {root: '.'}});
});
it('should have #compile method', () => {
plugin.should.respondTo('compile');
});
it('should do nothing for no preset', () => {
const content = 'var c = {};\nvar {\n a,\n b\n} = c;\n';
plugin = new Plugin({
paths: {root: '.'},
plugins: {
babel: {presets: []},
},
});
return plugin.compile({data: content, path: 'file.js'})
.then(result => result.data.should.contain(content));
});
it('should compile and produce valid result', () => {
const content = 'var c = {};\nvar {a, b} = c;';
const expected = 'var a = c.a,\n b = c.b;';
return plugin.compile({data: content, path: 'file.js'})
.then(result => result.data.should.contain(expected));
});
it('should load indicated presets', () => {
const content = 'x => x';
const expected = 'function';
plugin = new Plugin({
paths: {root: '.'},
plugins: {
babel: {presets: ['@babel/env']},
},
});
return plugin.compile({data: content, path: 'file.js'})
.then(result => result.data.should.contain(expected));
});
it('should load indicated presets with options', () => {
const content = 'export default 0';
const expected = 'System.register';
plugin = new Plugin({
paths: {root: '.'},
plugins: {
babel: {
presets: [
['@babel/env', {
modules: 'systemjs',
}],
],
},
},
});
return plugin.compile({data: content, path: 'file.js'})
.then(result => result.data.should.contain(expected));
});
it('should load indicated plugins', () => {
const content = 'var c = () => process.env.NODE_ENV;';
const expected = 'var c = () => undefined;\n';
plugin = new Plugin({
paths: {root: '.'},
plugins: {
babel: {
plugins: ['babel-plugin-transform-node-env-inline'],
},
},
});
return plugin.compile({data: content, path: 'file.js'})
.then(result => result.data.should.contain(expected));
});
it('should load indicated plugins with options', () => {
const content = '`var x = 1; test ${x}`';
const expected = '.concat(x)';
plugin = new Plugin({
paths: {root: '.'},
plugins: {
babel: {
plugins: [['@babel/transform-template-literals', {spec: true}]],
},
},
});
return plugin.compile({data: content, path: 'file.js'})
.then(result => result.data.should.contain(expected));
});
describe('custom file extensions & patterns', () => {
const content = 'let a = 1';
const path = 'file.es6';
const basicPlugin = new Plugin({
paths: {root: '.'},
plugins: {
babel: {
pattern: /\.(babel|es6|jsx)$/,
},
},
});
const sourceMapPlugin = new Plugin({
paths: {root: '.'},
sourceMaps: true,
plugins: {
babel: {
pattern: /\.(babel|es6|jsx)$/,
},
},
});
it('should handle custom file extensions', () =>
basicPlugin.compile({data: content, path})
);
it('should properly link to source file in source maps', () =>
sourceMapPlugin.compile({data: content, path})
.then(result => JSON.parse(result.map).sources.should.contain(path))
);
});
it('should produce source maps', () => {
const content = 'let a = 1';
plugin = new Plugin({
paths: {root: '.'},
sourceMaps: true,
});
return plugin.compile({data: content, path: 'file.js'})
.then(result => JSON.parse(result.map));
});
it('should pass through content of ignored paths', () => {
const content = 'asdf';
return plugin.compile({data: content, path: 'vendor/file.js'})
.then(result => result.data.should.be.equal(content));
});
});