add coverage report to browser tests

This commit is contained in:
Rene
2021-01-27 01:11:22 +01:00
parent be25d8a712
commit 6f590b2cc4
9 changed files with 117 additions and 42 deletions
+5
View File
@@ -0,0 +1,5 @@
const { globalSetup } = require('jest-playwright-preset');
module.exports = async (jestConfig) => {
await globalSetup(jestConfig);
};
+36
View File
@@ -0,0 +1,36 @@
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const del = require('del');
const { globalTeardown } = require('jest-playwright-preset');
const coverageTempDir = './.nyc_output';
const coverageTempDirFile = 'coverage.json';
const reportDir = './.coverage/browser';
module.exports = async (jestConfig) => {
await globalTeardown(jestConfig);
const { rootDir } = jestConfig;
const coverageTempDirPath = path.resolve(rootDir, coverageTempDir);
const coverageTempFilePath = path.resolve(coverageTempDirPath, coverageTempDirFile);
const reportDirPath = path.resolve(rootDir, reportDir);
if (fs.existsSync(coverageTempFilePath)) {
const coverageReportText = ' COVERAGE ';
console.log('');
console.log(`\x1b[1m\x1b[44m${coverageReportText}\x1b[0m`);
console.log(`Reporting from: "${path.relative(rootDir, coverageTempFilePath)}" in "${path.relative(rootDir, reportDirPath)}"`);
del.sync(reportDirPath);
execSync(`npx nyc report --reporter=lcov --report-dir=${reportDir}`, {
cwd: rootDir,
});
const [deletedTempDir] = del.sync(coverageTempDirPath);
if (deletedTempDir) {
console.log('Deleted:', path.relative(rootDir, deletedTempDir));
}
}
};
+9 -6
View File
@@ -4,16 +4,17 @@ const browserRollupConfig = require('./config/jest-browser.rollup.config.js');
const testServerLoaderPath = path.resolve(__dirname, './config/jest-test-server.loader.js');
const jsdomSetupFile = path.resolve(__dirname, './config/jest-jsdom.setup.js');
const browserGlobalSetupPath = path.resolve(__dirname, './config/jest-browser.globalSetup.js');
const browserGlobalTeardownPath = path.resolve(__dirname, './config/jest-browser.globalTeardown.js');
const browserTestEnvironmentPath = path.resolve(__dirname, './config/jest-browser.env.js');
const browserSetupFile = path.resolve(__dirname, './config/jest-browser.setup.js');
const browserSetupAfterEnvFile = path.resolve(__dirname, './config/jest-browser.setupAfterEnv.js');
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html
const base = {
clearMocks: true,
collectCoverage: true,
coverageDirectory: './.coverage',
coverageDirectory: './.coverage/jsdom',
moduleDirectories: resolve.directories,
moduleFileExtensions: resolve.extensions.map((ext) => ext.replace(/\./, '')),
testPathIgnorePatterns: ['\\\\node_modules\\\\'],
@@ -21,10 +22,13 @@ const base = {
const browserBase = {
...base,
collectCoverage: false,
preset: 'jest-playwright-preset',
setupFilesAfterEnv: [browserSetupFile],
testMatch: ['**/tests/browser/**/*.test.[jt]s?(x)'],
globalSetup: browserGlobalSetupPath,
globalTeardown: browserGlobalTeardownPath,
testEnvironment: browserTestEnvironmentPath,
setupFilesAfterEnv: [browserSetupAfterEnvFile],
testMatch: ['**/tests/browser/**/*.test.[jt]s?(x)'],
coveragePathIgnorePatterns: ['/node_modules/', `/${browserRollupConfig.build}/`],
transform: {
'^.+\\.[jt]sx?$': 'babel-jest',
@@ -54,7 +58,6 @@ module.exports = {
name: 'browser-dev',
color: 'white',
},
collectCoverage: false,
},
],
};
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
-13
View File
@@ -1,13 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OverlayScrollbars</title>
</head>
<body>
<div id="hi">hi</div>
<script type="text/javascript" src="./dist_/overlayscrollbars.js"></script>
</body>
</html>
+2 -2
View File
@@ -6,8 +6,8 @@
"scripts": {
"test": "jest --coverage --runInBand --detectOpenHandles",
"test:jsdom": "jest --coverage --runInBand --detectOpenHandles --selectProjects jsdom --testPathPattern",
"test:browser": "jest --coverage --runInBand --detectOpenHandles --selectProjects browser --testPathPattern",
"test:browser-dev": "jest --coverage --runInBand --detectOpenHandles --selectProjects browser-dev --testPathPattern",
"test:browser": "jest --runInBand --detectOpenHandles --selectProjects browser --testPathPattern",
"test:browser-dev": "jest --runInBand --detectOpenHandles --selectProjects browser-dev --testPathPattern",
"build": "rollup -c"
}
}
+63 -19
View File
@@ -1,5 +1,5 @@
const { nodeResolve: rollupResolve } = require('@rollup/plugin-node-resolve');
const { babel: rollupBabelPlugin } = require('@rollup/plugin-babel');
const { babel: rollupBabelInputPlugin, createBabelInputPluginFactory } = require('@rollup/plugin-babel');
const { terser: rollupTerser } = require('rollup-plugin-terser');
const rollupInject = require('@rollup/plugin-inject');
const rollupCommonjs = require('@rollup/plugin-commonjs');
@@ -25,10 +25,8 @@ const rollupConfigDefaults = {
exports: 'auto',
pipeline: ['typescript', 'resolve', 'inject', 'commonjs', 'babel'],
};
const legacyBabelConfig = {
exclude: isTestEnv ? [/\/core-js\//] : [], // /\/@testing-library\//
plugins: isTestEnv ? ['babel-plugin-istanbul'] : [],
presets: [
[
'@babel/preset-env',
@@ -42,7 +40,6 @@ const legacyBabelConfig = {
],
],
};
const esmBabelConfig = {
presets: [
[
@@ -58,14 +55,43 @@ const esmBabelConfig = {
],
};
// workaround for https://github.com/rollup/plugins/issues/774
const rollupBabelIstanbulSourceMaps = {};
const rollupBabelIstanbulGenerateKey = (filename) => (filename ? filename.replace(/[\\/]+/g, '') : '');
const rollupBabelPlugin = isTestEnv
? createBabelInputPluginFactory(() => {
return {
config(cfg) {
const { options } = cfg;
const { filename, plugins } = options;
const istanbulSourceMap = rollupBabelIstanbulSourceMaps[rollupBabelIstanbulGenerateKey(filename)];
if (istanbulSourceMap) {
return {
...options,
plugins: [
...(plugins || []),
[
'babel-plugin-istanbul',
{
useInlineSourceMaps: false,
inputSourceMap: istanbulSourceMap,
},
],
],
};
} else {
return options;
}
},
};
})
: rollupBabelInputPlugin;
const normalizePath = (pathName) => (pathName ? pathName.split(path.sep).join(path.posix.sep) : pathName);
const appendExtension = (file) => {
if (path.extname(file) === '') {
return file + resolve.extensions.find((ext) => fs.existsSync(path.resolve(`${file}${ext}`)));
}
return file;
};
const appendExtension = (file) =>
path.extname(file) === '' ? file + resolve.extensions.find((ext) => fs.existsSync(path.resolve(`${file}${ext}`))) : file;
const resolvePath = (basePath, pathToResolve, appendExt) => {
const result = pathToResolve ? (path.isAbsolute(pathToResolve) ? pathToResolve : path.resolve(basePath, pathToResolve)) : null;
@@ -185,14 +211,28 @@ const rollupConfig = (config = {}, { project = process.cwd(), overwrite = {}, si
sourceMap: sourcemap,
extensions: resolve.extensions,
}),
babel: rollupBabelPlugin({
...(esm ? esmBabelConfig : legacyBabelConfig),
babelHelpers: 'runtime',
extensions: resolve.extensions,
caller: {
name: 'babel-rollup-build',
},
}),
babel: [
isTestEnv
? {
name: 'collect-bable-plugin-istanbul-input-source-maps',
transform(code, filename) {
rollupBabelIstanbulSourceMaps[rollupBabelIstanbulGenerateKey(filename)] = { ...this.getCombinedSourcemap() };
return {
code,
map: null,
};
},
}
: {},
rollupBabelPlugin({
...(esm ? esmBabelConfig : legacyBabelConfig),
babelHelpers: 'runtime',
extensions: resolve.extensions,
caller: {
name: 'babel-rollup-build',
},
}),
],
};
const output = genOutputConfig(esm);
@@ -222,7 +262,11 @@ const rollupConfig = (config = {}, { project = process.cwd(), overwrite = {}, si
: []
),
external: [...Object.keys(devDependencies), ...Object.keys(peerDependencies), ...((Array.isArray(external) && external) || [])],
plugins: pipeline.map((item) => (typeof item === 'string' ? pipelineMap[item] : item)),
plugins: pipeline.reduce((arr, item) => {
const plugin = typeof item === 'string' ? pipelineMap[item] : item;
arr.push(...(Array.isArray(plugin) ? plugin : [plugin]));
return arr;
}, []),
};
};