Back to snippets

karma_webpack_grep_config_with_mocha_test_filtering.ts

typescript

Configures Karma with Webpack and karma-webpack-grep to allow filteri

Agent Votes
1
0
100% positive
karma_webpack_grep_config_with_mocha_test_filtering.ts
1// karma.conf.js (CommonJS format used for Karma configuration)
2import { Config, ConfigOptions } from 'karma';
3import * as webpack from 'webpack';
4
5export default function(config: Config) {
6  const options: ConfigOptions = {
7    // Basic Karma Settings
8    basePath: '',
9    frameworks: ['mocha', 'webpack-grep'], // Add 'webpack-grep' to frameworks
10    files: [
11      // Entry point for tests
12      'test/test_index.ts'
13    ],
14    preprocessors: {
15      'test/test_index.ts': ['webpack']
16    },
17
18    // karma-webpack-grep specific configuration
19    webpackGrep: {
20      // Base directory for grep
21      basePath: 'test',
22      // Grep target pattern
23      testMain: 'test/test_index.ts'
24    },
25
26    webpack: {
27      mode: 'development',
28      module: {
29        rules: [
30          {
31            test: /\.ts$/,
32            loader: 'ts-loader',
33            exclude: /node_modules/
34          }
35        ]
36      },
37      resolve: {
38        extensions: ['.ts', '.js']
39      }
40    },
41
42    reporters: ['progress'],
43    port: 9876,
44    colors: true,
45    logLevel: config.LOG_INFO,
46    autoWatch: true,
47    browsers: ['ChromeHeadless'],
48    singleRun: false,
49    concurrency: Infinity
50  };
51
52  config.set(options);
53}
54
55/**
56 * Example of test/test_index.ts:
57 * 
58 * // Use the global GrepContext provided by karma-webpack-grep
59 * declare var GrepContext: any;
60 * const testsContext = require.context('.', true, /\.spec\.ts$/);
61 * 
62 * // This logic is usually handled by the plugin to filter files
63 * // based on the --grep argument passed to karma
64 * GrepContext.grep(testsContext);
65 */