
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
vitest-launchdarkly-mock
Advanced tools
Easily unit test LaunchDarkly feature flagged components with Vitest
Easily unit test LaunchDarkly feature flagged components with Vitest :clap:
This package is a rip-off from jest-launchdarkly-mock, just adapted for Vitest.
This package is only compatible with React SDK.
yarn add -D vitest-launchdarkly-mock
or
npm install vitest-launchdarkly-mock --save-dev
Then in vite.config.js
add vitest-launchdarkly-mock
to setupFiles:
// vite.config.ts
export default defineConfig(() => {
return {
test: {
setupFiles: ['./node_modules/vitest-launchdarkly-mock/dist/index.js'],
}
}
});
Use the only 3 apis for test cases:
mockFlags(flags: LDFlagSet)
: mock flags at the start of each test case. Only mocks
flags returned by the useFlags
hook.
ldClientMock
: a Vitest mock of the ldClient. All
methods of this object are Vitest mocks.
resetLDMocks
: resets both mockFlags and ldClientMock.
import { mockFlags, ldClientMock, resetLDMocks } from 'vitest-launchdarkly-mock'
describe('button', () => {
beforeEach(() => {
// reset before each test case
resetLDMocks()
})
test('flag on', () => {
// arrange
// You can use the original unchanged case, kebab-case, camelCase or snake_case keys.
mockFlags({ devTestFlag: true })
// act
const { getByTestId } = render(<Button />)
// assert
expect(getByTestId('test-button')).toBeTruthy()
})
test('identify', () => {
// arrange
mockFlags({ 'dev-test-flag': true })
// act
const { getByTestId } = render(<Button />)
fireEvent.click(getByTestId('test-button'))
// assert: identify gets called
expect(ldClientMock.identify).toBeCalledWith({ key: 'aa0ceb' })
})
})
FAQs
Easily unit test LaunchDarkly feature flagged components with Vitest
We found that vitest-launchdarkly-mock demonstrated a healthy version release cadence and project activity because the last version was released less than 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
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.