forked from emberjs/ember-cli-babel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-babel-options-test.js
199 lines (178 loc) · 5.25 KB
/
get-babel-options-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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const expect = require("chai").expect;
const MockUI = require("console-ui/mock");
const CoreObject = require("core-object");
const AddonMixin = require("../index");
let {
_addTypeScriptPlugin,
_getAddonProvidedConfig,
_addDecoratorPlugins,
} = require("../lib/babel-options-util");
let Addon = CoreObject.extend(AddonMixin);
describe("get-babel-options", function () {
const ORIGINAL_EMBER_ENV = process.env.EMBER_ENV;
beforeEach(function () {
this.ui = new MockUI();
let project = {
isEmberCLIProject: () => true,
_addonsInitialized: true,
root: __dirname,
emberCLIVersion: () => "2.16.2",
dependencies() {
return {};
},
addons: [],
targets: {
browsers: ["ie 11"],
},
};
this.addon = new Addon({
project,
parent: project,
ui: this.ui,
});
project.addons.push(this.addon);
});
afterEach(function () {
if (ORIGINAL_EMBER_ENV === undefined) {
delete process.env.EMBER_ENV;
} else {
process.env.EMBER_ENV = ORIGINAL_EMBER_ENV;
}
});
describe("_addTypeScriptPlugin", function () {
it("should warn and not add the TypeScript plugin if already added", function () {
this.addon.project.ui = {
writeWarnLine(message) {
expect(message).to.match(
/has added the TypeScript transform plugin to its build/
);
},
};
expect(
_addTypeScriptPlugin(
[["@babel/plugin-transform-typescript"]],
{},
this.addon.parent,
this.addon.project
).length
).to.equal(1, "plugin was not added");
});
});
describe("_addDecoratorPlugins", function () {
it("should include babel transforms by default", function () {
expect(
_addDecoratorPlugins([], {}, {}, this.addon.parent, this.addon.project)
.length
).to.equal(4, "plugins added correctly");
});
it("should include only fields if it detects decorators plugin", function () {
this.addon.project.ui = {
writeWarnLine(message) {
expect(message).to.match(
/has added the decorators plugin to its build/
);
},
};
expect(
_addDecoratorPlugins(
[["@babel/plugin-proposal-decorators"]],
{},
{},
this.addon.parent,
this.addon.project
).length
).to.equal(4, "plugins were not added");
});
it("should include only decorators if it detects class fields plugin", function () {
this.addon.project.ui = {
writeWarnLine(message) {
expect(message).to.match(
/has added the class-properties plugin to its build/
);
},
};
expect(
_addDecoratorPlugins(
[["@babel/plugin-proposal-class-properties"]],
{},
{},
this.addon.parent,
this.addon.project
).length
).to.equal(2, "plugins were not added");
});
it("should use babel options loose mode for class properties", function () {
let strictPlugins = _addDecoratorPlugins(
[],
{},
{},
this.addon.parent,
this.addon.project
);
expect(strictPlugins[strictPlugins.length - 1][1].loose).to.equal(
false,
"loose is false if no option is provided"
);
let loosePlugins = _addDecoratorPlugins(
[],
{ loose: true },
{},
this.addon.parent,
this.addon.project
);
expect(loosePlugins[loosePlugins.length - 1][1].loose).to.equal(
true,
"loose setting added correctly"
);
});
it("should include class fields and decorators after typescript if handling typescript", function () {
const config = {
"ember-cli-babel": { enableTypeScriptTransform: true },
};
let plugins = _addDecoratorPlugins(
["@babel/plugin-transform-typescript"],
{},
config,
this.addon.parent,
this.addon.project
);
expect(plugins[0]).to.equal(
"@babel/plugin-transform-typescript",
"typescript still first"
);
expect(plugins.length).to.equal(5, "class fields and decorators added");
});
it("should include class fields and decorators before typescript if not handling typescript", function () {
const config = {
"ember-cli-babel": { enableTypeScriptTransform: false },
};
let plugins = _addDecoratorPlugins(
["@babel/plugin-transform-typescript"],
{},
config,
this.addon.parent,
this.addon.project
);
expect(plugins.length).to.equal(5, "class fields and decorators added");
expect(plugins[4]).to.equal(
"@babel/plugin-transform-typescript",
"typescript is now last"
);
});
});
describe("_getAddonProvidedConfig", function () {
it("does not mutate addonOptions.babel", function () {
let babelOptions = { blah: true };
this.addon.parent = {
dependencies() {
return {};
},
options: {
babel: babelOptions,
},
};
let result = _getAddonProvidedConfig(this.addon._getAddonOptions());
expect(result.options).to.not.equal(babelOptions);
});
});
});