service-worker: working on making tests run with the new plugin loader (ref. #15)

merge-requests/9/merge
Michał 'rysiek' Woźniak 2021-09-18 23:35:22 +00:00
rodzic ffe9dd28d5
commit 8603237db0
2 zmienionych plików z 74 dodań i 62 usunięć

Wyświetl plik

@ -27,13 +27,19 @@ describe("service-worker", () => {
global.self = new ServiceWorkerGlobalScope() global.self = new ServiceWorkerGlobalScope()
jest.resetModules(); jest.resetModules();
self.LibResilientPlugins = new Array() self.LibResilientPlugins = new Array()
self.importScripts = jest.fn((url)=>{
console.debug(`importScripts('../${url}')`)
try {
require('../' + url);
} catch(e) {}
})
global.LibResilientPluginConstructors = new Map()
}) })
test("basic set-up: LibResilientPlugins", async () => { test("basic set-up: LibResilientPlugins", async () => {
self.LibResilientPlugins = false self.LibResilientPlugins = false
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [],
},
loggedComponents: [ loggedComponents: [
'service-worker' 'service-worker'
] ]
@ -45,8 +51,7 @@ describe("service-worker", () => {
test("Promise.any() polyfill should work", async () => { test("Promise.any() polyfill should work", async () => {
self.LibResilientPlugins = false self.LibResilientPlugins = false
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [],
},
loggedComponents: [ loggedComponents: [
'service-worker' 'service-worker'
] ]
@ -87,14 +92,13 @@ describe("service-worker", () => {
test("fetching content should work", async () => { test("fetching content should work", async () => {
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [{
'fetch': {} name: 'fetch'
}, }],
loggedComponents: [ loggedComponents: [
'service-worker', 'fetch' 'service-worker', 'fetch'
] ]
} }
require("../plugins/fetch.js");
require("../service-worker.js"); require("../service-worker.js");
await self.trigger('install') await self.trigger('install')
@ -111,10 +115,11 @@ describe("service-worker", () => {
test("failed fetch by first configured plugin should not affect a successful fetch by a second one", async () => { test("failed fetch by first configured plugin should not affect a successful fetch by a second one", async () => {
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [{
'reject-all': {}, name: 'reject-all'
'resolve-all': {} },{
}, name: 'resolve-all'
}],
loggedComponents: [ loggedComponents: [
'service-worker' 'service-worker'
] ]
@ -162,9 +167,9 @@ describe("service-worker", () => {
jest.useFakeTimers() jest.useFakeTimers()
self.LibResilientConfig = { self.LibResilientConfig = {
defaultPluginTimeout: 1000, defaultPluginTimeout: 1000,
plugins: { plugins: [{
'resolve-with-timeout': {} name: 'resolve-with-timeout'
}, }],
loggedComponents: [ loggedComponents: [
'service-worker', 'service-worker',
] ]
@ -214,9 +219,9 @@ describe("service-worker", () => {
); );
}); });
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [{
'reject-all': {} name: 'reject-all'
}, }],
loggedComponents: [ loggedComponents: [
'service-worker' 'service-worker'
] ]
@ -252,9 +257,9 @@ describe("service-worker", () => {
); );
}); });
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [{
'reject-all': {} name: 'reject-all'
}, }],
loggedComponents: [ loggedComponents: [
'service-worker' 'service-worker'
] ]
@ -273,16 +278,15 @@ describe("service-worker", () => {
test("stashing content after a successful fetch should work", async () => { test("stashing content after a successful fetch should work", async () => {
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [{
'fetch': {}, name: 'fetch'
'cache': {} },{
}, name: 'cache'
}],
loggedComponents: [ loggedComponents: [
'service-worker', 'fetch', 'cache' 'service-worker', 'fetch', 'cache'
] ]
} }
require("../plugins/fetch.js");
require("../plugins/cache.js");
require("../service-worker.js"); require("../service-worker.js");
await self.trigger('install') await self.trigger('install')
@ -306,10 +310,11 @@ describe("service-worker", () => {
test("stashing should be skipped if content was retrieved from a stashing plugin", async () => { test("stashing should be skipped if content was retrieved from a stashing plugin", async () => {
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [{
'stashing-test': {}, name: 'stashing-test'
'reject-all': {} },{
}, name: 'reject-all'
}],
loggedComponents: [ loggedComponents: [
'service-worker' 'service-worker'
] ]
@ -360,10 +365,11 @@ describe("service-worker", () => {
test("content should be stashed if it was retrieved from a job after retrieval from a stashing plugin, and it differs from the stashed version", async () => { test("content should be stashed if it was retrieved from a job after retrieval from a stashing plugin, and it differs from the stashed version", async () => {
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [{
'stashing-test': {}, name: 'stashing-test'
'resolve-all': {} },{
}, name: 'resolve-all'
}],
loggedComponents: [ loggedComponents: [
'service-worker' 'service-worker'
] ]
@ -446,10 +452,11 @@ describe("service-worker", () => {
test("content should be stashed if it was retrieved from a job after retrieval from a stashing plugin, even it does not differ from the stashed version", async () => { test("content should be stashed if it was retrieved from a job after retrieval from a stashing plugin, even it does not differ from the stashed version", async () => {
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [{
'stashing-test': {}, name: 'stashing-test'
'resolve-all': {} },{
}, name: 'resolve-all'
}],
loggedComponents: [ loggedComponents: [
'service-worker' 'service-worker'
] ]
@ -498,14 +505,13 @@ describe("service-worker", () => {
test("stashing content explicitly should work", async () => { test("stashing content explicitly should work", async () => {
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [{
'cache': {} name: 'cache'
}, }],
loggedComponents: [ loggedComponents: [
'service-worker', 'cache' 'service-worker', 'cache'
] ]
} }
require("../plugins/cache.js");
require("../service-worker.js"); require("../service-worker.js");
await self.trigger('install') await self.trigger('install')
@ -547,14 +553,13 @@ describe("service-worker", () => {
test("unstashing content explicitly should work", async () => { test("unstashing content explicitly should work", async () => {
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [{
'cache': {} name: 'cache'
}, }],
loggedComponents: [ loggedComponents: [
'service-worker', 'cache' 'service-worker', 'cache'
] ]
} }
require("../plugins/cache.js");
require("../service-worker.js"); require("../service-worker.js");
await self.trigger('install') await self.trigger('install')
@ -608,9 +613,9 @@ describe("service-worker", () => {
test("publishing content explicitly should work (stub)", async () => { test("publishing content explicitly should work (stub)", async () => {
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [{
'publish-test': {} name: 'publish-test'
}, }],
loggedComponents: [ loggedComponents: [
'service-worker' 'service-worker'
] ]
@ -649,10 +654,15 @@ describe("service-worker", () => {
test("using plugins with dependencies should work", async () => { test("using plugins with dependencies should work", async () => {
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [{
'dependent-test': { // TODO: actually implement the test?
} name: 'dependent-test',
}, uses: [{
name: 'dependency1-test'
},{
name: 'dependency2-test'
}]
}],
loggedComponents: [ loggedComponents: [
'service-worker' 'service-worker'
] ]
@ -684,9 +694,9 @@ describe("service-worker", () => {
test("should error out if all plugins fail", async () => { test("should error out if all plugins fail", async () => {
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [{
'reject-all': {} name: 'reject-all'
}, }],
loggedComponents: [ loggedComponents: [
'service-worker' 'service-worker'
] ]
@ -708,9 +718,9 @@ describe("service-worker", () => {
test("should send clientId back if event.resultingClientId is set", async () => { test("should send clientId back if event.resultingClientId is set", async () => {
self.LibResilientConfig = { self.LibResilientConfig = {
plugins: { plugins: [{
'resolve-all': {} name: 'resolve-all'
}, }],
loggedComponents: [ loggedComponents: [
'service-worker' 'service-worker'
] ]

Wyświetl plik

@ -113,7 +113,9 @@ try {
self.log('service-worker', 'config loaded.') self.log('service-worker', 'config loaded.')
// create the LibResilientPluginConstructors map // create the LibResilientPluginConstructors map
var LibResilientPluginConstructors = new Map() // the global... hack is here so that we can run tests; not the most elegant
// TODO: find a better way
var LibResilientPluginConstructors = global.LibResilientPluginConstructors || new Map()
// this is the stash for plugins that need dependencies instantiated first // this is the stash for plugins that need dependencies instantiated first
var dependentPlugins = new Array() var dependentPlugins = new Array()
@ -157,8 +159,8 @@ try {
do { do {
// instantiate the plugin // instantiate the plugin
self.log('service-worker', `${pluginConfig.name}: instantiated`)
let plugin = LibResilientPluginConstructors.get(pluginConfig.name)(self, pluginConfig) let plugin = LibResilientPluginConstructors.get(pluginConfig.name)(self, pluginConfig)
self.log('service-worker', `${pluginConfig.name}: instantiated`)
// do we have a stashed plugin that requires dependencies? // do we have a stashed plugin that requires dependencies?
if (dependentPlugins.length === 0) { if (dependentPlugins.length === 0) {