Skip to content

Instantly share code, notes, and snippets.

@wilgert wilgert/wallaby.jest.js

Last active Aug 18, 2019
Embed
What would you like to do?
/*
* 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);
}
};
};
@wilgert

This comment has been minimized.

Copy link
Owner Author

wilgert commented Mar 19, 2019

NB: to get this working you will need to copy a test-setup.ts file from one of the libs that uses Jest to the root of your project.

@wescopeland

This comment has been minimized.

Copy link

wescopeland commented May 31, 2019

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

This comment has been minimized.

Copy link

wescopeland commented May 31, 2019

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
You can’t perform that action at this time.