Skip to content

Commit

Permalink
feat: keep compatibility with test-runner v2
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni authored and NathanWalker committed Nov 23, 2021
1 parent 42504ef commit 8212103
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ test-app/
*.js
*.js.map
!nativescript.webpack.js
!nativescript.webpack.compat.js
!loaders/unit-test-loader.js

coverage
Expand Down
139 changes: 139 additions & 0 deletions nativescript.webpack.compat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
const { join, dirname } = require('path');
const { merge } = require('webpack-merge');
const globRegex = require('glob-regex');

function getKarmaTestsRegex(webpack) {
const karmaConfig = require(webpack.Utils.project.getProjectFilePath('karma.conf.js'));
let filesRegex = karmaConfig.filesRegex ||
new RegExp((karmaConfig.filePatterns || []).map((glob) =>
globRegex(`./${glob}`).source // all webpack require.context start with `./` and glob-regex adds ^
).join('|'));

if (!filesRegex || !filesRegex.source) {
webpack.Utils.log.warn("Karma files regex not found, falling back to tests/**/*.ts");
filesRegex = /tests\/.*\.ts/;
}
return filesRegex;
}

/**
* @param {typeof import("@nativescript/webpack")} webpack
*/
module.exports = webpack => {
webpack.chainWebpack((config, env) => {
if (env.karmaWebpack) {
return setupKarmaBuild(config, env, webpack);
}

if (env.unitTesting) {
return setupUnitTestBuild(config, env, webpack);
}
});
};

/**
* @param {import("webpack-chain")} config
* @param {typeof import("@nativescript/webpack")} webpack
*/
function setupKarmaBuild(config, env, webpack) {
const karmaWebpack = require('karma-webpack/lib/webpack/defaults');
const defaults = karmaWebpack.create();
delete defaults.optimization;

karmaWebpack.create = () => {
return defaults;
};

config.entryPoints.clear();
config.optimization.clear();

config.plugins.delete('WatchStatePlugin');
config.plugins.delete('AngularCompilerPlugin');
config.plugins.delete('AngularWebpackPlugin');
config.module.rules.delete('angular');
// config.plugins.delete('CleanWebpackPlugin')
config.plugin('DefinePlugin').tap((args) => {
args[0] = merge(args[0], {
__TEST_RUNNER_STAY_OPEN__: !!env.stayOpen,
});

return args;
});



config.output.delete('path'); // use temp path
config.output.set('iife', true);
config.output.set('libraryTarget', 'global');
config.output.set('clean', true);

config.module
.rule('unit-test')
.enforce('post')
.include.add(webpack.Utils.platform.getEntryDirPath()).end()
.test(/\.(ts|js)/)
.use('unit-test-loader')
.loader(join(__dirname, 'loaders', 'unit-test-loader'))
.options({
appPath: webpack.Utils.platform.getEntryDirPath(),
platform: webpack.Utils.platform.getPlatformName()
});
}

/**
* @param {import("webpack-chain")} config
* @param {typeof import("@nativescript/webpack")} webpack
*/
function setupUnitTestBuild(config, env, webpack) {
// config.plugins.delete('CleanWebpackPlugin');
// config.output.set('clean', false);

// harmless warnings
config.set(
'ignoreWarnings',
(config.get('ignoreWarnings') || []).concat([
/Can't resolve '@nativescript\/unit-test-runner\/app\/stop-process.js'/
])
);

const runnerPath = dirname(
require.resolve('@nativescript/unit-test-runner/package.json')
);
config.module.rule('css').include.add(runnerPath);
config.module.rule('xml').include.add(runnerPath);
config.module.rule('js').include.add(runnerPath);
const filesRegex = getKarmaTestsRegex(webpack);

config.plugin('DefinePlugin').tap((args) => {
args[0] = merge(args[0], {
'global.TNS_WEBPACK': true,
});

return args;
});

const entryPath = webpack.Utils.virtualModules.addVirtualEntry(config, 'unit-test-runner', `
// VIRTUAL ENTRY START
const context = require.context(
"~/",
/* deep: */ true,
/* filter: */ ${filesRegex}
);
global.registerWebpackModules(context);
// VIRTUAL ENTRY END
`);

// config.entryPoints.clear()
config.entry('bundle')
.clear()
.add('@nativescript/core/globals/index.js')
.add('@nativescript/core/bundle-entry-points')
.add('@nativescript/unit-test-runner/app/bundle-app')
// .add('@nativescript/unit-test-runner/app/entry')
.add(entryPath);
if (webpack.Utils.platform.getPlatformName() === 'android') {
config.entry('bundle')
.add('@nativescript/core/ui/frame')
.add('@nativescript/core/ui/frame/activity');
}
}
41 changes: 30 additions & 11 deletions nativescript.webpack.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
const { join, dirname } = require('path');
const { existsSync } = require('fs');
const { merge } = require('webpack-merge');

