I want to export a function from a Svelte component and import it into a Jest test file for testing.
My expected result based on this post is that I can do:
// index.svelte
<script>
export function updateEnabledSubmitSignup(email, username, password, confirmedPassword) {
// ...
}
</script>
and then in my test file:
import Signup from "../src/routes/signup/index.svelte"
describe("signup page logic", () => {
test("ensure that the signup form button enablement conditions work properly", () => {
console.log(Signup, "5rm")
const failureOne = Signup.updateEnabledSubmitSignup()
expect(failureOne).toBe(false)
})
})
But running this code gives
tests/signup.test.ts:6:31 - error TS2339: Property 'updateEnabledSubmitSignup' does not exist on type 'typeof SvelteComponentDev'.
6 const failureOne = Signup.updateEnabledSubmitSignup()
It fails also if I do <script context="module">
with Property 'updateEnabledSubmitSignup' does not exist on type 'typeof SvelteComponentDev'.
If I try a named import like
import { updateEnabledSubmitSignup } from "../src/routes/signup/index.svelte"
then running the test gives error TS2614: Module '"*.svelte"' has no exported member 'updateEnabledSubmitSignup'. Did you mean to use 'import updateEnabledSubmitSignup from "*.svelte"' instead?
Anyone know how to do this please? This reddit post claims "You can use the tag at the top of your component to export a function." But even though I do that, the import fails.