Skip to content

Commit 8212103

Browse files
edusperoniNathanWalker
authored andcommitted
feat: keep compatibility with test-runner v2
1 parent 42504ef commit 8212103

5 files changed

+177
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test-app/
55
*.js
66
*.js.map
77
!nativescript.webpack.js
8+
!nativescript.webpack.compat.js
89
!loaders/unit-test-loader.js
910

1011
coverage

nativescript.webpack.compat.js

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
const { join, dirname } = require('path');
2+
const { merge } = require('webpack-merge');
3+
const globRegex = require('glob-regex');
4+
5+
function getKarmaTestsRegex(webpack) {
6+
const karmaConfig = require(webpack.Utils.project.getProjectFilePath('karma.conf.js'));
7+
let filesRegex = karmaConfig.filesRegex ||
8+
new RegExp((karmaConfig.filePatterns || []).map((glob) =>
9+
globRegex(`./${glob}`).source // all webpack require.context start with `./` and glob-regex adds ^
10+
).join('|'));
11+
12+
if (!filesRegex || !filesRegex.source) {
13+
webpack.Utils.log.warn("Karma files regex not found, falling back to tests/**/*.ts");
14+
filesRegex = /tests\/.*\.ts/;
15+
}
16+
return filesRegex;
17+
}
18+
19+
/**
20+
* @param {typeof import("@nativescript/webpack")} webpack
21+
*/
22+
module.exports = webpack => {
23+
webpack.chainWebpack((config, env) => {
24+
if (env.karmaWebpack) {
25+
return setupKarmaBuild(config, env, webpack);
26+
}
27+
28+
if (env.unitTesting) {
29+
return setupUnitTestBuild(config, env, webpack);
30+
}
31+
});
32+
};
33+
34+
/**
35+
* @param {import("webpack-chain")} config
36+
* @param {typeof import("@nativescript/webpack")} webpack
37+
*/
38+
function setupKarmaBuild(config, env, webpack) {
39+
const karmaWebpack = require('karma-webpack/lib/webpack/defaults');
40+
const defaults = karmaWebpack.create();
41+
delete defaults.optimization;
42+
43+
karmaWebpack.create = () => {
44+
return defaults;
45+
};
46+
47+
config.entryPoints.clear();
48+
config.optimization.clear();
49+
50+
config.plugins.delete('WatchStatePlugin');
51+
config.plugins.delete('AngularCompilerPlugin');
52+
config.plugins.delete('AngularWebpackPlugin');
53+
config.module.rules.delete('angular');
54+
// config.plugins.delete('CleanWebpackPlugin')
55+
config.plugin('DefinePlugin').tap((args) => {
56+
args[0] = merge(args[0], {
57+
__TEST_RUNNER_STAY_OPEN__: !!env.stayOpen,
58+
});
59+
60+
return args;
61+
});
62+
63+
64+
65+
config.output.delete('path'); // use temp path
66+
config.output.set('iife', true);
67+
config.output.set('libraryTarget', 'global');
68+
config.output.set('clean', true);
69+
70+
config.module
71+
.rule('unit-test')
72+
.enforce('post')
73+
.include.add(webpack.Utils.platform.getEntryDirPath()).end()
74+
.test(/\.(ts|js)/)
75+
.use('unit-test-loader')
76+
.loader(join(__dirname, 'loaders', 'unit-test-loader'))
77+
.options({
78+
appPath: webpack.Utils.platform.getEntryDirPath(),
79+
platform: webpack.Utils.platform.getPlatformName()
80+
});
81+
}
82+
83+
/**
84+
* @param {import("webpack-chain")} config
85+
* @param {typeof import("@nativescript/webpack")} webpack
86+
*/
87+
function setupUnitTestBuild(config, env, webpack) {
88+
// config.plugins.delete('CleanWebpackPlugin');
89+
// config.output.set('clean', false);
90+
91+
// harmless warnings
92+
config.set(
93+
'ignoreWarnings',
94+
(config.get('ignoreWarnings') || []).concat([
95+
/Can't resolve '@nativescript\/unit-test-runner\/app\/stop-process.js'/
96+
])
97+
);
98+
99+
const runnerPath = dirname(
100+
require.resolve('@nativescript/unit-test-runner/package.json')
101+
);
102+
config.module.rule('css').include.add(runnerPath);
103+
config.module.rule('xml').include.add(runnerPath);
104+
config.module.rule('js').include.add(runnerPath);
105+
const filesRegex = getKarmaTestsRegex(webpack);
106+
107+
config.plugin('DefinePlugin').tap((args) => {
108+
args[0] = merge(args[0], {
109+
'global.TNS_WEBPACK': true,
110+
});
111+
112+
return args;
113+
});
114+
115+
const entryPath = webpack.Utils.virtualModules.addVirtualEntry(config, 'unit-test-runner', `
116+
// VIRTUAL ENTRY START
117+
const context = require.context(
118+
"~/",
119+
/* deep: */ true,
120+
/* filter: */ ${filesRegex}
121+
);
122+
global.registerWebpackModules(context);
123+
// VIRTUAL ENTRY END
124+
`);
125+
126+
// config.entryPoints.clear()
127+
config.entry('bundle')
128+
.clear()
129+
.add('@nativescript/core/globals/index.js')
130+
.add('@nativescript/core/bundle-entry-points')
131+
.add('@nativescript/unit-test-runner/app/bundle-app')
132+
// .add('@nativescript/unit-test-runner/app/entry')
133+
.add(entryPath);
134+
if (webpack.Utils.platform.getPlatformName() === 'android') {
135+
config.entry('bundle')
136+
.add('@nativescript/core/ui/frame')
137+
.add('@nativescript/core/ui/frame/activity');
138+
}
139+
}