function getTestEntrypoint() {
const testTsEntryPath = join(webpack.Utils.platform.getEntryDirPath(), 'test.ts');
const testJsEntryPath = join(webpack.Utils.platform.getEntryDirPath(), 'test.js');
if (existsSync(testTsEntryPath)) {
return testTsEntryPath;
}
if (existsSync(testJsEntryPath)) {
return testJsEntryPath;
}
return null;
}

/**
* @param {typeof import("@nativescript/webpack")} webpack
*/
module.exports = webpack => {
if (!getTestEntrypoint()) {
return require('./nativescript.webpack.compat')(webpack);
}
webpack.chainWebpack((config, env) => {

if (env.unitTesting) {
return setupUnitTestBuild(config, env, webpack);
}
Expand All @@ -18,6 +33,12 @@ module.exports = webpack => {
* @param {typeof import("@nativescript/webpack")} webpack
*/
function setupUnitTestBuild(config, env, webpack) {

const testEntrypointPath = getTestEntrypoint();
if (!testEntrypointPath) { // this should never happen
webpack.Utils.log.error('No test entrypoint found');
return;
}
// config.plugins.delete('CleanWebpackPlugin');
// config.output.set('clean', false);

Expand All @@ -40,8 +61,6 @@ function setupUnitTestBuild(config, env, webpack) {
}
env.testTsConfig = env.testTsConfig || env.testTSConfig;
const defaultTsConfig = webpack.Utils.project.getProjectFilePath('tsconfig.spec.json');
const testTsEntryPath = join(webpack.Utils.platform.getEntryDirPath(), 'test.ts');
const testJsEntryPath = join(webpack.Utils.platform.getEntryDirPath(), 'test.js');
const tsConfigPath = env.testTsConfig || (require('fs').existsSync(defaultTsConfig) ? defaultTsConfig : undefined);
if (tsConfigPath) {
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 })));
Expand All @@ -52,12 +71,12 @@ function setupUnitTestBuild(config, env, webpack) {
}

config.plugin('DefinePlugin').tap((args) => {
args[0] = merge(args[0], {
'global.TNS_WEBPACK': true,
});
args[0] = merge(args[0], {
'global.TNS_WEBPACK': true,
});

return args;
});
return args;
});

if (env.codeCoverage) {
config.module
Expand All @@ -84,9 +103,9 @@ function setupUnitTestBuild(config, env, webpack) {
.add('@nativescript/core/globals/index.js')
.add('@nativescript/core/bundle-entry-points')
// .add('@nativescript/unit-test-runner/app/bundle-app')
.add(require('fs').existsSync(testTsEntryPath) ? testTsEntryPath : testJsEntryPath);
// .add('@nativescript/unit-test-runner/app/entry')
// .add(entryPath);
.add(testEntrypointPath);
// .add('@nativescript/unit-test-runner/app/entry')
// .add(entryPath);
if (webpack.Utils.platform.getPlatformName() === 'android') {
config.entry('bundle')
.add('@nativescript/core/ui/frame')
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
]
},
"dependencies": {
"@nativescript/hook": "^2.0.0"
"@nativescript/hook": "^2.0.0",
"glob-regex": "^0.3.2"
}
}

0 comments on commit 8212103

Please sign in to comment.