I want to test the following Svelte component with Vitest:
<script context="module">
import {push} from 'svelte-spa-router'
import {onDestroy} from 'svelte'
let time = 1000*120;
let interval = 1000;
let running = true;
export const twoDigits = (number) => {
return number.toLocaleString('en-US', {
minimumIntegerDigits: 2,
useGrouping: false
})
}
export const timeString = (ms) => {
let seconds = ms / 1000;
const minutes = Math.floor(seconds / 60);
seconds = seconds % 60;
const timeString = twoDigits(minutes) + ":" + twoDigits(seconds);
return timeString
}
let timeShown = timeString(time)
export const startCountdown = () => {
setTimeout(()=>{
time -= interval;
timeShown = timeString(time);
if (time > 0 && running) {
startCountdown();
} else if (time > 0 && !running) {
console.log("Countdown stopped")
} else {
console.log("Done!")
setTimeout( () => {
push('/gameover')
},1000)
}
}, interval)
}
startCountdown()
onDestroy(()=>{
running = false;
})
</script>
<div id="countdown">
{timeShown}
</div>
Unfortunately, I get an error saying Function called outside component initialization
as soon as I try to import the component in my test file with import Countdown from "../lib/Countdown.svelte"
.
The problem could have something to do with the onDestroy
function but I don’t know, how to fix this.
3