From 360e69c7926c1801256504812c16151be833f14b Mon Sep 17 00:00:00 2001 From: Khoa Bui Date: Tue, 24 Sep 2019 23:26:23 +0700 Subject: [PATCH] fix: module build failed error at Linter.parseResults (#294) --- src/Linter.js | 10 ++++++---- test/Linter.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 test/Linter.test.js diff --git a/src/Linter.js b/src/Linter.js index 1ff0792..e9d0bd7 100644 --- a/src/Linter.js +++ b/src/Linter.js @@ -113,10 +113,12 @@ export default class Linter { parseResults({ results }) { // add filename for each results so formatter can have relevant filename - results.forEach((r) => { - // eslint-disable-next-line no-param-reassign - r.filePath = this.loaderContext.resourcePath; - }); + if (results) { + results.forEach((r) => { + // eslint-disable-next-line no-param-reassign + r.filePath = this.loaderContext.resourcePath; + }); + } return results; } diff --git a/test/Linter.test.js b/test/Linter.test.js new file mode 100644 index 0000000..e80dfc1 --- /dev/null +++ b/test/Linter.test.js @@ -0,0 +1,24 @@ +import Linter from '../src/Linter'; + +const loaderContext = { resourcePath: 'test' }; +const options = { + eslintPath: 'eslint', + ignore: false, + formatter: jest.fn(), +}; +const res = { results: [{ filePath: '' }] }; + +describe('Linter', () => { + let linter; + beforeAll(() => { + linter = new Linter(loaderContext, options); + }); + + it('should parse undefined results without error', () => { + expect(linter.parseResults({})).toBeUndefined(); + }); + + it('should parse results correctly', () => { + expect(linter.parseResults(res)).toEqual([{ filePath: 'test' }]); + }); +});