Start sharing components as a team!Share components as a team!Join Bit to build your applications faster.Get Started Free
This component has been deprecated

karma-mocha-react

v0.0.22arrow_drop_down
v0.0.22
v0.0.21
v0.0.20
v0.0.19
v0.0.18
v0.0.17
v0.0.16
v0.0.15
v0.0.14
v0.0.13
v0.0.12
v0.0.11
v0.0.10
v0.0.9
v0.0.8
v0.0.7
v0.0.6
v0.0.5
v0.0.4
v0.0.3
v0.0.2
v0.0.1
STATUS
None
DOWNLOADS
1,420
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
3 years ago
SIZE
N/A
DEPRECATED!!!
2 contributors
Install karma-mocha-react as a package?
Copied
npm i @bit/bit.envs.testers.karma-mocha-react
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
Files
  • extractFileNameFromPath.js
  • index.js
  • jsonReporter.js
  • karma.conf.js
  • readme.md
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
  }
};
Help and resources