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

Commit

Permalink
chore(): resolve uncaught rejected promise warning from webpack loade…
Browse files Browse the repository at this point in the history
…r-impl
  • Loading branch information
jthoms1 committed Dec 19, 2016
1 parent deeafed commit 3c08f59
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 44 deletions.
40 changes: 16 additions & 24 deletions src/bundle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,115 +7,108 @@ import { ChangedFile } from './util/interfaces';
describe('bundle task', () => {

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

// act
bundle.bundle(context).then(() => {
return bundle.bundle(context).then(() => {
// assert
expect(rollup.rollup).toHaveBeenCalled();
done();
});
});

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

// act
bundle.bundle(context).then(() => {
return bundle.bundle(context).then(() => {
throw new Error('Should never happen');
}).catch(err => {
// assert
expect(rollup.rollup).toHaveBeenCalled();
expect(err.message).toBe(errorText);
done();
});
});

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

// act
bundle.bundle(context).then(() => {
return bundle.bundle(context).then(() => {
// assert
expect(webpack.webpack).toHaveBeenCalled();
done();
});
});

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

// act
bundle.bundle(context).then(() => {
return bundle.bundle(context).then(() => {
throw new Error('Should never happen');
}).catch(err => {
// assert
expect(webpack.webpack).toHaveBeenCalled();
expect(err.message).toBe(errorText);
done();
});
});
});

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

// act
bundle.bundleUpdate(changedFiles, context).then(() => {
return bundle.bundleUpdate(changedFiles, context).then(() => {
// assert
expect(rollup.rollupUpdate).toHaveBeenCalledWith(changedFiles, context);
done();
});
});

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

// act
bundle.bundleUpdate(changedFiles, context).then(() => {
return bundle.bundleUpdate(changedFiles, context).then(() => {
throw new Error('Should never happen');
}).catch(err => {
// assert
expect(rollup.rollupUpdate).toHaveBeenCalled();
expect(err.message).toBe(errorText);
done();
});
});

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

// act
bundle.bundleUpdate(changedFiles, context).then(() => {
return bundle.bundleUpdate(changedFiles, context).then(() => {
// assert
expect(webpack.webpackUpdate).toHaveBeenCalledWith(changedFiles, context);
done();
});
});

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

// act
bundle.bundleUpdate(changedFiles, context).then(() => {
return bundle.bundleUpdate(changedFiles, context).then(() => {
throw new Error('Should never happen');
}).catch(err => {
// assert
expect(webpack.webpackUpdate).toHaveBeenCalled();
expect(err.message).toBe(errorText);
done();
});

} catch (ex) {
Expand Down
10 changes: 6 additions & 4 deletions src/webpack/loader-impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,19 @@ describe('webpack loader', () => {
spyOn(helpers, helpers.getContext.name).and.returnValue(mockContext);
spyOn(mockContext.fileCache, mockContext.fileCache.get.name).and.returnValue(null);
spyOn(mockContext.fileCache, mockContext.fileCache.set.name);
spyOn(helpers, helpers.readFileAsync.name).and.returnValue(Promise.reject(new Error(cantReadFileError)));

// act
loader.webpackLoader(sourceString, mockSourceMap, mockWebpackObject);
spyOn(helpers, helpers.readFileAsync.name).and.callFake(() => {
return Promise.reject(new Error(cantReadFileError));
});

// assert
const assertFunction = () => {
expect(spy.calls.mostRecent().args[0]).toBeTruthy();
expect(spy.calls.mostRecent().args[0].message).toEqual(cantReadFileError);
done();
};

// act
return loader.webpackLoader(sourceString, mockSourceMap, mockWebpackObject);
});

it('should callback with content from disk', (done: Function) => {
Expand Down
21 changes: 5 additions & 16 deletions src/webpack/loader-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,10 @@ export function webpackLoader(source: string, map: any, webpackContex: any) {
const javascriptPath = changeExtension(absolutePath, '.js');
const sourceMapPath = javascriptPath + '.map';

let javascriptFile: File = null;
let mapFile: File = null;

const promises: Promise<File>[] = [];
let readJavascriptFilePromise = readFile(context.fileCache, javascriptPath);
promises.push(readJavascriptFilePromise);
readJavascriptFilePromise.then(file => {
javascriptFile = file;
});
let readJavascriptMapFilePromise = readFile(context.fileCache, sourceMapPath);
promises.push(readJavascriptMapFilePromise);
readJavascriptMapFilePromise.then(file => {
mapFile = file;
});

Promise.all(promises).then(() => {
Promise.all([
readFile(context.fileCache, javascriptPath),
readFile(context.fileCache, sourceMapPath)
]).then(([javascriptFile, mapFile]) => {
let sourceMapObject = map;
if (mapFile) {
try {
Expand Down Expand Up @@ -58,6 +46,7 @@ function readFile(fileCache: FileCache, filePath: string) {
return Promise.resolve(file);
}
Logger.debug(`[Webpack] loader: File ${filePath} not found in file cache - falling back to disk`);

return readFileAsync(filePath).then((fileContent: string) => {
Logger.debug(`[Webpack] loader: Loaded ${filePath} successfully from disk`);
const file = { path: filePath, content: fileContent };
Expand Down

0 comments on commit 3c08f59

Please sign in to comment.