test-workers (#1863)
* Add getImportedScripts utility function to test-sw.js * Move functionality to testServiceWorker * Rename file * Add loadEslint utility function * Add filterScriptPaths utility
This commit is contained in:
parent
11f210375e
commit
992c8bcf75
@ -22,6 +22,15 @@ const {VM} = require('../dev/vm');
|
|||||||
const assert = require('assert');
|
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) {
|
function getAllHtmlScriptPaths(fileName) {
|
||||||
const domSource = fs.readFileSync(fileName, {encoding: 'utf8'});
|
const domSource = fs.readFileSync(fileName, {encoding: 'utf8'});
|
||||||
const dom = new JSDOM(domSource);
|
const dom = new JSDOM(domSource);
|
||||||
@ -43,30 +52,31 @@ function convertBackgroundScriptsToServiceWorkerScripts(scripts) {
|
|||||||
scripts[index] = '/js/dom/simple-dom-parser.js';
|
scripts[index] = '/js/dom/simple-dom-parser.js';
|
||||||
}
|
}
|
||||||
|
|
||||||
function main() {
|
function getImportedScripts(scriptPath, fields) {
|
||||||
try {
|
|
||||||
// Verify that sw.js scripts match background.html scripts
|
|
||||||
const rootDir = path.join(__dirname, '..');
|
|
||||||
const extDirName = 'ext';
|
|
||||||
const extDir = path.join(rootDir, extDirName);
|
|
||||||
|
|
||||||
const scripts = getAllHtmlScriptPaths(path.join(extDir, 'background.html'));
|
|
||||||
convertBackgroundScriptsToServiceWorkerScripts(scripts);
|
|
||||||
const importedScripts = [];
|
const importedScripts = [];
|
||||||
|
|
||||||
const importScripts = (...scripts2) => {
|
const importScripts = (...scripts) => {
|
||||||
importedScripts.push(...scripts2);
|
importedScripts.push(...scripts);
|
||||||
};
|
};
|
||||||
|
|
||||||
const vm = new VM({importScripts});
|
const vm = new VM(Object.assign({importScripts}, fields));
|
||||||
vm.context.self = vm.context;
|
vm.context.self = vm.context;
|
||||||
vm.execute(['sw.js']);
|
vm.execute([scriptPath]);
|
||||||
|
|
||||||
vm.assert.deepStrictEqual(scripts, importedScripts);
|
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);
|
||||||
|
|
||||||
// Verify that eslint config lists files correctly
|
// Verify that eslint config lists files correctly
|
||||||
const expectedSwRulesFiles = scripts.filter((src) => !src.startsWith('/lib/')).map((src) => `${extDirName}${src}`);
|
const expectedSwRulesFiles = filterScriptPaths(scripts);
|
||||||
const eslintConfig = JSON.parse(fs.readFileSync(path.join(rootDir, '.eslintrc.json'), {encoding: 'utf8'}));
|
const eslintConfig = loadEslint();
|
||||||
const swRules = eslintConfig.overrides.find((item) => (
|
const swRules = eslintConfig.overrides.find((item) => (
|
||||||
typeof item.env === 'object' &&
|
typeof item.env === 'object' &&
|
||||||
item.env !== null &&
|
item.env !== null &&
|
||||||
@ -75,6 +85,11 @@ function main() {
|
|||||||
assert.ok(typeof swRules !== 'undefined');
|
assert.ok(typeof swRules !== 'undefined');
|
||||||
assert.ok(Array.isArray(swRules.files));
|
assert.ok(Array.isArray(swRules.files));
|
||||||
assert.deepStrictEqual(swRules.files, expectedSwRulesFiles);
|
assert.deepStrictEqual(swRules.files, expectedSwRulesFiles);
|
||||||
|
}
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
try {
|
||||||
|
testServiceWorker();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
process.exit(-1);
|
process.exit(-1);
|
Loading…
Reference in New Issue
Block a user