2020-05-15 17:08:21 -04:00
|
|
|
const IS_EE = require('./config/helpers/is_ee_env');
|
|
|
|
const isESLint = require('./config/helpers/is_eslint');
|
2021-08-30 05:09:12 -04:00
|
|
|
const IS_JH = require('./config/helpers/is_jh_env');
|
2020-05-15 17:08:21 -04:00
|
|
|
|
2021-03-23 14:09:05 -04:00
|
|
|
module.exports = (path, options = {}) => {
|
|
|
|
const {
|
|
|
|
moduleNameMapper: extModuleNameMapper = {},
|
|
|
|
moduleNameMapperEE: extModuleNameMapperEE = {},
|
2021-08-30 05:09:12 -04:00
|
|
|
moduleNameMapperJH: extModuleNameMapperJH = {},
|
2021-03-23 14:09:05 -04:00
|
|
|
} = options;
|
|
|
|
|
2020-05-15 17:08:21 -04:00
|
|
|
const reporters = ['default'];
|
|
|
|
|
|
|
|
// To have consistent date time parsing both in local and CI environments we set
|
|
|
|
// the timezone of the Node process. https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/27738
|
|
|
|
process.env.TZ = 'GMT';
|
|
|
|
|
|
|
|
if (process.env.CI) {
|
|
|
|
reporters.push([
|
|
|
|
'jest-junit',
|
|
|
|
{
|
2020-10-07 17:08:21 -04:00
|
|
|
outputName: './junit_jest.xml',
|
2020-10-14 05:08:46 -04:00
|
|
|
addFileAttribute: 'true',
|
2020-05-15 17:08:21 -04:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const glob = `${path}/**/*_spec.js`;
|
|
|
|
let testMatch = [`<rootDir>/${glob}`];
|
|
|
|
if (IS_EE) {
|
|
|
|
testMatch.push(`<rootDir>/ee/${glob}`);
|
|
|
|
}
|
|
|
|
|
2021-08-30 05:09:12 -04:00
|
|
|
if (IS_JH) {
|
|
|
|
testMatch.push(`<rootDir>/jh/${glob}`);
|
|
|
|
}
|
2020-05-15 17:08:21 -04:00
|
|
|
// workaround for eslint-import-resolver-jest only resolving in test files
|
|
|
|
// see https://github.com/JoinColony/eslint-import-resolver-jest#note
|
|
|
|
if (isESLint(module)) {
|
2021-03-10 10:09:11 -05:00
|
|
|
testMatch = testMatch.map((modulePath) => modulePath.replace('_spec.js', ''));
|
2020-05-15 17:08:21 -04:00
|
|
|
}
|
|
|
|
|
2020-11-25 01:09:20 -05:00
|
|
|
const TEST_FIXTURES_PATTERN = 'test_fixtures(/.*)$';
|
|
|
|
|
2020-05-15 17:08:21 -04:00
|
|
|
const moduleNameMapper = {
|
|
|
|
'^~(/.*)$': '<rootDir>/app/assets/javascripts$1',
|
|
|
|
'^ee_component(/.*)$':
|
|
|
|
'<rootDir>/app/assets/javascripts/vue_shared/components/empty_component.js',
|
2021-08-30 05:09:12 -04:00
|
|
|
'^jh_component(/.*)$':
|
|
|
|
'<rootDir>/app/assets/javascripts/vue_shared/components/empty_component.js',
|
2020-10-13 08:08:41 -04:00
|
|
|
'^shared_queries(/.*)$': '<rootDir>/app/graphql/queries$1',
|
2020-05-15 17:08:21 -04:00
|
|
|
'^ee_else_ce(/.*)$': '<rootDir>/app/assets/javascripts$1',
|
2021-08-30 05:09:12 -04:00
|
|
|
'^jh_else_ce(/.*)$': '<rootDir>/app/assets/javascripts$1',
|
2021-01-14 19:10:45 -05:00
|
|
|
'^helpers(/.*)$': '<rootDir>/spec/frontend/__helpers__$1',
|
2020-05-15 17:08:21 -04:00
|
|
|
'^vendor(/.*)$': '<rootDir>/vendor/assets/javascripts$1',
|
2020-11-25 01:09:20 -05:00
|
|
|
[TEST_FIXTURES_PATTERN]: '<rootDir>/tmp/tests/frontend/fixtures$1',
|
2020-05-15 17:08:21 -04:00
|
|
|
'\\.(jpg|jpeg|png|svg|css)$': '<rootDir>/spec/frontend/__mocks__/file_mock.js',
|
|
|
|
'emojis(/.*).json': '<rootDir>/fixtures/emojis$1.json',
|
2021-01-14 19:10:45 -05:00
|
|
|
'^spec/test_constants$': '<rootDir>/spec/frontend/__helpers__/test_constants',
|
2020-05-15 17:08:21 -04:00
|
|
|
'^jest/(.*)$': '<rootDir>/spec/frontend/$1',
|
2021-03-23 20:09:26 -04:00
|
|
|
'^jquery$': '<rootDir>/node_modules/jquery/dist/jquery.slim.js',
|
2021-03-23 14:09:05 -04:00
|
|
|
...extModuleNameMapper,
|
2020-05-15 17:08:21 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const collectCoverageFrom = ['<rootDir>/app/assets/javascripts/**/*.{js,vue}'];
|
|
|
|
|
|
|
|
if (IS_EE) {
|
|
|
|
const rootDirEE = '<rootDir>/ee/app/assets/javascripts$1';
|
|
|
|
Object.assign(moduleNameMapper, {
|
|
|
|
'^ee(/.*)$': rootDirEE,
|
|
|
|
'^ee_component(/.*)$': rootDirEE,
|
|
|
|
'^ee_else_ce(/.*)$': rootDirEE,
|
|
|
|
'^ee_jest/(.*)$': '<rootDir>/ee/spec/frontend/$1',
|
2020-11-25 01:09:20 -05:00
|
|
|
[TEST_FIXTURES_PATTERN]: '<rootDir>/tmp/tests/frontend/fixtures-ee$1',
|
2021-03-23 14:09:05 -04:00
|
|
|
...extModuleNameMapperEE,
|
2020-05-15 17:08:21 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
collectCoverageFrom.push(rootDirEE.replace('$1', '/**/*.{js,vue}'));
|
|
|
|
}
|
|
|
|
|
2021-08-30 05:09:12 -04:00
|
|
|
if (IS_JH) {
|
|
|
|
const rootDirJH = '<rootDir>/jh/app/assets/javascripts$1';
|
|
|
|
Object.assign(moduleNameMapper, {
|
|
|
|
'^jh(/.*)$': rootDirJH,
|
|
|
|
'^jh_component(/.*)$': rootDirJH,
|
|
|
|
'^jh_else_ce(/.*)$': rootDirJH,
|
|
|
|
'^jh_jest/(.*)$': '<rootDir>/jh/spec/frontend/$1',
|
|
|
|
...extModuleNameMapperJH,
|
|
|
|
});
|
|
|
|
|
|
|
|
collectCoverageFrom.push(rootDirJH.replace('$1', '/**/*.{js,vue}'));
|
|
|
|
}
|
|
|
|
|
2020-05-15 17:08:21 -04:00
|
|
|
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/';
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
clearMocks: true,
|
|
|
|
testMatch,
|
2021-09-20 11:12:39 -04:00
|
|
|
moduleFileExtensions: ['js', 'json', 'vue', 'gql', 'graphql'],
|
2020-05-15 17:08:21 -04:00
|
|
|
moduleNameMapper,
|
|
|
|
collectCoverageFrom,
|
|
|
|
coverageDirectory: coverageDirectory(),
|
|
|
|
coverageReporters: ['json', 'lcov', 'text-summary', 'clover'],
|
2021-04-08 11:09:06 -04:00
|
|
|
// We need ignore _worker code coverage since we are manually transforming it
|
|
|
|
coveragePathIgnorePatterns: ['<rootDir>/node_modules/', '_worker\\.js$'],
|
2020-05-15 17:08:21 -04:00
|
|
|
cacheDirectory: '<rootDir>/tmp/cache/jest',
|
|
|
|
modulePathIgnorePatterns: ['<rootDir>/.yarn-cache/'],
|
|
|
|
reporters,
|
2020-08-06 17:10:15 -04:00
|
|
|
setupFilesAfterEnv: [`<rootDir>/${path}/test_setup.js`, 'jest-canvas-mock'],
|
2020-05-15 17:08:21 -04:00
|
|
|
restoreMocks: true,
|
|
|
|
transform: {
|
|
|
|
'^.+\\.(gql|graphql)$': 'jest-transform-graphql',
|
2021-04-08 11:09:06 -04:00
|
|
|
'^.+_worker\\.js$': './spec/frontend/__helpers__/web_worker_transformer.js',
|
2020-05-15 17:08:21 -04:00
|
|
|
'^.+\\.js$': 'babel-jest',
|
|
|
|
'^.+\\.vue$': 'vue-jest',
|
2020-11-25 01:09:20 -05:00
|
|
|
'^.+\\.(md|zip|png)$': 'jest-raw-loader',
|
2020-05-15 17:08:21 -04:00
|
|
|
},
|
2020-07-07 11:08:49 -04:00
|
|
|
transformIgnorePatterns: [
|
2021-09-23 02:12:14 -04:00
|
|
|
'node_modules/(?!(@gitlab/ui|@gitlab/favicon-overlay|bootstrap-vue|three|monaco-editor|monaco-yaml|fast-mersenne-twister|prosemirror-markdown|dateformat)/)',
|
2020-07-07 11:08:49 -04:00
|
|
|
],
|
2020-05-15 17:08:21 -04:00
|
|
|
timers: 'fake',
|
|
|
|
testEnvironment: '<rootDir>/spec/frontend/environment.js',
|
|
|
|
testEnvironmentOptions: {
|
|
|
|
IS_EE,
|
2021-08-30 05:09:12 -04:00
|
|
|
IS_JH,
|
2020-05-15 17:08:21 -04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|