Inside my Svelte application, I am working with a boolean variable.
import { writable } from 'svelte/store'
export const authorised = writable(false)
This variable is imported into App.svelte and other Svelte files, where it can be accessed and updated using $authorised.
However, the $ syntax cannot be used in Cypress, so instead I have utilized the following:
import { authorised } from '../../src/stores'
describe( etc etc
authorised.set(true)
cy.visit(`/`)
In App.svelte, there is a console.log displaying the value of $authorised.
Despite setting values in the Svelte store during testing, the output in the Cypress browser's dev tools shows that authorised is still false.
The backend API successfully logs in and provides the necessary data. This data is used to update various values in the Svelte store, including authorised, with the expectation that the app will show the authorized screen upon visit rather than the login screen.
But why does the authorised value not get properly set in the Svelte store?
An update was made by incorporating Fody's approach at the beginning of App.svelte, outside of the script tags,
declare global {
interface Window {
Cypress?: Cypress.Cypress;
}
}
Moving this declaration inside the script led to an error related to the use of 'interface,' while placing it as shown above caused an issue regarding the keyword itself.
The keyword 'interface' is reserved
After referring to Parsing error: The keyword 'interface' is reserved and attempting to install ts-standard for resolution, the lengthy installation process has commenced.
Is installing ts-standard mandatory in this context?
A further modification involved adding the following code to main.ts:
import type { Writable } from 'svelte/store';
declare global {
interface Window {
Cypress?: Cypress.Cypress
authorised?: Writable<boolean>
}
}
This was then utilized in App.svelte as follows:
if (window.Cypress) {
window.authorised.set($authorised)
}
Lastly, 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 resulted in an error stating "window.authorised is undefined" along with the log message: 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
The line causing the error is mentioned as "window.authorised.set($authorised)" on line 16 of App.svelte.