diff --git a/src/bundle.spec.ts b/src/bundle.spec.ts index 73df1f8d..eca71605 100644 --- a/src/bundle.spec.ts +++ b/src/bundle.spec.ts @@ -7,83 +7,78 @@ 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))); @@ -91,31 +86,29 @@ 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(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 @@ -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) { diff --git a/src/webpack/loader-impl.spec.ts b/src/webpack/loader-impl.spec.ts index 87ec82d6..33497936 100644 --- a/src/webpack/loader-impl.spec.ts +++ b/src/webpack/loader-impl.spec.ts @@ -107,10 +107,9 @@ 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 = () => { @@ -118,6 +117,9 @@ describe('webpack loader', () => { 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) => { diff --git a/src/webpack/loader-impl.ts b/src/webpack/loader-impl.ts index 5953616e..1223d89b 100644 --- a/src/webpack/loader-impl.ts +++ b/src/webpack/loader-impl.ts @@ -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[] = []; - 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 { @@ -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 };