Skip to content

Instantly share code, notes, and snippets.

@wilgert
Last active January 26, 2020 15:11
Show Gist options
  • Save wilgert/497ce07976d2942acf608cf13e943389 to your computer and use it in GitHub Desktop.
Save wilgert/497ce07976d2942acf608cf13e943389 to your computer and use it in GitHub Desktop.
Dynamic Wallaby config that runs Karma/Jasmine unit test for all apps and libs in your Nx workspace that use it as a testrunner. Requires a wallabyTest.ts in root of the workspace.
var wallabyWebpack = require('wallaby-webpack');
var path = require('path');
const angularConfig = require('./angular.json');
var compilerOptions = Object.assign(
require('./tsconfig.json').compilerOptions,
require('./apps/frontend/tsconfig.spec.json').compilerOptions
);
compilerOptions.module = 'CommonJs';
const onlyKarma = Object.keys(angularConfig.projects)
.filter(key => !key.includes('-e2e'))
.map(key => ({ ...angularConfig.projects[key] }))
.filter(project => project.architect.test.builder.includes('karma'));
const webpackPostprocessorEntryPatterns = [
'wallabyTest.js',
...onlyKarma.map(project => path.join(project.sourceRoot, '**', '*spec.js'))
];
const resolveModules = onlyKarma.reduce(
(accumulator, project) => [
path.join(wallaby.projectCacheDir, project.sourceRoot),
path.join(project.sourceRoot, 'app'),
...accumulator
],
['node_modules']
);
const filesBluePrints = [
{
pattern: '**/*.+(ts|css|less|scss|sass|styl|html|json|svg)',
load: false
},
{
pattern: '**/*.d.ts',
ignore: true
},
{
pattern: '**/*spec.ts',
ignore: true
}
];
const files = onlyKarma.reduce(
(accumulator, project) => [
...accumulator,
...filesBluePrints.map(blueprint => ({
...blueprint,
pattern: path.join(project.sourceRoot, blueprint.pattern)
}))
],
[{ pattern: 'wallabyTest.ts', load: false }]
);
const tests = onlyKarma.map(project => ({
pattern: path.join(project.sourceRoot, '**/*spec.ts'),
load: false
}));
module.exports = function(wallaby) {
var webpackPostprocessor = wallabyWebpack({
entryPatterns: webpackPostprocessorEntryPatterns,
module: {
rules: [
{ test: /\.css$/, loader: ['raw-loader'] },
{ test: /\.html$/, loader: 'raw-loader' },
{
test: /\.ts$/,
loader: '@ngtools/webpack',
include: /node_modules/,
query: { tsConfigPath: 'tsconfig.json' }
},
{
test: /\.js$/,
loader: 'angular2-template-loader',
exclude: /node_modules/
},
{ test: /\.styl$/, loaders: ['raw-loader', 'stylus-loader'] },
{
test: /\.less$/,
loaders: [
'raw-loader',
{ loader: 'less-loader', options: { paths: [__dirname] } }
]
},
{ test: /\.scss$|\.sass$/, loaders: ['raw-loader', 'sass-loader'] },
{ test: /\.(jpg|png|svg)$/, loader: 'url-loader?limit=128000' }
]
},
resolve: {
extensions: ['.js', '.ts'],
modules: resolveModules
},
node: {
fs: 'empty',
net: 'empty',
tls: 'empty',
dns: 'empty'
}
});
return {
files,
tests,
testFramework: 'jasmine',
compilers: {
'**/*.ts': wallaby.compilers.typeScript(compilerOptions)
},
env: {
kind: 'electron',
options: {
webPreferences: {
nodeIntegration: true
}
}
},
postprocessor: webpackPostprocessor,
setup: function() {
window.__moduleBundler.loadTests();
},
debug: true
};
};
@allhandsondeck
Copy link

allhandsondeck commented Jan 26, 2020

const resolveModules = onlyKarma.reduce( (accumulator, project) => [ path.join(wallaby.projectCacheDir, project.sourceRoot), path.join(project.sourceRoot, 'app'), ...accumulator ], ['node_modules'] );

should be inside

module.exports = function(wallaby) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment