BigW Consortium Gitlab

test_bundle.js 5.03 KB
Newer Older
1
/* eslint-disable jasmine/no-global-setup */
2 3 4
import $ from 'jquery';
import 'jasmine-jquery';
import '~/commons';
Fatih Acet committed
5

6 7 8
import Vue from 'vue';
import VueResource from 'vue-resource';

9 10
import { getDefaultAdapter } from '~/lib/utils/axios_utils';

11 12 13 14
const isHeadlessChrome = /\bHeadlessChrome\//.test(navigator.userAgent);
Vue.config.devtools = !isHeadlessChrome;
Vue.config.productionTip = false;

Winnie Hellmann committed
15 16 17 18 19 20
let hasVueWarnings = false;
Vue.config.warnHandler = (msg, vm, trace) => {
  hasVueWarnings = true;
  fail(`${msg}${trace}`);
};

21 22 23 24 25 26
let hasVueErrors = false;
Vue.config.errorHandler = function (err) {
  hasVueErrors = true;
  fail(err);
};

27 28
Vue.use(VueResource);

29
// enable test fixtures
30 31
jasmine.getFixtures().fixturesPath = '/base/spec/javascripts/fixtures';
jasmine.getJSONFixtures().fixturesPath = '/base/spec/javascripts/fixtures';
32

33 34
// globalize common libraries
window.$ = window.jQuery = $;
35 36

// stub expected globals
37
window.gl = window.gl || {};
38 39
window.gl.TEST_HOST = 'http://test.host';
window.gon = window.gon || {};
40

41 42 43 44 45 46 47 48
let hasUnhandledPromiseRejections = false;

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

49 50 51 52 53 54
// 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.
55 56 57 58 59 60 61 62
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();
});
63

64 65
const axiosDefaultAdapter = getDefaultAdapter();

66 67 68 69 70 71
// render all of our tests
const testsContext = require.context('.', true, /_spec$/);
testsContext.keys().forEach(function (path) {
  try {
    testsContext(path);
  } catch (err) {
72 73 74 75 76 77
    console.error('[ERROR] Unable to load spec: ', path);
    describe('Test bundle', function () {
      it(`includes '${path}'`, function () {
        expect(err).toBeNull();
      });
    });
78 79
  }
});
80

Winnie Hellmann committed
81 82
describe('test errors', () => {
  beforeAll((done) => {
83
    if (hasUnhandledPromiseRejections || hasVueWarnings || hasVueErrors) {
Winnie Hellmann committed
84 85 86 87 88 89 90 91 92 93 94 95 96
      setTimeout(done, 1000);
    } else {
      done();
    }
  });

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

  it('has no Vue warnings', () => {
    expect(hasVueWarnings).toBe(false);
  });
97 98 99 100

  it('has no Vue error', () => {
    expect(hasVueErrors).toBe(false);
  });
101 102 103 104 105 106

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

109 110 111 112 113
// 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
114
  const troubleMakers = [
115 116
    './blob_edit/blob_bundle.js',
    './boards/boards_bundle.js',
117
    './cycle_analytics/cycle_analytics_bundle.js',
118 119 120
    './cycle_analytics/components/stage_plan_component.js',
    './cycle_analytics/components/stage_staging_component.js',
    './cycle_analytics/components/stage_test_component.js',
121 122
    './commit/pipelines/pipelines_bundle.js',
    './diff_notes/diff_notes_bundle.js',
123 124
    './diff_notes/components/jump_to_discussion.js',
    './diff_notes/components/resolve_count.js',
125 126 127 128 129 130 131
    './dispatcher.js',
    './environments/environments_bundle.js',
    './filtered_search/filtered_search_bundle.js',
    './graphs/graphs_bundle.js',
    './issuable/time_tracking/time_tracking_bundle.js',
    './main.js',
    './merge_conflicts/merge_conflicts_bundle.js',
132 133
    './merge_conflicts/components/inline_conflict_lines.js',
    './merge_conflicts/components/parallel_conflict_lines.js',
134 135
    './monitoring/monitoring_bundle.js',
    './network/network_bundle.js',
136
    './network/branch_graph.js',
137 138 139 140 141
    './profile/profile_bundle.js',
    './protected_branches/protected_branches_bundle.js',
    './snippet/snippet_bundle.js',
    './terminal/terminal_bundle.js',
    './users/users_bundle.js',
Regis Boudinot committed
142
    './issue_show/index.js',
143 144
  ];

145 146 147 148 149 150 151
  describe('Uncovered files', function () {
    const sourceFiles = require.context('~', true, /\.js$/);
    sourceFiles.keys().forEach(function (path) {
      // ignore if there is a matching spec file
      if (testsContext.keys().indexOf(`${path.replace(/\.js$/, '')}_spec`) > -1) {
        return;
      }
152

153 154 155 156 157 158 159
      it(`includes '${path}'`, function () {
        try {
          sourceFiles(path);
        } catch (err) {
          if (troubleMakers.indexOf(path) === -1) {
            expect(err).toBeNull();
          }
160
        }
161
      });
162 163
    });
  });
164
}