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

feat: Implement unit testing with bundle #835

Merged
merged 3 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, 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
34 changes: 34 additions & 0 deletions unit-testing-config-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { join, relative } = require("path");

module.exports = function ({ appFullPath, projectRoot, angular }) {
// TODO: Consider to use the files property from karma.conf.js
const testFilesRegExp = /tests\/.*\.js/;
const rootPagesRegExp = /(root|page)\.(xml|css|js|ts|scss)/;
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;
}