Skip to content
This repository has been archived by the owner on Aug 7, 2021. It is now read-only.

Commit

Permalink
feat: Implement unit testing with bundle (#835)
Browse files Browse the repository at this point in the history
* feat: Implement unit testing with bundle

NativeScript/nativescript-cli#4392

* chore: share the regex for `root|page` between bundle-config loader and unit-testing config loader
  • Loading branch information
Fatme authored Mar 19, 2019
1 parent 36824b1 commit 7944611
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
10 changes: 9 additions & 1 deletion bundle-config-loader.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions templates/webpack.angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions templates/webpack.javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions templates/webpack.typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions templates/webpack.vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)),
Expand Down
33 changes: 33 additions & 0 deletions unit-testing-config-loader.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 7944611

Please sign in to comment.