nativescript.webpack.js

+30-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
const { join, dirname } = require('path');
2+
const { existsSync } = require('fs');
23
const { merge } = require('webpack-merge');
34

5+
function getTestEntrypoint() {
6+
const testTsEntryPath = join(webpack.Utils.platform.getEntryDirPath(), 'test.ts');
7+
const testJsEntryPath = join(webpack.Utils.platform.getEntryDirPath(), 'test.js');
8+
if (existsSync(testTsEntryPath)) {
9+
return testTsEntryPath;
10+
}
11+
if (existsSync(testJsEntryPath)) {
12+
return testJsEntryPath;
13+
}
14+
return null;
15+
}
16+
417
/**
518
* @param {typeof import("@nativescript/webpack")} webpack
619
*/
720
module.exports = webpack => {
21+
if (!getTestEntrypoint()) {
22+
return require('./nativescript.webpack.compat')(webpack);
23+
}
824
webpack.chainWebpack((config, env) => {
9-
1025
if (env.unitTesting) {
1126
return setupUnitTestBuild(config, env, webpack);
1227
}
@@ -18,6 +33,12 @@ module.exports = webpack => {
1833
* @param {typeof import("@nativescript/webpack")} webpack
1934
*/
2035
function setupUnitTestBuild(config, env, webpack) {
36+
37+
const testEntrypointPath = getTestEntrypoint();
38+
if (!testEntrypointPath) { // this should never happen
39+
webpack.Utils.log.error('No test entrypoint found');
40+
return;
41+
}
2142
// config.plugins.delete('CleanWebpackPlugin');
2243
// config.output.set('clean', false);
2344

@@ -40,8 +61,6 @@ function setupUnitTestBuild(config, env, webpack) {
4061
}
4162
env.testTsConfig = env.testTsConfig || env.testTSConfig;
4263
const defaultTsConfig = webpack.Utils.project.getProjectFilePath('tsconfig.spec.json');
43-
const testTsEntryPath = join(webpack.Utils.platform.getEntryDirPath(), 'test.ts');
44-
const testJsEntryPath = join(webpack.Utils.platform.getEntryDirPath(), 'test.js');
4564
const tsConfigPath = env.testTsConfig || (require('fs').existsSync(defaultTsConfig) ? defaultTsConfig : undefined);
4665
if (tsConfigPath) {
4766
config.when(config.module.rules.has('ts'), (config) => config.module.rule('ts').uses.get('ts-loader').options(merge(config.module.rule('ts').uses.get('ts-loader').get('options'), { configFile: tsConfigPath })));
@@ -52,12 +71,12 @@ function setupUnitTestBuild(config, env, webpack) {
5271
}
5372

5473
config.plugin('DefinePlugin').tap((args) => {
55-
args[0] = merge(args[0], {
56-
'global.TNS_WEBPACK': true,
57-
});
74+
args[0] = merge(args[0], {
75+
'global.TNS_WEBPACK': true,
76+
});
5877

59-
return args;
60-
});
78+
return args;
79+
});
6180

6281
if (env.codeCoverage) {
6382
config.module
@@ -84,9 +103,9 @@ function setupUnitTestBuild(config, env, webpack) {
84103
.add('@nativescript/core/globals/index.js')
85104
.add('@nativescript/core/bundle-entry-points')
86105
// .add('@nativescript/unit-test-runner/app/bundle-app')
87-
.add(require('fs').existsSync(testTsEntryPath) ? testTsEntryPath : testJsEntryPath);
88-
// .add('@nativescript/unit-test-runner/app/entry')
89-
// .add(entryPath);
106+
.add(testEntrypointPath);
107+
// .add('@nativescript/unit-test-runner/app/entry')
108+
// .add(entryPath);
90109
if (webpack.Utils.platform.getPlatformName() === 'android') {
91110
config.entry('bundle')
92111
.add('@nativescript/core/ui/frame')

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
]
5050
},
5151
"dependencies": {
52-
"@nativescript/hook": "^2.0.0"
52+
"@nativescript/hook": "^2.0.0",
53+
"glob-regex": "^0.3.2"
5354
}
5455
}

0 commit comments

Comments
 (0)