aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-07-31 14:36:14 -0400
committerGitHub <noreply@github.com>2021-07-31 14:36:14 -0400
commit992c8bcf75b9477e1652cf2a458418e375903cc0 (patch)
treeefa833fb86bc023ab2a99773e1c3a98767435b9e
parent11f210375ef64ccf969f1ad3da10dbed00a18fef (diff)
test-workers (#1863)
* Add getImportedScripts utility function to test-sw.js * Move functionality to testServiceWorker * Rename file * Add loadEslint utility function * Add filterScriptPaths utility
-rw-r--r--test/test-workers.js (renamed from test/test-sw.js)69
1 files changed, 42 insertions, 27 deletions
diff --git a/test/test-sw.js b/test/test-workers.js
index 498a0a32..9f9c4d13 100644
--- a/test/test-sw.js
+++ b/test/test-workers.js
@@ -22,6 +22,15 @@ const {VM} = require('../dev/vm');
const assert = require('assert');
+function loadEslint() {
+ return JSON.parse(fs.readFileSync(path.join(__dirname, '..', '.eslintrc.json'), {encoding: 'utf8'}));
+}
+
+function filterScriptPaths(scriptPaths) {
+ const extDirName = 'ext';
+ return scriptPaths.filter((src) => !src.startsWith('/lib/')).map((src) => `${extDirName}${src}`);
+}
+
function getAllHtmlScriptPaths(fileName) {
const domSource = fs.readFileSync(fileName, {encoding: 'utf8'});
const dom = new JSDOM(domSource);
@@ -43,38 +52,44 @@ function convertBackgroundScriptsToServiceWorkerScripts(scripts) {
scripts[index] = '/js/dom/simple-dom-parser.js';
}
-function main() {
- try {
- // Verify that sw.js scripts match background.html scripts
- const rootDir = path.join(__dirname, '..');
- const extDirName = 'ext';
- const extDir = path.join(rootDir, extDirName);
+function getImportedScripts(scriptPath, fields) {
+ const importedScripts = [];
- const scripts = getAllHtmlScriptPaths(path.join(extDir, 'background.html'));
- convertBackgroundScriptsToServiceWorkerScripts(scripts);
- const importedScripts = [];
+ const importScripts = (...scripts) => {
+ importedScripts.push(...scripts);
+ };
- const importScripts = (...scripts2) => {
- importedScripts.push(...scripts2);
- };
+ const vm = new VM(Object.assign({importScripts}, fields));
+ vm.context.self = vm.context;
+ vm.execute([scriptPath]);
- const vm = new VM({importScripts});
- vm.context.self = vm.context;
- vm.execute(['sw.js']);
+ return importedScripts;
+}
+
+function testServiceWorker() {
+ // Verify that sw.js scripts match background.html scripts
+ const extDir = path.join(__dirname, '..', 'ext');
+ const scripts = getAllHtmlScriptPaths(path.join(extDir, 'background.html'));
+ convertBackgroundScriptsToServiceWorkerScripts(scripts);
+ const importedScripts = getImportedScripts('sw.js', {});
+ assert.deepStrictEqual(scripts, importedScripts);
- vm.assert.deepStrictEqual(scripts, importedScripts);
+ // Verify that eslint config lists files correctly
+ const expectedSwRulesFiles = filterScriptPaths(scripts);
+ const eslintConfig = loadEslint();
+ const swRules = eslintConfig.overrides.find((item) => (
+ typeof item.env === 'object' &&
+ item.env !== null &&
+ item.env.serviceworker === true
+ ));
+ assert.ok(typeof swRules !== 'undefined');
+ assert.ok(Array.isArray(swRules.files));
+ assert.deepStrictEqual(swRules.files, expectedSwRulesFiles);
+}
- // Verify that eslint config lists files correctly
- const expectedSwRulesFiles = scripts.filter((src) => !src.startsWith('/lib/')).map((src) => `${extDirName}${src}`);
- const eslintConfig = JSON.parse(fs.readFileSync(path.join(rootDir, '.eslintrc.json'), {encoding: 'utf8'}));
- const swRules = eslintConfig.overrides.find((item) => (
- typeof item.env === 'object' &&
- item.env !== null &&
- item.env.serviceworker === true
- ));
- assert.ok(typeof swRules !== 'undefined');
- assert.ok(Array.isArray(swRules.files));
- assert.deepStrictEqual(swRules.files, expectedSwRulesFiles);
+function main() {
+ try {
+ testServiceWorker();
} catch (e) {
console.error(e);
process.exit(-1);