Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
jest-storybook-facade
Advanced tools
Facades for Jest and Storybook so that your Jest tests become stories
Jest Storybook Facade allows you to return "stories" (aka React elements) from your it
tests to produce
your React Storybook. Each describe
block is one "story", and its it
blocks the sub-stories. It's fantastic! Yay! Try it.
yarn add jest-storybook-facade
storybook config.js:
import 'jest-storybook-facade/storybook-facade.js'
import { configure } from '@kadira/storybook'
...
package.json:
"jest": {
"setupTestFrameworkScriptFile": "jest-storybook-facade/jest-facade.js",
...
import React from 'react'
import renderer from 'react-test-renderer'
import MyComponent from '../src/MyComponent'
import add from '../src/add'
describe('my test', () => {
it('does something', () => {
const element = <MyComponent />
const tree = renderer.create(element).toJSON()
expect(tree).toMatchSnapshot()
return element // will become a story in Storybook, yay!
})
it('does something', () => {
const value = myFunction(1, 2)
expect(value).toEqual(3)
// if you don't return anything, no story is displayed
})
})
Yay, it looks just like my regular tests! I can trust this won't break or become a nuisance to my all-import test harness!!!
...and in fact, you can get started today with your existing tests, just by returning react elements!
In Storybook land, globals for describe
, it
, expect
, etc are created by the storybook-facade.js
file you import. The
Jest environment is essentially mirrored. Under the hood, it creates your stories and spec tests using the specifications
addon.
In Jest land, a few minor tweaks are made by jest-facade.js
: For one, you can now return react elements without penality (typically
undefined or a promise is expected as the return). In addition to describe
, it
, jest
, etc, you have one more global: storybook
.
And of course it exists on both sides. Here's what it has on it in Jest land:
global.storybook = {
action: name => () => console.log('action', name),
linkTo: (component, story) => () => console.log('linkTo', component, story),
withReadme: readme => null,
knobs: {
text: (label, value) => value,
boolean: (label, value) => value,
number: (label, value, options) => value,
color: (label, value) => value,
object: (label, value) => value,
array: (label, value, separator = ',') => value,
select: (label, options, value) => value,
date: (label, value) => value,
},
stories: {
addDecorator: () => null,
add: () => null,
addWithInfo: () => null,
},
for this particular package, we urge you to analyze all the code. It's simple and small. You should know what's available to you, and how to add to it if we missed anything.
In this case, you can see that several Storybook addons such as knobs
and withReadme
is available to you, as well as the ability to
addDecorator
. In Storybook land, the real ones will of course be used. In general, the goal is to not obstruct the standard appearance
of your tests, but our assumption is that after you get used to it, you will want some of the Storybook features back.
Here's how an example of how to use it:
import React from 'react'
import renderer from 'react-test-renderer'
import readme from '../README.md'
const { color } = storybook.knobs
describe('my test', () => {
storybook.stories.addDecorator(withReadme(readme))
it('does something', () => {
const color = color('my color', 'blue')
const element = <MyComponent color={color} />
const tree = renderer.create(element).toJSON()
expect(tree).toMatchSnapshot()
return element
})
})
The Spec tests displayed in Storybook have one interesting difference from your tests when run by the Jest test runner:
storybook-facade.js
The mocks in storybook-facade
.js could be improved. We are missing a few expect
methods from Jest, and the jest.fn()
mock tool
could be better. Basically they should be copied from the Jest source. When I started this, I copied the initial way the specifications addon
did this, but since have decided to make this jest
-only. So that means we should use the precise tools from Jest.
Until we replace these things with code from the Jest repository, basically here's what you need to do:
const expectMethods = [
'toBeAn',
'toBeFalsy',
'toBeFewerThan',
'toBeMoreThan',
'toBeTruthy',
'toContain',
'toContainKey',
'addAnotherMethodHere',
...
expectReal.extend({
toMatchSnapshot() {
expectReal.assert(
true,
'expected a snapshot',
this.actual,
)
return this
},
addAnotherMethodHere...
})
and this should be improved:
const jest = {
fn: (implementation) => {
implementation = implementation || function jestFn() {}
implementation.mock = {
calls: [[], [], [], [], []],
instances: [{}, {}, {}, {}],
mockClear() {},
mockReset() {},
mockImplementation: () => implementation,
mockImplementationOnce: () => implementation,
mockReturnThis: () => implementation,
mockReturnValue: () => implementation,
mockReturnValueOnce: () => implementation,
}
return implementation
}
That all said, when your tests run in Storybook, for most use-cases you should be fine. If you need a specific enhancement, git clone
it,
make your enhancement, add your fork to package.json
instead, and make a pull request. It should be a fairly simple process to add what you need
for a package this basic.
In addition, it should be fairly simple for all of us together get this up to speed, so that we are not worrying about
what methods are missing in Storybook land. Making this jest
-only provides us that advantage. Our thinking is that Jest is (or will become) the
one true way for React testing. So lets reduce complexity by focusing on it.
FAQs
Facades for Jest and Storybook so that your Jest tests become stories
The npm package jest-storybook-facade receives a total of 6 weekly downloads. As such, jest-storybook-facade popularity was classified as not popular.
We found that jest-storybook-facade 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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.