From 30eeb2fadca28e4386e11f8b2f6682ec21e96784 Mon Sep 17 00:00:00 2001 From: falsandtru Date: Wed, 6 May 2020 08:16:04 +0900 Subject: [PATCH] fix(middleware): fix file type detection --- lib/helper.js | 4 ++++ lib/middleware/karma.js | 3 +-- test/unit/helper.spec.js | 9 +++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/helper.js b/lib/helper.js index 34b349efd..b1313d5a3 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -141,6 +141,10 @@ const replaceWinPath = (path) => { exports.normalizeWinPath = process.platform === 'win32' ? replaceWinPath : _.identity +exports.getFileType = (file) => { + return file.type || path.extname(file.path.split(/[?#]/, 1)[0] || '').substring(1) +} + exports.mkdirIfNotExists = (directory, done) => { // TODO(vojta): handle if it's a file /* eslint-disable handle-callback-err */ diff --git a/lib/middleware/karma.js b/lib/middleware/karma.js index dd1ee1d00..588fa609e 100644 --- a/lib/middleware/karma.js +++ b/lib/middleware/karma.js @@ -11,7 +11,6 @@ * - setting propert caching headers */ -const path = require('path') const url = require('url') const helper = require('../helper') @@ -164,7 +163,7 @@ function createKarmaMiddleware ( const scriptTags = [] for (const file of files.included) { let filePath = file.path - const fileType = file.type || path.extname(filePath).substring(1) + const fileType = helper.getFileType(file) if (helper.isDefined(fileType) && !FILE_TYPES.includes(fileType)) { log.warn(`Invalid file type (${fileType}), defaulting to js.`) diff --git a/test/unit/helper.spec.js b/test/unit/helper.spec.js index 8c5808c50..50318d150 100644 --- a/test/unit/helper.spec.js +++ b/test/unit/helper.spec.js @@ -228,6 +228,15 @@ describe('helper', () => { }) }) + describe('getFileType', () => { + it('should extract file type', () => { + expect(helper.getFileType({ path: 'a.js' })).to.equal('js') + expect(helper.getFileType({ path: 'a.js#' })).to.equal('js') + expect(helper.getFileType({ path: 'a.js?#' })).to.equal('js') + expect(helper.getFileType({ type: 'js', path: 'a' })).to.equal('js') + }) + }) + describe('mkdirIfNotExists', () => { const fsMock = require('mocks').fs const loadFile = require('mocks').loadFile