-
Notifications
You must be signed in to change notification settings - Fork 167
/
test_index.js
41 lines (36 loc) · 2.02 KB
/
test_index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Use Webpack's `require.context()` method to find all test files. See karma-webpack's
// "alternative usage" https://github.com/webpack/karma-webpack#alternative-usage and
// https://webpack.github.io/docs/context.html for more, ahem, "context".
var appInitializerRE = /(components|\.)\/index.js/;
var testFileRE = /\.test\.js/;
// Build a list of all JS and JSX files in the application source directory
var context = require.context('./src/js', true, /\.jsx?$/);
// `require.context` returns a customized require function: we will invoke that
// function for each file path we actually want to include in our bundle.
// Files must be included in our test bundle in order to be instrumented for
// code coverage reporting. To this end, we start by loading all non-test files
// to ensure we get reports for files that are not yet tested, so that "100%"
// coverage really will be.
//
// The two exceptions are the root index.js file (which is also suppressed via
// IgnorePlugin, see "note also" below) and the root component file which
// renders the root React component into the document; both of these are for
// kicking off the app in a live environment, and cause undesirable side-
// effects when run in a test environment.
//
// It's important to note that the tests will run if we skip this step: we'd
// simply only have coverage reports for files that are actually required from
// our test specs. See https://github.com/deepsweet/istanbul-instrumenter-loader
// for more information.
//
// Note also that Webpack will _parse_ the files even if they are filtered out
// here, so if you want to exclude a file's existence from Webpack entirely then
// an IgnorePlugin in Karma's Webpack configuration is a more thorough solution.
context.keys().filter(function(path) {
return !testFileRE.test(path) && !appInitializerRE.test(path);
}).forEach(context);
// Finally, extract the test spec files from that list and require them to kick
// off our unit tests.
context.keys().filter(function(path) {
return testFileRE.test(path);
}).forEach(context);