Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 3c08f59

Browse files
committed
chore(): resolve uncaught rejected promise warning from webpack loader-impl
1 parent deeafed commit 3c08f59

File tree

3 files changed

+27
-44
lines changed

3 files changed

+27
-44
lines changed

src/bundle.spec.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,115 +7,108 @@ import { ChangedFile } from './util/interfaces';
77
describe('bundle task', () => {
88

99
describe('bundle', () => {
10-
it('should return the value rollup task returns', (done: Function) => {
10+
it('should return the value rollup task returns', () => {
1111
// arrange
1212
spyOn(rollup, rollup.rollup.name).and.returnValue(Promise.resolve());
1313
const context = { bundler: Constants.BUNDLER_ROLLUP};
1414

1515
// act
16-
bundle.bundle(context).then(() => {
16+
return bundle.bundle(context).then(() => {
1717
// assert
1818
expect(rollup.rollup).toHaveBeenCalled();
19-
done();
2019
});
2120
});
2221

23-
it('should throw when rollup throws', (done: Function) => {
22+
it('should throw when rollup throws', () => {
2423
const errorText = 'simulating an error';
2524
// arrange
2625
spyOn(rollup, rollup.rollup.name).and.returnValue(Promise.reject(new Error(errorText)));
2726
const context = { bundler: Constants.BUNDLER_ROLLUP};
2827

2928
// act
30-
bundle.bundle(context).then(() => {
29+
return bundle.bundle(context).then(() => {
3130
throw new Error('Should never happen');
3231
}).catch(err => {
3332
// assert
3433
expect(rollup.rollup).toHaveBeenCalled();
3534
expect(err.message).toBe(errorText);
36-
done();
3735
});
3836
});
3937

40-
it('should return the value webpack task returns', (done: Function) => {
38+
it('should return the value webpack task returns', () => {
4139
// arrange
4240
spyOn(webpack, webpack.webpack.name).and.returnValue(Promise.resolve());
4341
const context = { bundler: Constants.BUNDLER_WEBPACK};
4442

4543
// act
46-
bundle.bundle(context).then(() => {
44+
return bundle.bundle(context).then(() => {
4745
// assert
4846
expect(webpack.webpack).toHaveBeenCalled();
49-
done();
5047
});
5148
});
5249

53-
it('should throw when rollup throws', (done: Function) => {
50+
it('should throw when rollup throws', () => {
5451
const errorText = 'simulating an error';
5552
// arrange
5653
spyOn(webpack, webpack.webpack.name).and.returnValue(Promise.reject(new Error(errorText)));
5754
const context = { bundler: Constants.BUNDLER_WEBPACK};
5855

5956
// act
60-
bundle.bundle(context).then(() => {
57+
return bundle.bundle(context).then(() => {
6158
throw new Error('Should never happen');
6259
}).catch(err => {
6360
// assert
6461
expect(webpack.webpack).toHaveBeenCalled();
6562
expect(err.message).toBe(errorText);
66-
done();
6763
});
6864
});
6965
});
7066

7167
describe('bundleUpdate', () => {
72-
it('should return the value rollup returns', (done: Function) => {
68+
it('should return the value rollup returns', () => {
7369
// arrange
7470
spyOn(rollup, rollup.rollupUpdate.name).and.returnValue(Promise.resolve());
7571
const context = { bundler: Constants.BUNDLER_ROLLUP};
7672
const changedFiles: ChangedFile[] = [];
7773

7874
// act
79-
bundle.bundleUpdate(changedFiles, context).then(() => {
75+
return bundle.bundleUpdate(changedFiles, context).then(() => {
8076
// assert
8177
expect(rollup.rollupUpdate).toHaveBeenCalledWith(changedFiles, context);
82-
done();
8378
});
8479
});
8580

86-
it('should throw when rollup throws', (done: Function) => {
81+
it('should throw when rollup throws', () => {
8782
const errorText = 'simulating an error';
8883
// arrange
8984
spyOn(rollup, rollup.rollupUpdate.name).and.returnValue(Promise.reject(new Error(errorText)));
9085
const context = { bundler: Constants.BUNDLER_ROLLUP};
9186
const changedFiles: ChangedFile[] = [];
9287

9388
// act
94-
bundle.bundleUpdate(changedFiles, context).then(() => {
89+
return bundle.bundleUpdate(changedFiles, context).then(() => {
9590
throw new Error('Should never happen');
9691
}).catch(err => {
9792
// assert
9893
expect(rollup.rollupUpdate).toHaveBeenCalled();
9994
expect(err.message).toBe(errorText);
100-
done();
10195
});
10296
});
10397

104-
it('should return the value webpack returns', (done: Function) => {
98+
it('should return the value webpack returns', () => {
10599
// arrange
106100
spyOn(webpack, webpack.webpackUpdate.name).and.returnValue(Promise.resolve());
107101
const context = { bundler: Constants.BUNDLER_WEBPACK};
108102
const changedFiles: ChangedFile[] = [];
109103

110104
// act
111-
bundle.bundleUpdate(changedFiles, context).then(() => {
105+
return bundle.bundleUpdate(changedFiles, context).then(() => {
112106
// assert
113107
expect(webpack.webpackUpdate).toHaveBeenCalledWith(changedFiles, context);
114-
done();
115108
});
116109
});
117110

118-
it('should throw when webpack throws', (done: Function) => {
111+
it('should throw when webpack throws', () => {
119112
const errorText = 'simulating an error';
120113
try {
121114
// arrange
@@ -124,13 +117,12 @@ describe('bundle task', () => {
124117
const changedFiles: ChangedFile[] = [];
125118

126119
// act
127-
bundle.bundleUpdate(changedFiles, context).then(() => {
120+
return bundle.bundleUpdate(changedFiles, context).then(() => {
128121
throw new Error('Should never happen');
129122
}).catch(err => {
130123
// assert
131124
expect(webpack.webpackUpdate).toHaveBeenCalled();
132125
expect(err.message).toBe(errorText);
133-
done();
134126
});
135127

136128
} catch (ex) {

src/webpack/loader-impl.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,19 @@ describe('webpack loader', () => {
107107
spyOn(helpers, helpers.getContext.name).and.returnValue(mockContext);
108108
spyOn(mockContext.fileCache, mockContext.fileCache.get.name).and.returnValue(null);
109109
spyOn(mockContext.fileCache, mockContext.fileCache.set.name);
110-
spyOn(helpers, helpers.readFileAsync.name).and.returnValue(Promise.reject(new Error(cantReadFileError)));
111-
112-
// act
113-
loader.webpackLoader(sourceString, mockSourceMap, mockWebpackObject);
110+
spyOn(helpers, helpers.readFileAsync.name).and.callFake(() => {
111+
return Promise.reject(new Error(cantReadFileError));
112+
});
114113

115114
// assert
116115
const assertFunction = () => {
117116
expect(spy.calls.mostRecent().args[0]).toBeTruthy();
118117
expect(spy.calls.mostRecent().args[0].message).toEqual(cantReadFileError);
119118
done();
120119
};
120+
121+
// act
122+
return loader.webpackLoader(sourceString, mockSourceMap, mockWebpackObject);
121123
});
122124

123125
it('should callback with content from disk', (done: Function) => {

src/webpack/loader-impl.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,10 @@ export function webpackLoader(source: string, map: any, webpackContex: any) {
1414
const javascriptPath = changeExtension(absolutePath, '.js');
1515
const sourceMapPath = javascriptPath + '.map';
1616

17-
let javascriptFile: File = null;
18-
let mapFile: File = null;
19-
20-
const promises: Promise<File>[] = [];
21-
let readJavascriptFilePromise = readFile(context.fileCache, javascriptPath);
22-
promises.push(readJavascriptFilePromise);
23-
readJavascriptFilePromise.then(file => {
24-
javascriptFile = file;
25-
});
26-
let readJavascriptMapFilePromise = readFile(context.fileCache, sourceMapPath);
27-
promises.push(readJavascriptMapFilePromise);
28-
readJavascriptMapFilePromise.then(file => {
29-
mapFile = file;
30-
});
31-
32-
Promise.all(promises).then(() => {
17+
Promise.all([
18+
readFile(context.fileCache, javascriptPath),
19+
readFile(context.fileCache, sourceMapPath)
20+
]).then(([javascriptFile, mapFile]) => {
3321
let sourceMapObject = map;
3422
if (mapFile) {
3523
try {
@@ -58,6 +46,7 @@ function readFile(fileCache: FileCache, filePath: string) {
5846
return Promise.resolve(file);
5947
}
6048
Logger.debug(`[Webpack] loader: File ${filePath} not found in file cache - falling back to disk`);
49+
6150
return readFileAsync(filePath).then((fileContent: string) => {
6251
Logger.debug(`[Webpack] loader: Loaded ${filePath} successfully from disk`);
6352
const file = { path: filePath, content: fileContent };

0 commit comments

Comments
 (0)