Share your code. Components are building blocks, you are the architect.

testers/karma-mocha-react
v0.0.21
0.0.21
0.0.20
0.0.19
0.0.18
0.0.17
0.0.16
0.0.15
0.0.14
0.0.13
0.0.12
0.0.11
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
In envs
MIT
PUBLIC
266
18     DEPENDENCIES
Build: none
BuildNone
Bit testing environment for testing of React components using Karma, with Mocha and ChaiJS.

Please configure Bit as a scoped registry to install components with NPM.Learn more

npm config set '@bit:registry' https://node.bitsrc.io
Use testers/karma-mocha-react in your project
npm i @bit/bit.envs.testers.karma-mocha-react
Copied
Files
  • readme.md
  • jsonReporter.js
  • extractFileNameFromPath.js
  • index.js
  • karma.conf.js
index.js
173 Lines (145 sloc)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const mockery = require('mockery');
const chai = require('chai');
const chaiEnzyme = require('chai-enzyme');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
chai.use(sinonChai);
chai.use(chaiEnzyme())
const path = require('path');
const fse = require('fs-extra');
const teaspoon = require('teaspoon');
const TestUtils = require('react-dom/test-utils');
const React = require('react');
const ReactDom = require('react-dom');
const karma = require('karma');
const enzyme = require('enzyme');
require('react-test-renderer');
const { shallow } = require('enzyme');
const getFileName = require('./extractFileNameFromPath');
require('./karma.conf');
const isEmptyObject = obj => Object.keys(obj).length === 0;
const exec = require('child-process-promise').exec;

const readResults = (filePath) => {
  const resultsFilePath = path.resolve(`${__dirname}/${filePath}-results.json`);

  let parsedResults = {};

  try {
    parsedResults = fse.readJsonSync(resultsFilePath);
  }
  catch (e) {
    return {failures: [{title:'Cannot read results file',err:{message:e.message}}]};
  }

  try {
    fse.removeSync(resultsFilePath);
  }
  catch (e) {
    console.log('Cannot delete results file: ' + e.message);
  }

  return parsedResults;
}

function normalizeResults(results, runStart, runEnd) {
  
  let errorsFound = false;

  function normalizeError(test) {
    errorsFound = true;
    let message = 'No info regarding error';
    let stack = 'No info regarding error';
    
    if (test.log.length > 0) {
      const errorLines = test.log[0].split('at');
      message = errorLines.shift();
      stack = 'at ' + errorLines.join('at');
  }
    
    return {
      message,
      stack
    };
  }

  function normalizeTest(test) {
      return ({
        title: test.suite[0] + ' ' + test.description,
        pass: test.success,
        err: !test.success ? normalizeError(test) : null,
        duration: test.endTime - test.startTime
      });
  }

  function getFailures() {
    let failures = results.failures || [];

    if (failures.length == 0 && !errorsFound && results.summary.error) {
      failures[0] = {title:'Karma failure',err:{message:''}};
    }

    return failures;
  }

  return {
    tests: results.result && !isEmptyObject(results.result) ?
      Object.values(results.result)[0].map(normalizeTest) :
      [],
    stats: {
      start: runStart,
      end: runEnd
    },
    failures: getFailures()
  };
}

const run = (specFile) => {
  
  const karmaConfig = karma.config.parseConfig(path.resolve(__dirname + '/karma.conf.js'), { files: [specFile], port: 9876 } );
  var server = new karma.Server(karmaConfig, function(exitCode) {
    console.info('Karma Server has exited with ' + exitCode)
    process.exit(exitCode)
  });

  let runStart;

  server.on('run_start', browsers => {
    runStart = new Date();
  });

  return new Promise((resolve) => {    
    let isBrowserValid = false;
    server.start();

    server.on('browser_register', browsers => {
      isBrowserValid = true;
    });

    server.on('run_complete', (browsers, resultsSummary) => {
      const runEnd = new Date();
      const parsedResults = readResults(getFileName(specFile));

      if (!isBrowserValid) {
        return resolve(normalizeBrowserFailure());
      }

      if (isEmptyObject(parsedResults)) {
        return resolve({});
      }
      
      const normalizedResults = normalizeResults(parsedResults, runStart, runEnd);
      return resolve(normalizedResults);
    });
    
  });
};

const normalizeBrowserFailure = () => {
  return {
    tests: [],
    pass: false,
    stats: {
      start: null,
      end: null
    },
    failures: [{title: 'Cannot find browser',
      err: {
        message: ''
      }
    }]
  }
};

module.exports = {
  run,
  globals: {
    mockery,
    ReactDom,
    shallow,
    chai,
    sinon
  },
  modules: {
    teaspoon,
    mockery,
    enzyme,
    chai,
    sinon,
    'react-dom/test-utils': TestUtils,
    'react-addons-test-utils': TestUtils,
    'react-dom': ReactDom
  }
};