diff --git a/.changeset/thirty-pianos-occur.md b/.changeset/thirty-pianos-occur.md new file mode 100644 index 000000000..b631dc2f5 --- /dev/null +++ b/.changeset/thirty-pianos-occur.md @@ -0,0 +1,5 @@ +--- +'@guardian/libs': major +--- + +loadScript rejects with an Error diff --git a/libs/@guardian/libs/src/loadScript/loadScript.test.ts b/libs/@guardian/libs/src/loadScript/loadScript.test.ts index 990c502ad..2b433cef0 100644 --- a/libs/@guardian/libs/src/loadScript/loadScript.test.ts +++ b/libs/@guardian/libs/src/loadScript/loadScript.test.ts @@ -75,6 +75,12 @@ describe('loadScript', () => { }); it('rejects if the script fails to load', async () => { - await expect(loadScript(badURL)).rejects.toMatch(badURL); + expect.assertions(2); + try { + await loadScript(badURL); + } catch (error) { + expect(error instanceof Error).toEqual(true); + expect((error as Error).message).toEqual('Error loading script: bad-url'); + } }); }); diff --git a/libs/@guardian/libs/src/loadScript/loadScript.ts b/libs/@guardian/libs/src/loadScript/loadScript.ts index 9d18de31b..84d7c71c2 100644 --- a/libs/@guardian/libs/src/loadScript/loadScript.ts +++ b/libs/@guardian/libs/src/loadScript/loadScript.ts @@ -35,26 +35,22 @@ export const loadScript = ( ) => { if (error) { reject(error); - return; } if (typeof event === 'string') { - reject(`Error loading script: ${event}`); - + reject(new Error(`Error loading script: ${event}`)); return; } if (event instanceof Event) { const target = event.target as Element; - const src = target.getAttribute('src') ?? ''; - - reject(`Error loading script: ${src}`); - + const targetSrc = target.getAttribute('src') ?? ''; + reject(new Error(`Error loading script: ${targetSrc}`)); return; } - reject(`Error loading script`); + reject(new Error(`Error loading script: ${src}`)); }; const ref = document.scripts[0];