Within a Svelte app, I have a boolean variable.
import { writable } from 'svelte/store'
export const authorised = writable(false)
This is imported into App.svelte and other svelte files and accessed and updated using $authorised.
I can't use the $ syntax in Cypress so I used
import { authorised } from '../../src/stores'
describe( etc etc
authorised.set(true)
cy.visit(`/`)
Within App.svelte, I have a console.log showing value of $authorised.
Using dev tools in the cypress browser output, authorised is showing false.
The test logs in via the backend api, receiving the token and user id in the body response. With this data I set various values in svelte store (including authorised) so that when visit the app, it should show the authorised screen rather than the login screen.
So why is authorised not being set in svelte store?
Updated:
Using Fody's approach, I added to the start of App.svelte, outside of script,
declare global {
interface Window {
Cypress?: Cypress.Cypress;
}
}
Putting this within script gives an error on declare. But the above code, gives an error on interface.
The keyword 'interface' is reserved
I have looked at Parsing error: The keyword 'interface' is reserved and am installing ts-standard and updating the package.json. But the install is taking a long time.
Am I supposed to be installing ts-standard?
Further update:
In main.ts, I added
import type { Writable } from 'svelte/store';
declare global {
interface Window {
Cypress?: Cypress.Cypress
authorised?: Writable<boolean>
}
}
And in App.svelte.
if (window.Cypress) {
window.authorised.set($authorised)
}
and finally in the test.js file
.its('body').then((body) => {
cy.log(body)
cy.visit(`/`)
cy.window().then(win => win.authorised.set(true))
Running the test shows an error of "windows.authorised is undefined" with a console log of Uncaught TypeError: window.authorised is undefined instance App.svelte:16 init index.mjs:1891 App bundle.js:4033 app main.ts:14 bundle.js:4050
where App.svelte:16 is "window.authorised.set($authorised) "