
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
svelte-mock
Advanced tools
Svelte-mock is a testing library to help with mocking svelte components, testing svelte component properties and making code coverage reports more accurate.
Note: Jest version must be 25.0.0 for code coverage to work
Add the following to your jest config:
transform: {
'\\.html$': 'svelte-mock/transform',
'\\.svelte$': 'svelte-mock/transform'
},
setupFilesAfterEnv: [
'svelte-mock/extend'
],
You can mock a svelte component with the following code:
jest.mock('Component.svelte')
import Component from 'Component.svelte'
svelteMock.mockImplementation(Component)
If you want to specify your own mock implementations you can pass a svelte component as a second argument:
svelteMock.mockImplementation(Component, MockComponent)
svelte-mock includes some useful expect extensions
toHaveInstancePasses if a mocked component class has been instantiated at least once.
<Component />
expect(Component).toHaveInstance()
Component - a svelte component class to be checked for an instancetoHaveInstanceWithPropsPasses if a mocked component class has an instance with props.
<Component first={firstValue} second={secondValue} />
expect(Component).toHaveInstanceWithProps(['first'])
expect(Component).toHaveInstanceWithProps(['first', 'second'])
expect(Component).not.toHaveInstanceWithProps(['nonExistent'])
expect(Component).toHaveInstanceWithProps({ first: firstValue })
expect(Component).not.toHaveInstanceWithProps({ first: wrongValue })
Component - a svelte component class to be checked for a matching instancetoHaveInstanceWithBoundPropsPasses if a mocked component class has an instance with bound props.
<Component bind:first=firstValue bind:second=secondValue />
expect(Component).toHaveInstanceWithBoundProps(['first'])
expect(Component).toHaveInstanceWithBoundProps(['first', 'second'])
expect(Component).not.toHaveInstanceWithBoundProps(['nonExistent'])
expect(Component).toHaveInstanceWithBoundProps({ first: firstValue })
expect(Component).not.toHaveInstanceWithBoundProps({ first: wrongValue })
Component - a svelte component class to be checked for a matching instancetoHaveInstanceWithSlotsPasses if a mocked component class has an instance with the specified slots.
<Component>
<span>First</span>
</Component>
<Component>
<span slot="first">First</span>
<span slot="second">Second</span>
</Component>
// Check for unnamed slot
expect(Component).toHaveInstanceWithSlots()
// Check for named slots
expect(Component).toHaveInstanceWithSlots(['first'])
expect(Component).toHaveInstanceWithSlots(['first', 'second'])
expect(Component).not.toHaveInstanceWithSlots(['nonExistent'])
expect(Component).toHaveInstanceWithSlots({ first: firstSlot })
expect(Component).not.toHaveInstanceWithSlots({ first: wrongSlot })
Component - a svelte component class to be checked for a matching instancetoHaveInstanceWithEventHandlersPasses if a mocked component class has an instance with event handlers.
<Component on:click="clickFn()" on:custom="customFn()" />
expect(Component).toHaveInstanceWithEventHandlers(['click'])
expect(Component).toHaveInstanceWithEventHandlers(['click', 'custom'])
expect(Component).not.toHaveInstanceWithEventHandlers(['nonExistent'])
expect(Component).toHaveInstanceWithEventHandlers({ click: clickFn })
expect(Component).not.toHaveInstanceWithEventHandlers({ click: wrongFn })
Component - a svelte component class to be checked for a matching instancetoHavePropsPasses if a component instance has the specified props.
<Component first={firstValue} second={secondValue} />
expect(component).toHaveProps(['first'])
expect(component).toHaveProps(['first', 'second'])
expect(component).not.toHaveProps(['nonExistent'])
expect(component).toHaveProps({ first: firstValue })
expect(component).not.toHaveProps({ first: wrongValue })
component - an instance of ComponenttoHaveBoundPropsPasses if a component instance has the specified bound props.
<Component bind:first=firstValue bind:second=secondValue />
expect(component).toHaveBoundProps(['first'])
expect(component).toHaveBoundProps(['first', 'second'])
expect(component).not.toHaveBoundProps(['nonExistent'])
expect(component).toHaveBoundProps({ first: firstValue })
expect(component).not.toHaveBoundProps({ first: wrongValue })
component - an instance of ComponenttoHaveSlotsPasses if a component instance has the specified slots.
<Component>
<span>First</span>
</Component>
<Component>
<span slot="first">First</span>
<span slot="second">Second</span>
</Component>
// Check for unnamed slot
expect(component1).toHaveSlots()
// Check for named slots
expect(component2).toHaveSlots(['first'])
expect(component2).toHaveSlots(['first', 'second'])
expect(component2).not.toHaveSlots(['nonExistent'])
expect(component2).toHaveSlots({ first: firstSlot })
expect(component2).not.toHaveSlots({ first: wrongSlot })
component1 - an instance of Componentcomponent2 - an instance of ComponenttoHaveEventHandlersPasses if a component instance has the specified event handlers.
<Component on:click="clickFn()" on:custom="customFn()" />
expect(component).toHaveEventHandlers(['click'])
expect(component).toHaveEventHandlers(['click', 'custom'])
expect(component).not.toHaveEventHandlers(['nonExistent'])
expect(component).toHaveEventHandlers({ click: clickFn })
expect(component).not.toHaveEventHandlers({ click: wrongFn })
component - an instance of ComponentgetInstanceByPropsReturns the first instance for a component that has the specified props.
<Component first={firstValue} second={secondValue} />
Component.getInstanceByProps(['first'])
Component.getInstanceByProps(['first', 'second'])
Component.getInstanceByProps({ first: firstValue })
Component - a svelte component class to be checked for an instancegetInstanceByBoundPropsReturns the first instance for a component that has the specified bound props.
<Component bind:first=firstValue bind:second=secondValue />
Component.getInstanceByBoundProps(['first'])
Component.getInstanceByBoundProps(['first', 'second'])
Component.getInstanceByBoundProps({ first: firstValue })
Component - a svelte component class to be checked for an instancegetInstanceBySlotsReturns the first instance for a component that has the specified slots.
<Component>
<span>First</span>
</Component>
<Component>
<span slot="first">First</span>
<span slot="second">Second</span>
</Component>
// Get component with unnamed slot
Component.getInstanceBySlots()
// Check for named slots
Component.getInstanceBySlots(['first'])
Component.getInstanceBySlots(['first', 'second'])
Component.getInstanceBySlots({ first: firstSlot })
Component - a svelte component class to be checked for an instancegetInstanceByEventHandlersReturns the first instance for a component that has the specified event handlers.
<Component on:click="clickFn()" on:custom="customFn()" />
Component.getInstanceByEventHandlers(['click'])
Component.getInstanceByEventHandlers(['click', 'custom'])
Component.getInstanceByEventHandlers({ click: clickFn })
Component - a svelte component class to be checked for an instanceFAQs
A package for mocking svelte components with jest.
The npm package svelte-mock receives a total of 689 weekly downloads. As such, svelte-mock popularity was classified as not popular.
We found that svelte-mock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.