|
@@ -1,21 +1,35 @@
|
|
|
import path = require('path');
|
|
import path = require('path');
|
|
|
import fs from 'fs';
|
|
import fs from 'fs';
|
|
|
|
|
|
|
|
-const whitelistedJestConfigOverrides = ['snapshotSerializers', 'moduleNameMapper'];
|
|
|
|
|
|
|
+export const allowedJestConfigOverrides = ['snapshotSerializers', 'moduleNameMapper'];
|
|
|
|
|
|
|
|
-export const jestConfig = () => {
|
|
|
|
|
- const jestConfigOverrides = require(path.resolve(process.cwd(), 'package.json')).jest;
|
|
|
|
|
- const blacklistedOverrides = jestConfigOverrides
|
|
|
|
|
- ? Object.keys(jestConfigOverrides).filter(override => whitelistedJestConfigOverrides.indexOf(override) === -1)
|
|
|
|
|
|
|
+interface EnabledJestConfigOverrides {
|
|
|
|
|
+ snapshotSerializers: string[];
|
|
|
|
|
+ moduleNameMapper: { [key: string]: string };
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export const jestConfig = (baseDir: string = process.cwd()) => {
|
|
|
|
|
+ const jestConfigOverrides = (require(path.resolve(baseDir, 'package.json')).jest || {}) as EnabledJestConfigOverrides;
|
|
|
|
|
+
|
|
|
|
|
+ const deniedOverrides = jestConfigOverrides
|
|
|
|
|
+ ? Object.keys(jestConfigOverrides).filter(override => allowedJestConfigOverrides.indexOf(override) === -1)
|
|
|
: [];
|
|
: [];
|
|
|
- if (blacklistedOverrides.length > 0) {
|
|
|
|
|
- console.error("\ngrafana-toolkit doesn't support following Jest options: ", blacklistedOverrides);
|
|
|
|
|
- console.log('Supported Jest options are: ', JSON.stringify(whitelistedJestConfigOverrides));
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (deniedOverrides.length > 0) {
|
|
|
|
|
+ console.error("\ngrafana-toolkit doesn't support following Jest options: ", deniedOverrides);
|
|
|
|
|
+ console.log('Supported Jest options are: ', JSON.stringify(allowedJestConfigOverrides));
|
|
|
throw new Error('Provided Jest config is not supported');
|
|
throw new Error('Provided Jest config is not supported');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const shimsFilePath = path.resolve(process.cwd(), 'config/jest-shim.ts');
|
|
|
|
|
- const setupFilePath = path.resolve(process.cwd(), 'config/jest-setup.ts');
|
|
|
|
|
|
|
+ const shimsFilePath = path.resolve(baseDir, 'config/jest-shim.ts');
|
|
|
|
|
+ const setupFilePath = path.resolve(baseDir, 'config/jest-setup.ts');
|
|
|
|
|
+
|
|
|
|
|
+ // Mock css imports for tests. Otherwise Jest will have troubles understanding SASS/CSS imports
|
|
|
|
|
+ const { moduleNameMapper, ...otherOverrides } = jestConfigOverrides;
|
|
|
|
|
+ const moduleNameMapperConfig = {
|
|
|
|
|
+ '\\.(css|sass|scss)$': `${__dirname}/styles.mock.js`,
|
|
|
|
|
+ ...moduleNameMapper,
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
const setupFile = fs.existsSync(setupFilePath) ? setupFilePath : undefined;
|
|
const setupFile = fs.existsSync(setupFilePath) ? setupFilePath : undefined;
|
|
|
const shimsFile = fs.existsSync(shimsFilePath) ? shimsFilePath : undefined;
|
|
const shimsFile = fs.existsSync(shimsFilePath) ? shimsFilePath : undefined;
|
|
@@ -29,7 +43,6 @@ export const jestConfig = () => {
|
|
|
globals: { 'ts-jest': { isolatedModules: true } },
|
|
globals: { 'ts-jest': { isolatedModules: true } },
|
|
|
coverageReporters: ['json-summary', 'text', 'lcov'],
|
|
coverageReporters: ['json-summary', 'text', 'lcov'],
|
|
|
collectCoverageFrom: ['src/**/*.{ts,tsx}', '!**/node_modules/**', '!**/vendor/**'],
|
|
collectCoverageFrom: ['src/**/*.{ts,tsx}', '!**/node_modules/**', '!**/vendor/**'],
|
|
|
-
|
|
|
|
|
testMatch: [
|
|
testMatch: [
|
|
|
'<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}',
|
|
'<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}',
|
|
|
'<rootDir>/src/**/*.{spec,test,jest}.{js,jsx,ts,tsx}',
|
|
'<rootDir>/src/**/*.{spec,test,jest}.{js,jsx,ts,tsx}',
|
|
@@ -38,10 +51,11 @@ export const jestConfig = () => {
|
|
|
'[/\\\\\\\\]node_modules[/\\\\\\\\].+\\\\.(js|jsx|ts|tsx)$',
|
|
'[/\\\\\\\\]node_modules[/\\\\\\\\].+\\\\.(js|jsx|ts|tsx)$',
|
|
|
'^.+\\\\.module\\\\.(css|sass|scss)$',
|
|
'^.+\\\\.module\\\\.(css|sass|scss)$',
|
|
|
],
|
|
],
|
|
|
|
|
+ moduleNameMapper: moduleNameMapperConfig,
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
return {
|
|
|
...defaultJestConfig,
|
|
...defaultJestConfig,
|
|
|
- ...jestConfigOverrides,
|
|
|
|
|
|
|
+ ...otherOverrides,
|
|
|
};
|
|
};
|
|
|
};
|
|
};
|