Skip to content

Commit f90487b

Browse files
authored
fix(karma-webpack) respect exclude list from config (codymikol#520) (codymikol#521)
* fix(karma-webpack) respect exclude list from config (codymikol#520) * fix(test) get the actual tmpdir location * fix(test) respect os path separator * chore(test) test exclude
1 parent 934a486 commit f90487b

5 files changed

Lines changed: 82 additions & 4 deletions

File tree

lib/karma-webpack/preprocessor.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ function configToWebpackEntries(config) {
1919
const { preprocessors } = config;
2020

2121
let files = [];
22+
const exclude = config.exclude || [];
23+
const excluded = (filePath) =>
24+
exclude.find((exPat) => minimatch(filePath, exPat));
25+
2226
config.files.forEach((fileEntry, i) => {
2327
// forcefully disable karma watch as we use webpack watch only
2428
config.files[i].watched = false;
@@ -34,7 +38,7 @@ function configToWebpackEntries(config) {
3438
const filteredFiles = [];
3539
files.forEach((filePath) => {
3640
filteredPreprocessorsPatterns.forEach((pattern) => {
37-
if (minimatch(filePath, pattern)) {
41+
if (minimatch(filePath, pattern) && !excluded(filePath)) {
3842
filteredFiles.push(filePath);
3943
}
4044
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* eslint-disable prettier/prettier */
2+
3+
import karmaChromeLauncher from 'karma-chrome-launcher';
4+
import karmaMocha from 'karma-mocha';
5+
import karmaChai from 'karma-chai';
6+
7+
import Scenario from '../../utils/scenario';
8+
9+
process.env.CHROME_BIN = require('puppeteer').executablePath();
10+
11+
const path = require('path');
12+
13+
const karmaWebpack = require('../../../../lib/index');
14+
15+
// The karma server integration tests take longer than the jest 5 sec default,
16+
// we will give them 30 seconds to complete.
17+
const KARMA_SERVER_TIMEOUT = 30 * 1000;
18+
19+
describe('A basic karma-webpack setup', () => {
20+
21+
let scenario;
22+
23+
const TEST_PATH_FAILING = path.resolve(__dirname, './index.scenario.js');
24+
const TEST_PATH_PASSING = path.resolve(__dirname, './js-test-passes.js');
25+
26+
const config = {
27+
frameworks: ['mocha', 'chai', 'webpack'],
28+
files: [{ pattern: TEST_PATH_FAILING }, TEST_PATH_PASSING],
29+
exclude: [TEST_PATH_FAILING],
30+
preprocessors: { [TEST_PATH_FAILING]: ['webpack'] },
31+
webpack: {},
32+
browsers: ['ChromeHeadless'],
33+
// Explicitly turn off reporters so the simulated test results are not confused with the actual results.
34+
reporters: [],
35+
plugins: [karmaWebpack, karmaChromeLauncher, karmaMocha, karmaChai],
36+
port: 2389,
37+
logLevel: 'ERROR',
38+
singleRun: true
39+
};
40+
41+
beforeAll((done) => {
42+
jest.spyOn(console, 'log').mockImplementation()
43+
Scenario.run(config)
44+
.then((res) => {
45+
scenario = res;
46+
})
47+
.catch((err) => console.error('Integration Scenario Failed: ', err))
48+
.finally(() => done());
49+
}, KARMA_SERVER_TIMEOUT);
50+
51+
it('should have an exit code of 0 because the failing file was excluded', () => {
52+
expect(scenario.exitCode).toBe(0);
53+
})
54+
55+
it('should have one successful test runs', () => {
56+
expect(scenario.success).toBe(1);
57+
});
58+
59+
it('should have zero failed test runs', () => {
60+
expect(scenario.failed).toBe(0);
61+
});
62+
63+
it('should complete with no errors', () => {
64+
expect(scenario.error).toBe(false);
65+
});
66+
67+
});

test/integration/scenarios/basic-setup/custom-output-path.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import karmaChai from 'karma-chai';
77
import Scenario from '../../utils/scenario';
88

99
const fs = require('fs');
10+
const os = require('os');
1011

1112
process.env.CHROME_BIN = require('puppeteer').executablePath();
1213

@@ -17,7 +18,7 @@ const karmaWebpack = require('../../../../lib/index');
1718
// The karma server integration tests take longer than the jest 5 sec default,
1819
// we will give them 30 seconds to complete.
1920
const KARMA_SERVER_TIMEOUT = 30 * 1000;
20-
const KARMA_CUSTOM_PATH = '/tmp/karma_webpack__custom_file_path';
21+
const KARMA_CUSTOM_PATH = path.join(os.tmpdir(), 'karma_webpack__custom_file_path');
2122

2223
describe('A basic karma-webpack setup', () => {
2324
let scenario;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('quick maths', () => {
2+
it('2 plus 2 is 4', () => {
3+
assert.equal(2 + 2, 4);
4+
});
5+
});

test/unit/karma-webpack/controller.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const os = require('os');
2+
const path = require('path');
23

34
const KW_Controller = require('../../../lib/karma-webpack/controller');
45
const DefaultWebpackOptionsFactory = require('../../../lib/webpack/defaults');
56

67
const defaultWebpackOptions = DefaultWebpackOptionsFactory.create();
78

89
describe('KW_Controller', () => {
9-
const EXPECTED_DEFAULT_PATH_PREFIX = '/_karma_webpack_';
10+
const EXPECTED_DEFAULT_PATH_PREFIX = '_karma_webpack_';
1011

1112
let controller;
1213

@@ -20,7 +21,7 @@ describe('KW_Controller', () => {
2021
it('correctly sets the default output path prefix', () => {
2122
expect(
2223
controller.webpackOptions.output.path.startsWith(
23-
os.tmpdir() + EXPECTED_DEFAULT_PATH_PREFIX
24+
path.join(os.tmpdir(), EXPECTED_DEFAULT_PATH_PREFIX)
2425
)
2526
).toBeTruthy();
2627
});

0 commit comments

Comments
 (0)