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

Commit 7944611

Browse files
author
Fatme
authored
feat: Implement unit testing with bundle (#835)
* 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
1 parent 36824b1 commit 7944611

6 files changed

+58
-1
lines changed

Diff for: bundle-config-loader.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
const unitTestingConfigLoader = require("./unit-testing-config-loader");
2+
13
module.exports = function (source) {
24
this.cacheable();
3-
const { angular = false, loadCss = true, registerModules = /(root|page)\.(xml|css|js|ts|scss)$/ } = this.query;
5+
const { angular = false, loadCss = true, unitTesting, projectRoot, appFullPath, registerModules = /(root|page)\.(xml|css|js|ts|scss)$/ } = this.query;
6+
7+
if (unitTesting) {
8+
source = unitTestingConfigLoader({ appFullPath, projectRoot, angular, rootPagesRegExp: registerModules });
9+
this.callback(null, source);
10+
return;
11+
}
412

513
const hmr = `
614
if (module.hot) {

Diff for: templates/webpack.angular.js

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module.exports = env => {
4747
report, // --env.report
4848
sourceMap, // --env.sourceMap
4949
hmr, // --env.hmr,
50+
unitTesting, // --env.unitTesting
5051
} = env;
5152

5253
const externals = nsWebpack.getConvertedExternals(env.externals);
@@ -192,6 +193,9 @@ module.exports = env => {
192193
options: {
193194
angular: true,
194195
loadCss: !snapshot, // load the application css if in debug mode
196+
unitTesting,
197+
appFullPath,
198+
projectRoot,
195199
}
196200
},
197201
].filter(loader => !!loader)

Diff for: templates/webpack.javascript.js

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module.exports = env => {
4242
report, // --env.report
4343
sourceMap, // --env.sourceMap
4444
hmr, // --env.hmr,
45+
unitTesting, // --env.unitTesting
4546
} = env;
4647
const externals = nsWebpack.getConvertedExternals(env.externals);
4748

@@ -153,6 +154,9 @@ module.exports = env => {
153154
loader: "nativescript-dev-webpack/bundle-config-loader",
154155
options: {
155156
loadCss: !snapshot, // load the application css if in debug mode
157+
unitTesting,
158+
appFullPath,
159+
projectRoot,
156160
}
157161
},
158162
].filter(loader => !!loader)

Diff for: templates/webpack.typescript.js

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module.exports = env => {
4242
report, // --env.report
4343
sourceMap, // --env.sourceMap
4444
hmr, // --env.hmr,
45+
unitTesting, // --env.unitTesting
4546
} = env;
4647
const externals = nsWebpack.getConvertedExternals(env.externals);
4748

@@ -155,6 +156,9 @@ module.exports = env => {
155156
loader: "nativescript-dev-webpack/bundle-config-loader",
156157
options: {
157158
loadCss: !snapshot, // load the application css if in debug mode
159+
unitTesting,
160+
appFullPath,
161+
projectRoot,
158162
}
159163
},
160164
].filter(loader => !!loader)

Diff for: templates/webpack.vue.js

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module.exports = env => {
4646
report, // --env.report
4747
hmr, // --env.hmr
4848
sourceMap, // --env.sourceMap
49+
unitTesting, // --env.unitTesting
4950
} = env;
5051

5152
const externals = nsWebpack.getConvertedExternals(env.externals);
@@ -167,6 +168,9 @@ module.exports = env => {
167168
options: {
168169
registerPages: true, // applicable only for non-angular apps
169170
loadCss: !snapshot, // load the application css if in debug mode
171+
unitTesting,
172+
appFullPath,
173+
projectRoot,
170174
},
171175
},
172176
].filter(loader => Boolean(loader)),

Diff for: unit-testing-config-loader.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { join, relative } = require("path");
2+
3+
module.exports = function ({ appFullPath, projectRoot, angular, rootPagesRegExp }) {
4+
// TODO: Consider to use the files property from karma.conf.js
5+
const testFilesRegExp = /tests\/.*\.js/;
6+
const runnerFullPath = join(projectRoot, "node_modules", "nativescript-unit-test-runner");
7+
const runnerRelativePath = relative(appFullPath, runnerFullPath);
8+
let source = `
9+
require("tns-core-modules/bundle-entry-points");
10+
const runnerContext = require.context("${runnerRelativePath}", true, ${rootPagesRegExp});
11+
global.registerWebpackModules(runnerContext);
12+
`;
13+
14+
if (angular) {
15+
source += `
16+
const context = require.context("~/", true, ${testFilesRegExp});
17+
global.registerWebpackModules(context);
18+
`;
19+
} else {
20+
const registerModules = new RegExp(`(${rootPagesRegExp.source})|(${testFilesRegExp.source})`);
21+
source += `
22+
const context = require.context("~/", true, ${registerModules});
23+
global.registerWebpackModules(context);
24+
`;
25+
}
26+
27+
const runnerEntryPointPath = join(runnerRelativePath, "bundle-app.js");
28+
source += `
29+
require("${runnerEntryPointPath}");
30+
`;
31+
32+
return source;
33+
}

0 commit comments

Comments
 (0)