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

Commit b4c2a87

Browse files
committed
feat: Implement unit testing with bundle
NativeScript/nativescript-cli#4392
1 parent 41b0a64 commit b4c2a87

6 files changed

+59
-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, 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

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

0 commit comments

Comments
 (0)