jest.config.js 3.33 KB
Newer Older
Winnie Hellmann's avatar
Winnie Hellmann committed
1
const IS_EE = require('./config/helpers/is_ee_env');
Winnie Hellmann's avatar
Winnie Hellmann committed
2 3 4

const reporters = ['default'];

5
// To have consistent date time parsing both in local and CI environments we set
6
// the timezone of the Node process. https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/27738
7 8
process.env.TZ = 'GMT';

Winnie Hellmann's avatar
Winnie Hellmann committed
9 10 11 12 13 14 15 16 17
if (process.env.CI) {
  reporters.push([
    'jest-junit',
    {
      output: './junit_jest.xml',
    },
  ]);
}

18 19 20 21
let testMatch = ['<rootDir>/spec/frontend/**/*_spec.js'];
if (IS_EE) {
  testMatch.push('<rootDir>/ee/spec/frontend/**/*_spec.js');
}
22 23 24

// workaround for eslint-import-resolver-jest only resolving in test files
// see https://github.com/JoinColony/eslint-import-resolver-jest#note
25 26
const { filename: parentModuleName } = module.parent;
const isESLint = parentModuleName && parentModuleName.includes('/eslint-import-resolver-jest/');
27 28 29 30
if (isESLint) {
  testMatch = testMatch.map(path => path.replace('_spec.js', ''));
}

31 32 33 34 35 36 37
const moduleNameMapper = {
  '^~(/.*)$': '<rootDir>/app/assets/javascripts$1',
  '^ee_component(/.*)$':
    '<rootDir>/app/assets/javascripts/vue_shared/components/empty_component.js',
  '^ee_else_ce(/.*)$': '<rootDir>/app/assets/javascripts$1',
  '^helpers(/.*)$': '<rootDir>/spec/frontend/helpers$1',
  '^vendor(/.*)$': '<rootDir>/vendor/assets/javascripts$1',
38
  '\\.(jpg|jpeg|png|svg|css)$': '<rootDir>/spec/frontend/__mocks__/file_mock.js',
39 40
  'emojis(/.*).json': '<rootDir>/fixtures/emojis$1.json',
  '^spec/test_constants$': '<rootDir>/spec/frontend/helpers/test_constants',
samdbeckham's avatar
samdbeckham committed
41
  '^jest/(.*)$': '<rootDir>/spec/frontend/$1',
42 43
};

44 45
const collectCoverageFrom = ['<rootDir>/app/assets/javascripts/**/*.{js,vue}'];

46 47 48 49 50 51
if (IS_EE) {
  const rootDirEE = '<rootDir>/ee/app/assets/javascripts$1';
  Object.assign(moduleNameMapper, {
    '^ee(/.*)$': rootDirEE,
    '^ee_component(/.*)$': rootDirEE,
    '^ee_else_ce(/.*)$': rootDirEE,
samdbeckham's avatar
samdbeckham committed
52
    '^ee_jest/(.*)$': '<rootDir>/ee/spec/frontend/$1',
53
  });
54 55

  collectCoverageFrom.push(rootDirEE.replace('$1', '/**/*.{js,vue}'));
56 57
}

Albert Salim's avatar
Albert Salim committed
58 59 60 61 62 63 64 65
const coverageDirectory = () => {
  if (process.env.CI_NODE_INDEX && process.env.CI_NODE_TOTAL) {
    return `<rootDir>/coverage-frontend/jest-${process.env.CI_NODE_INDEX}-${process.env.CI_NODE_TOTAL}`;
  }

  return '<rootDir>/coverage-frontend/';
};

Winnie Hellmann's avatar
Winnie Hellmann committed
66 67
// eslint-disable-next-line import/no-commonjs
module.exports = {
Winnie Hellmann's avatar
Winnie Hellmann committed
68
  clearMocks: true,
69
  testMatch,
Winnie Hellmann's avatar
Winnie Hellmann committed
70
  moduleFileExtensions: ['js', 'json', 'vue'],
71
  moduleNameMapper,
72
  collectCoverageFrom,
Albert Salim's avatar
Albert Salim committed
73
  coverageDirectory: coverageDirectory(),
Winnie Hellmann's avatar
Winnie Hellmann committed
74 75 76 77
  coverageReporters: ['json', 'lcov', 'text-summary', 'clover'],
  cacheDirectory: '<rootDir>/tmp/cache/jest',
  modulePathIgnorePatterns: ['<rootDir>/.yarn-cache/'],
  reporters,
78
  setupFilesAfterEnv: ['<rootDir>/spec/frontend/test_setup.js', 'jest-canvas-mock'],
79
  restoreMocks: true,
Winnie Hellmann's avatar
Winnie Hellmann committed
80
  transform: {
Phil Hughes's avatar
Phil Hughes committed
81
    '^.+\\.(gql|graphql)$': 'jest-transform-graphql',
Winnie Hellmann's avatar
Winnie Hellmann committed
82 83 84
    '^.+\\.js$': 'babel-jest',
    '^.+\\.vue$': 'vue-jest',
  },
85
  transformIgnorePatterns: ['node_modules/(?!(@gitlab/ui|bootstrap-vue|three|monaco-editor)/)'],
Winnie Hellmann's avatar
Winnie Hellmann committed
86
  timers: 'fake',
87
  testEnvironment: '<rootDir>/spec/frontend/environment.js',
Winnie Hellmann's avatar
Winnie Hellmann committed
88 89 90
  testEnvironmentOptions: {
    IS_EE,
  },
Winnie Hellmann's avatar
Winnie Hellmann committed
91
};
92 93 94 95 96 97 98 99 100 101 102 103

const karmaTestFile = process.argv.find(arg => arg.includes('spec/javascripts/'));
if (karmaTestFile) {
  console.error(`
Files in spec/javascripts/ and ee/spec/javascripts need to be run with Karma.
Please use the following command instead:

yarn karma -f ${karmaTestFile}

`);
  process.exit(1);
}