Skip to content

Commit f760e52

Browse files
committed
Remove some tests related to the public API proxy and improve some texts/comments
1 parent ce6e7d3 commit f760e52

File tree

2 files changed

+40
-139
lines changed

2 files changed

+40
-139
lines changed

index.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ const parseRuntime = require('./lib/config/parse-runtime');
1919
let webpackConfig = null;
2020
let runtimeConfig = require('./lib/context').runtimeConfig;
2121

22-
// If runtimeConfig is already set webpackConfig can directly
23-
// be initialized here.
24-
if (runtimeConfig) {
25-
webpackConfig = new WebpackConfig(runtimeConfig);
22+
function initializeWebpackConfig() {
2623
if (runtimeConfig.verbose) {
2724
logger.verbose();
2825
}
26+
27+
webpackConfig = new WebpackConfig(runtimeConfig);
28+
}
29+
30+
// If runtimeConfig is already set webpackConfig can directly
31+
// be initialized here.
32+
if (runtimeConfig) {
33+
initializeWebpackConfig();
2934
}
3035

3136
const publicApi = {
@@ -453,8 +458,9 @@ const publicApi = {
453458
/**
454459
* Initialize the runtime environment.
455460
*
456-
* It can be used to directly manipulate the Encore API without
457-
* executing the "./node_module/.bin/encore" utility.
461+
* This can be used to configure the Encore runtime if you're
462+
* using Encore without executing the "./node_module/.bin/encore"
463+
* utility (e.g. with karma-webpack).
458464
*
459465
* Encore.configureRuntimeEnvironment(
460466
* // Environment to use (dev, dev-server, production)
@@ -486,11 +492,7 @@ const publicApi = {
486492
process.cwd()
487493
);
488494

489-
if (runtimeConfig.verbose) {
490-
logger.verbose();
491-
}
492-
493-
webpackConfig = new WebpackConfig(runtimeConfig);
495+
initializeWebpackConfig();
494496

495497
return this;
496498
},
@@ -522,24 +524,26 @@ const publicApiProxy = new Proxy(publicApi, {
522524
];
523525

524526
if (!webpackConfig && (safeMethods.indexOf(prop) === -1)) {
525-
throw new Error(`Encore.${prop}() cannot be called yet because the runtime environment doesn't appear to be configured. Try calling Encore.configureRuntimeEnvironment() first.`);
526-
} else {
527-
// Either a safe method has been called or the webpackConfig
528-
// object is already available. In this case act as a passthrough.
529-
return (...parameters) => {
530-
try {
531-
const res = target[prop](...parameters);
532-
return (res === target) ? publicApiProxy : res;
533-
} catch (error) {
534-
// prettifies errors thrown by our library
535-
const pe = new PrettyError();
536-
537-
console.log(pe.render(error));
538-
process.exit(1); // eslint-disable-line
539-
}
540-
};
527+
throw new Error(`Encore.${prop}() cannot be called yet because the runtime environment doesn't appear to be configured. Make sure you're using the encore executable or call Encore.configureRuntimeEnvironment() first if you're purposely not calling Encore directly.`);
541528
}
529+
530+
// Either a safe method has been called or the webpackConfig
531+
// object is already available. In this case act as a passthrough.
532+
return (...parameters) => {
533+
try {
534+
const res = target[prop](...parameters);
535+
return (res === target) ? publicApiProxy : res;
536+
} catch (error) {
537+
// prettifies errors thrown by our library
538+
const pe = new PrettyError();
539+
540+
console.log(pe.render(error));
541+
process.exit(1); // eslint-disable-line
542+
}
543+
};
542544
}
545+
546+
return target[prop];
543547
}
544548
});
545549

test/index.js

Lines changed: 9 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,14 @@
1212
const expect = require('chai').expect;
1313
const api = require('../index');
1414

15-
function configureApi() {
16-
return api.configureRuntimeEnvironment('dev');
17-
}
18-
1915
describe('Public API', () => {
2016
beforeEach(() => {
21-
api.clearRuntimeEnvironment();
17+
api.configureRuntimeEnvironment('dev');
2218
});
2319

2420
describe('setOutputPath', () => {
2521

26-
it('should not be callable before the runtime environment has been configured', () => {
27-
expect(() => api.setOutputPath('/')).to.throw();
28-
});
29-
3022
it('must return the API object', () => {
31-
configureApi();
3223
const returnedValue = api.setOutputPath('/');
3324
expect(returnedValue).to.equal(api);
3425
});
@@ -37,12 +28,7 @@ describe('Public API', () => {
3728

3829
describe('setPublicPath', () => {
3930

40-
it('should not be callable before the runtime environment has been configured', () => {
41-
expect(() => api.setPublicPath('/')).to.throw();
42-
});
43-
4431
it('must return the API object', () => {
45-
configureApi();
4632
const returnedValue = api.setPublicPath('/');
4733
expect(returnedValue).to.equal(api);
4834
});
@@ -51,12 +37,7 @@ describe('Public API', () => {
5137

5238
describe('setManifestKeyPrefix', () => {
5339

54-
it('should not be callable before the runtime environment has been configured', () => {
55-
expect(() => api.setManifestKeyPrefix('/build')).to.throw();
56-
});
57-
5840
it('must return the API object', () => {
59-
configureApi();
6041
const returnedValue = api.setManifestKeyPrefix('/build');
6142
expect(returnedValue).to.equal(api);
6243
});
@@ -65,12 +46,7 @@ describe('Public API', () => {
6546

6647
describe('addEntry', () => {
6748

68-
it('should not be callable before the runtime environment has been configured', () => {
69-
expect(() => api.addEntry('entry', 'main.js')).to.throw();
70-
});
71-
7249
it('must return the API object', () => {
73-
configureApi();
7450
const returnedValue = api.addEntry('entry', 'main.js');
7551
expect(returnedValue).to.equal(api);
7652
});
@@ -79,12 +55,7 @@ describe('Public API', () => {
7955

8056
describe('addStyleEntry', () => {
8157

82-
it('should not be callable before the runtime environment has been configured', () => {
83-
expect(() => api.addStyleEntry('styleEntry', 'main.css')).to.throw();
84-
});
85-
8658
it('must return the API object', () => {
87-
configureApi();
8859
const returnedValue = api.addStyleEntry('styleEntry', 'main.css');
8960
expect(returnedValue).to.equal(api);
9061
});
@@ -93,12 +64,7 @@ describe('Public API', () => {
9364

9465
describe('addPlugin', () => {
9566

96-
it('should not be callable before the runtime environment has been configured', () => {
97-
expect(() => api.addPlugin(null)).to.throw();
98-
});
99-
10067
it('must return the API object', () => {
101-
configureApi();
10268
const returnedValue = api.addPlugin(null);
10369
expect(returnedValue).to.equal(api);
10470
});
@@ -107,12 +73,7 @@ describe('Public API', () => {
10773

10874
describe('addLoader', () => {
10975

110-
it('should not be callable before the runtime environment has been configured', () => {
111-
expect(() => api.addLoader(null)).to.throw();
112-
});
113-
11476
it('must return the API object', () => {
115-
configureApi();
11677
const returnedValue = api.addLoader(null);
11778
expect(returnedValue).to.equal(api);
11879
});
@@ -121,12 +82,7 @@ describe('Public API', () => {
12182

12283
describe('addRule', () => {
12384

124-
it('should not be callable before the runtime environment has been configured', () => {
125-
expect(() => api.addRule(null)).to.throw();
126-
});
127-
12885
it('must return the API object', () => {
129-
configureApi();
13086
const returnedValue = api.addRule(null);
13187
expect(returnedValue).to.equal(api);
13288
});
@@ -135,12 +91,7 @@ describe('Public API', () => {
13591

13692
describe('enableVersioning', () => {
13793

138-
it('should not be callable before the runtime environment has been configured', () => {
139-
expect(() => api.enableVersioning()).to.throw();
140-
});
141-
14294
it('must return the API object', () => {
143-
configureApi();
14495
const returnedValue = api.enableVersioning();
14596
expect(returnedValue).to.equal(api);
14697
});
@@ -149,12 +100,7 @@ describe('Public API', () => {
149100

150101
describe('enableSourceMaps', () => {
151102

152-
it('should not be callable before the runtime environment has been configured', () => {
153-
expect(() => api.enableSourceMaps()).to.throw();
154-
});
155-
156103
it('must return the API object', () => {
157-
configureApi();
158104
const returnedValue = api.enableSourceMaps();
159105
expect(returnedValue).to.equal(api);
160106
});
@@ -163,12 +109,7 @@ describe('Public API', () => {
163109

164110
describe('createSharedEntry', () => {
165111

166-
it('should not be callable before the runtime environment has been configured', () => {
167-
expect(() => api.createSharedEntry('sharedEntry', 'vendor.js')).to.throw();
168-
});
169-
170112
it('must return the API object', () => {
171-
configureApi();
172113
const returnedValue = api.createSharedEntry('sharedEntry', 'vendor.js');
173114
expect(returnedValue).to.equal(api);
174115
});
@@ -177,12 +118,7 @@ describe('Public API', () => {
177118

178119
describe('autoProvideVariables', () => {
179120

180-
it('should not be callable before the runtime environment has been configured', () => {
181-
expect(() => api.autoProvideVariables({})).to.throw();
182-
});
183-
184121
it('must return the API object', () => {
185-
configureApi();
186122
const returnedValue = api.autoProvideVariables({});
187123
expect(returnedValue).to.equal(api);
188124
});
@@ -191,12 +127,7 @@ describe('Public API', () => {
191127

192128
describe('autoProvidejQuery', () => {
193129

194-
it('should not be callable before the runtime environment has been configured', () => {
195-
expect(() => api.autoProvidejQuery()).to.throw();
196-
});
197-
198130
it('must return the API object', () => {
199-
configureApi();
200131
const returnedValue = api.autoProvidejQuery();
201132
expect(returnedValue).to.equal(api);
202133
});
@@ -205,12 +136,7 @@ describe('Public API', () => {
205136

206137
describe('enablePostCssLoader', () => {
207138

208-
it('should not be callable before the runtime environment has been configured', () => {
209-
expect(() => api.enablePostCssLoader()).to.throw();
210-
});
211-
212139
it('must return the API object', () => {
213-
configureApi();
214140
const returnedValue = api.enablePostCssLoader();
215141
expect(returnedValue).to.equal(api);
216142
});
@@ -219,12 +145,7 @@ describe('Public API', () => {
219145

220146
describe('enableSassLoader', () => {
221147

222-
it('should not be callable before the runtime environment has been configured', () => {
223-
expect(() => api.enableSassLoader()).to.throw();
224-
});
225-
226148
it('must return the API object', () => {
227-
configureApi();
228149
const returnedValue = api.enableSassLoader();
229150
expect(returnedValue).to.equal(api);
230151
});
@@ -233,12 +154,7 @@ describe('Public API', () => {
233154

234155
describe('enableLessLoader', () => {
235156

236-
it('should not be callable before the runtime environment has been configured', () => {
237-
expect(() => api.enableLessLoader()).to.throw();
238-
});
239-
240157
it('must return the API object', () => {
241-
configureApi();
242158
const returnedValue = api.enableLessLoader();
243159
expect(returnedValue).to.equal(api);
244160
});
@@ -247,12 +163,7 @@ describe('Public API', () => {
247163

248164
describe('setOutputPath', () => {
249165

250-
it('should not be callable before the runtime environment has been configured', () => {
251-
expect(() => api.configureBabel(() => {})).to.throw();
252-
});
253-
254166
it('must return the API object', () => {
255-
configureApi();
256167
const returnedValue = api.configureBabel(() => {});
257168
expect(returnedValue).to.equal(api);
258169
});
@@ -261,12 +172,7 @@ describe('Public API', () => {
261172

262173
describe('enableReactPreset', () => {
263174

264-
it('should not be callable before the runtime environment has been configured', () => {
265-
expect(() => api.enableReactPreset()).to.throw();
266-
});
267-
268175
it('must return the API object', () => {
269-
configureApi();
270176
const returnedValue = api.enableReactPreset();
271177
expect(returnedValue).to.equal(api);
272178
});
@@ -275,12 +181,7 @@ describe('Public API', () => {
275181

276182
describe('enableTypeScriptLoader', () => {
277183

278-
it('should not be callable before the runtime environment has been configured', () => {
279-
expect(() => api.enableTypeScriptLoader()).to.throw();
280-
});
281-
282184
it('must return the API object', () => {
283-
configureApi();
284185
const returnedValue = api.enableTypeScriptLoader();
285186
expect(returnedValue).to.equal(api);
286187
});
@@ -289,12 +190,7 @@ describe('Public API', () => {
289190

290191
describe('enableVueLoader', () => {
291192

292-
it('should not be callable before the runtime environment has been configured', () => {
293-
expect(() => api.enableVueLoader()).to.throw();
294-
});
295-
296193
it('must return the API object', () => {
297-
configureApi();
298194
const returnedValue = api.enableVueLoader();
299195
expect(returnedValue).to.equal(api);
300196
});
@@ -303,12 +199,7 @@ describe('Public API', () => {
303199

304200
describe('cleanupOutputBeforeBuild', () => {
305201

306-
it('should not be callable before the runtime environment has been configured', () => {
307-
expect(() => api.cleanupOutputBeforeBuild()).to.throw();
308-
});
309-
310202
it('must return the API object', () => {
311-
configureApi();
312203
const returnedValue = api.cleanupOutputBeforeBuild();
313204
expect(returnedValue).to.equal(api);
314205
});
@@ -324,11 +215,17 @@ describe('Public API', () => {
324215

325216
});
326217

327-
describe('clearRuntimeEnvironment', () => {
218+
describe('Runtime environment proxy', () => {
219+
beforeEach(() => {
220+
api.clearRuntimeEnvironment();
221+
});
328222

329-
it('should be callable even if the runtime environment has not been configured', () => {
223+
it('safe methods should be callable even if the runtime environment has not been configured', () => {
330224
expect(() => api.clearRuntimeEnvironment()).to.not.throw();
331225
});
332226

227+
it('unsafe methods should NOT be callable if the runtime environment has not been configured', () => {
228+
expect(() => api.setOutputPath('/')).to.throw('Encore.setOutputPath() cannot be called yet');
229+
});
333230
});
334231
});

0 commit comments

Comments
 (0)