Skip to content

Instantly share code, notes, and snippets.

@wilgert
Last active August 18, 2019 18:36
Show Gist options
  • Save wilgert/1bfb72db97a53465897f669c29e72916 to your computer and use it in GitHub Desktop.
Save wilgert/1bfb72db97a53465897f669c29e72916 to your computer and use it in GitHub Desktop.
/*
* NB: to get this working you will need to
* 1. copy a test-setup.ts file from one of the apps that uses Jest to the root of your project
* 2. replace YOUR_WORK_SPACE_NAME with the name of your work space on line 68
*/
const path = require('path');
const angularConfig = require('./angular.json');
const excludingE2E = Object.keys(angularConfig.projects)
.map(key => ({ ...angularConfig.projects[key], id: key }))
.filter(project => !project.id.includes('-e2e'));
const onlyJest = excludingE2E.filter(project =>
project.architect.test.builder.includes('jest')
);
const filesBlueprints = [
{
prefix: '',
postfix: '**/*.+(ts|html|json|snap|css|less|sass|scss|jpg|jpeg|gif|png|svg)'
},
{ prefix: '!', postfix: '**/*.spec.ts' }
];
const files = onlyJest.reduce(
(accumulator, project) => [
...accumulator,
...filesBlueprints.map(blueprint =>
path.join(blueprint.prefix, project.sourceRoot, blueprint.postfix)
)
],
['test-setup.ts', 'jest.config.js']
);
const tests = [...onlyJest.map(
({sourceRoot}) => path.join(sourceRoot, '**/*.spec.ts')
), '!apps/*-e2e/**/*.spec.ts'];
const ngxWallabyJest = require('ngx-wallaby-jest');
module.exports = function(wallaby) {
return {
files,
tests,
env: {
type: 'node',
runner: 'node'
},
compilers: {
'**/*.ts?(x)': wallaby.compilers.typeScript({ module: 'commonjs' })
},
preprocessors: {
// This translate templateUrl and styleUrls to the right implementation
// For wallaby
'apps/**/*.component.ts': ngxWallabyJest,
'libs/**/*.component.ts': ngxWallabyJest
},
testFramework: 'jest',
setup: function(wallaby) {
var jestConfig = require('./jest.config');
jestConfig.setupTestFrameworkScriptFile = '<rootDir>/test-setup.js';
jestConfig.moduleNameMapper = {
'@YOUR_WORK_SPACE_NAME/(.*)': `${wallaby.localProjectDir}libs/$1/src`
};
wallaby.testFramework.configure(jestConfig);
}
};
};
@wescopeland
Copy link

With Nx 8, I'm seeing an error similar to this:
Error: Cannot find module '@workspace/lib' from 'foo.js'

Any luck running this on Nx 8?

@wescopeland
Copy link

Just figured it out:

setup: function(wallaby) {
  var jestConfig = require('./jest.config');
  jestConfig.setupTestFrameworkScriptFile = '<rootDir>/test-setup.js';

  jestConfig.moduleNameMapper = {
    '@workspace/(.*)': `${wallaby.localProjectDir}libs/$1/src`
  };

  wallaby.testFramework.configure(jestConfig);
},

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