test_bundle.js 5.77 KB
Newer Older
Mike Greiling's avatar
Mike Greiling committed
1 2
/* eslint-disable jasmine/no-global-setup, jasmine/no-unsafe-spy, no-underscore-dangle */

3
import $ from 'jquery';
Jacob Schatz's avatar
Jacob Schatz committed
4
import 'vendor/jasmine-jquery';
5
import '~/commons';
Fatih Acet's avatar
Fatih Acet committed
6

7 8
import Vue from 'vue';
import VueResource from 'vue-resource';
9
import Translate from '~/vue_shared/translate';
10

11
import { getDefaultAdapter } from '~/lib/utils/axios_utils';
12 13 14
import { FIXTURES_PATH, TEST_HOST } from './test_constants';

import customMatchers from './matchers';
15

16 17 18 19
const isHeadlessChrome = /\bHeadlessChrome\//.test(navigator.userAgent);
Vue.config.devtools = !isHeadlessChrome;
Vue.config.productionTip = false;

Winnie Hellmann's avatar
Winnie Hellmann committed
20 21 22 23 24 25
let hasVueWarnings = false;
Vue.config.warnHandler = (msg, vm, trace) => {
  hasVueWarnings = true;
  fail(`${msg}${trace}`);
};

26
let hasVueErrors = false;
27
Vue.config.errorHandler = function(err) {
28 29 30 31
  hasVueErrors = true;
  fail(err);
};

32
Vue.use(VueResource);
33
Vue.use(Translate);
34

35
// enable test fixtures
36 37 38 39
jasmine.getFixtures().fixturesPath = FIXTURES_PATH;
jasmine.getJSONFixtures().fixturesPath = FIXTURES_PATH;

beforeAll(() => jasmine.addMatchers(customMatchers));
40

41
// globalize common libraries
42 43
window.$ = $;
window.jQuery = window.$;
44 45

// stub expected globals
46
window.gl = window.gl || {};
47
window.gl.TEST_HOST = TEST_HOST;
48
window.gon = window.gon || {};
Mike Greiling's avatar
Mike Greiling committed
49
window.gon.test_env = true;
50
gon.relative_url_root = '';
51

52 53
let hasUnhandledPromiseRejections = false;

54
window.addEventListener('unhandledrejection', event => {
55 56 57 58 59
  hasUnhandledPromiseRejections = true;
  console.error('Unhandled promise rejection:');
  console.error(event.reason.stack || event.reason);
});

Mike Greiling's avatar
Mike Greiling committed
60 61 62 63 64 65 66 67 68 69 70
// Add global function to spy on a module's dependencies via rewire
window.spyOnDependency = (module, name) => {
  const dependency = module.__GetDependency__(name);
  const spy = jasmine.createSpy(name, dependency);
  module.__Rewire__(name, spy);
  return spy;
};

// Reset any rewired modules after each test (see babel-plugin-rewire)
afterEach(__rewire_reset_all__); // eslint-disable-line

71 72 73 74 75 76
// HACK: Chrome 59 disconnects if there are too many synchronous tests in a row
// because it appears to lock up the thread that communicates to Karma's socket
// This async beforeEach gets called on every spec and releases the JS thread long
// enough for the socket to continue to communicate.
// The downside is that it creates a minor performance penalty in the time it takes
// to run our unit tests.
77 78 79 80 81 82 83 84
beforeEach(done => done());

const builtinVueHttpInterceptors = Vue.http.interceptors.slice();

beforeEach(() => {
  // restore interceptors so we have no remaining ones from previous tests
  Vue.http.interceptors = builtinVueHttpInterceptors.slice();
});
85

86 87
const axiosDefaultAdapter = getDefaultAdapter();

88 89
// render all of our tests
const testsContext = require.context('.', true, /_spec$/);
90
testsContext.keys().forEach(function(path) {
91
  try {
92
    testsContext(path);
93
  } catch (err) {
94
    console.error('[ERROR] Unable to load spec: ', path);
95 96
    describe('Test bundle', function() {
      it(`includes '${path}'`, function() {
97 98 99
        expect(err).toBeNull();
      });
    });
100 101
  }
});
102

Winnie Hellmann's avatar
Winnie Hellmann committed
103
describe('test errors', () => {
104
  beforeAll(done => {
105
    if (hasUnhandledPromiseRejections || hasVueWarnings || hasVueErrors) {
Winnie Hellmann's avatar
Winnie Hellmann committed
106 107 108 109 110 111 112 113 114 115 116 117 118
      setTimeout(done, 1000);
    } else {
      done();
    }
  });

  it('has no unhandled Promise rejections', () => {
    expect(hasUnhandledPromiseRejections).toBe(false);
  });

  it('has no Vue warnings', () => {
    expect(hasVueWarnings).toBe(false);
  });
119 120 121 122

  it('has no Vue error', () => {
    expect(hasVueErrors).toBe(false);
  });
123 124 125 126 127 128

  it('restores axios adapter after mocking', () => {
    if (getDefaultAdapter() !== axiosDefaultAdapter) {
      fail('axios adapter is not restored! Did you forget a restore() on MockAdapter?');
    }
  });
129 130
});

131 132 133 134 135
// if we're generating coverage reports, make sure to include all files so
// that we can catch files with 0% coverage
// see: https://github.com/deepsweet/istanbul-instrumenter-loader/issues/15
if (process.env.BABEL_ENV === 'coverage') {
  // exempt these files from the coverage report
136
  const troubleMakers = [
137
    './blob_edit/blob_bundle.js',
138 139 140
    './boards/components/modal/empty_state.js',
    './boards/components/modal/footer.js',
    './boards/components/modal/header.js',
141
    './cycle_analytics/cycle_analytics_bundle.js',
142 143 144
    './cycle_analytics/components/stage_plan_component.js',
    './cycle_analytics/components/stage_staging_component.js',
    './cycle_analytics/components/stage_test_component.js',
145 146
    './commit/pipelines/pipelines_bundle.js',
    './diff_notes/diff_notes_bundle.js',
147 148
    './diff_notes/components/jump_to_discussion.js',
    './diff_notes/components/resolve_count.js',
149 150 151 152 153 154
    './dispatcher.js',
    './environments/environments_bundle.js',
    './graphs/graphs_bundle.js',
    './issuable/time_tracking/time_tracking_bundle.js',
    './main.js',
    './merge_conflicts/merge_conflicts_bundle.js',
155 156
    './merge_conflicts/components/inline_conflict_lines.js',
    './merge_conflicts/components/parallel_conflict_lines.js',
157 158
    './monitoring/monitoring_bundle.js',
    './network/network_bundle.js',
159
    './network/branch_graph.js',
160 161 162 163 164
    './profile/profile_bundle.js',
    './protected_branches/protected_branches_bundle.js',
    './snippet/snippet_bundle.js',
    './terminal/terminal_bundle.js',
    './users/users_bundle.js',
Regis Boudinot's avatar
Regis Boudinot committed
165
    './issue_show/index.js',
166 167
  ];

168
  describe('Uncovered files', function() {
169
    const sourceFiles = require.context('~', true, /\.js$/);
Jacob Schatz's avatar
Jacob Schatz committed
170 171 172

    $.holdReady(true);

173
    sourceFiles.keys().forEach(function(path) {
174 175 176 177
      // ignore if there is a matching spec file
      if (testsContext.keys().indexOf(`${path.replace(/\.js$/, '')}_spec`) > -1) {
        return;
      }
178

179
      it(`includes '${path}'`, function() {
180 181 182 183 184 185
        try {
          sourceFiles(path);
        } catch (err) {
          if (troubleMakers.indexOf(path) === -1) {
            expect(err).toBeNull();
          }
186
        }
187
      });
188 189
    });
  });
190
}