Skip to content

Commit

Permalink
fix(jest mock): Add stub for static content
Browse files Browse the repository at this point in the history
Fix bug for unit testing component dom that includes static content like a sass file.
Include jest-tranform-stub.

Resolves aurelia/testing#67
  • Loading branch information
michaelw85 committed Mar 8, 2019
1 parent 116d6d7 commit f7cb875
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 76 deletions.
121 changes: 45 additions & 76 deletions lib/commands/new/buildsystems/general/unit-test-runners/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
const ProjectItem = require('../../../../../project-item').ProjectItem;

module.exports = function(project) {
let configureJasmine = require('./jasmine');
const isUsingBabel = project.model.transpiler.id === 'babel';
const isUsingTS = project.model.transpiler.id === 'typescript';
const configureJasmine = require('./jasmine');

configureJasmine(project);

project.addToTasks(
Expand All @@ -15,94 +18,60 @@ module.exports = function(project) {
).addToDevDependencies(
'jest',
'jest-cli',
'jest-transform-stub',
'aurelia-loader-nodejs',
'aurelia-pal-nodejs'
).addNPMScript('test', 'au jest');

if (project.model.transpiler.id === 'babel') {
const transform = {
'^.+\\.(css|less|sass|scss|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': 'jest-transform-stub'
};

const moduleFileExtensions = ['js', 'json'];
const setupFiles = [];
const collectCoverageFrom = [ '!**/node_modules/**', '!**/test/**' ];
let testRegex = '\\.spec\\.';

if (isUsingBabel) {
project.addToDevDependencies(
'babel-jest',
'[email protected]'
);

project.package.jest = {
moduleNameMapper: {
// avoid aurelia-bindings v1+v2 conflict
'^aurelia-binding$': '<rootDir>/node_modules/aurelia-binding'
},
modulePaths: [
'<rootDir>/src',
'<rootDir>/node_modules'
],
moduleFileExtensions: [
'js',
'json'
],
transform: {
'^.+\\.js$': 'babel-jest'
},
testRegex: '\\.spec\\.js$',
setupFiles: [
'<rootDir>/test/jest-pretest.js'
],
testEnvironment: 'node',
collectCoverage: true,
collectCoverageFrom: [
'src/**/*.js',
'!**/*.spec.js',
'!**/node_modules/**',
'!**/test/**'
],
coverageDirectory: '<rootDir>/test/coverage-jest',
coverageReporters: [
'json',
'lcov',
'text',
'html'
]
};
} else if (project.model.transpiler.id === 'typescript') {
transform['^.+\\.js$'] = 'babel-jest';
testRegex += 'js$';
setupFiles.push('<rootDir>/test/jest-pretest.js');
collectCoverageFrom.push( 'src/**/*.js', '!**/*.spec.js');
} else if (isUsingTS) {
project.addToDevDependencies(
'ts-jest',
'@types/jest'
);

project.package.jest = {
moduleNameMapper: {
// avoid aurelia-bindings v1+v2 conflict
'^aurelia-binding$': '<rootDir>/node_modules/aurelia-binding'
},
modulePaths: [
'<rootDir>/src',
'<rootDir>/node_modules'
],
moduleFileExtensions: [
'js',
'json',
'ts'
],
transform: {
'^.+\\.ts$': 'ts-jest'
},
testRegex: '\\.spec\\.(ts|js)$',
setupFiles: [
'<rootDir>/test/jest-pretest.ts'
],
testEnvironment: 'node',
collectCoverage: true,
collectCoverageFrom: [
'src/**/*.{js,ts}',
'!**/*.spec.{js,ts}',
'!**/node_modules/**',
'!**/test/**'
],
coverageDirectory: '<rootDir>/test/coverage-jest',
coverageReporters: [
'json',
'lcov',
'text',
'html'
]
};
transform['^.+\\.ts$'] = 'ts-jest';
moduleFileExtensions.push('ts');
testRegex += '(ts|js)$';
setupFiles.push('<rootDir>/test/jest-pretest.ts');
collectCoverageFrom.push( 'src/**/*.{js,ts}', '!**/*.spec.{js,ts}');
}

project.package.jest = {
moduleNameMapper: {
// avoid aurelia-bindings v1+v2 conflict
'^aurelia-binding$': '<rootDir>/node_modules/aurelia-binding'
},
modulePaths: [
'<rootDir>/src',
'<rootDir>/node_modules'
],
moduleFileExtensions,
transform,
testRegex,
setupFiles,
testEnvironment: 'node',
collectCoverage: true,
collectCoverageFrom,
coverageDirectory: '<rootDir>/test/coverage-jest',
coverageReporters: [ 'json', 'lcov', 'text', 'html']
};
};
1 change: 1 addition & 0 deletions lib/dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"jest-jasmine2": "^23.6.0",
"jest-cli": "^23.6.0",
"jest-matchers": "^20.0.3",
"jest-transform-stub": "^2.0.0",
"json-loader": "^0.5.7",
"html-loader": "^0.5.5",
"karma": "^3.0.0",
Expand Down

0 comments on commit f7cb875

Please sign in to comment.