diff --git a/bundle-config-loader.js b/bundle-config-loader.js index 20986c16..cf403453 100644 --- a/bundle-config-loader.js +++ b/bundle-config-loader.js @@ -1,6 +1,14 @@ +const unitTestingConfigLoader = require("./unit-testing-config-loader"); + module.exports = function (source) { this.cacheable(); - const { angular = false, loadCss = true, registerModules = /(root|page)\.(xml|css|js|ts|scss)$/ } = this.query; + const { angular = false, loadCss = true, unitTesting, projectRoot, appFullPath, registerModules = /(root|page)\.(xml|css|js|ts|scss)$/ } = this.query; + + if (unitTesting) { + source = unitTestingConfigLoader({ appFullPath, projectRoot, angular, rootPagesRegExp: registerModules }); + this.callback(null, source); + return; + } const hmr = ` if (module.hot) { diff --git a/templates/webpack.angular.js b/templates/webpack.angular.js index 9e98a093..dd22f5cb 100644 --- a/templates/webpack.angular.js +++ b/templates/webpack.angular.js @@ -47,6 +47,7 @@ module.exports = env => { report, // --env.report sourceMap, // --env.sourceMap hmr, // --env.hmr, + unitTesting, // --env.unitTesting } = env; const externals = nsWebpack.getConvertedExternals(env.externals); @@ -192,6 +193,9 @@ module.exports = env => { options: { angular: true, loadCss: !snapshot, // load the application css if in debug mode + unitTesting, + appFullPath, + projectRoot, } }, ].filter(loader => !!loader) diff --git a/templates/webpack.javascript.js b/templates/webpack.javascript.js index 10ec2ad0..39df050d 100644 --- a/templates/webpack.javascript.js +++ b/templates/webpack.javascript.js @@ -42,6 +42,7 @@ module.exports = env => { report, // --env.report sourceMap, // --env.sourceMap hmr, // --env.hmr, + unitTesting, // --env.unitTesting } = env; const externals = nsWebpack.getConvertedExternals(env.externals); @@ -153,6 +154,9 @@ module.exports = env => { loader: "nativescript-dev-webpack/bundle-config-loader", options: { loadCss: !snapshot, // load the application css if in debug mode + unitTesting, + appFullPath, + projectRoot, } }, ].filter(loader => !!loader) diff --git a/templates/webpack.typescript.js b/templates/webpack.typescript.js index 1a31289f..c3df62a4 100644 --- a/templates/webpack.typescript.js +++ b/templates/webpack.typescript.js @@ -42,6 +42,7 @@ module.exports = env => { report, // --env.report sourceMap, // --env.sourceMap hmr, // --env.hmr, + unitTesting, // --env.unitTesting } = env; const externals = nsWebpack.getConvertedExternals(env.externals); @@ -155,6 +156,9 @@ module.exports = env => { loader: "nativescript-dev-webpack/bundle-config-loader", options: { loadCss: !snapshot, // load the application css if in debug mode + unitTesting, + appFullPath, + projectRoot, } }, ].filter(loader => !!loader) diff --git a/templates/webpack.vue.js b/templates/webpack.vue.js index 81877d18..ece42c8e 100644 --- a/templates/webpack.vue.js +++ b/templates/webpack.vue.js @@ -46,6 +46,7 @@ module.exports = env => { report, // --env.report hmr, // --env.hmr sourceMap, // --env.sourceMap + unitTesting, // --env.unitTesting } = env; const externals = nsWebpack.getConvertedExternals(env.externals); @@ -167,6 +168,9 @@ module.exports = env => { options: { registerPages: true, // applicable only for non-angular apps loadCss: !snapshot, // load the application css if in debug mode + unitTesting, + appFullPath, + projectRoot, }, }, ].filter(loader => Boolean(loader)), diff --git a/unit-testing-config-loader.js b/unit-testing-config-loader.js new file mode 100644 index 00000000..3d257915 --- /dev/null +++ b/unit-testing-config-loader.js @@ -0,0 +1,33 @@ +const { join, relative } = require("path"); + +module.exports = function ({ appFullPath, projectRoot, angular, rootPagesRegExp }) { + // TODO: Consider to use the files property from karma.conf.js + const testFilesRegExp = /tests\/.*\.js/; + const runnerFullPath = join(projectRoot, "node_modules", "nativescript-unit-test-runner"); + const runnerRelativePath = relative(appFullPath, runnerFullPath); + let source = ` + require("tns-core-modules/bundle-entry-points"); + const runnerContext = require.context("${runnerRelativePath}", true, ${rootPagesRegExp}); + global.registerWebpackModules(runnerContext); + `; + + if (angular) { + source += ` + const context = require.context("~/", true, ${testFilesRegExp}); + global.registerWebpackModules(context); + `; + } else { + const registerModules = new RegExp(`(${rootPagesRegExp.source})|(${testFilesRegExp.source})`); + source += ` + const context = require.context("~/", true, ${registerModules}); + global.registerWebpackModules(context); + `; + } + + const runnerEntryPointPath = join(runnerRelativePath, "bundle-app.js"); + source += ` + require("${runnerEntryPointPath}"); + `; + + return source; +} \ No newline at end of file