-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Storybook addon Jest angular suport #3532
Changes from 6 commits
dfab0e1
f8ff98d
b2b1b9f
a06dc24
7bdc6e8
068e9ba
499d43f
193a053
2c99646
4ca019a
db61595
76fad9b
489ca4c
0ca9f06
c735809
507ec7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,6 +118,48 @@ storiesOf('MyComponent', module) | |
- **options.results**: OBJECT jest output results. *mandatory* | ||
- **filesExt**: STRING test file extention. *optionnal*. This allow you to write "MyComponent" and not "MyComponent.test.js". It will be used as regex to find your file results. Default value is `((\\.specs?)|(\\.tests?))?(\\.js)?$`. That mean it will match: MyComponent.js, MyComponent.test.js, MyComponent.tests.js, MyComponent.spec.js, MyComponent.specs.js... | ||
|
||
## Usage with Angular | ||
|
||
Assuming that you have created a test files `my.component.spec.ts` and `my-other.comonent.spec.ts` | ||
|
||
Config Jest with [jest-preset-angular](https://www.npmjs.com/package/jest-preset-angular) | ||
|
||
In project `typings.d.ts` add | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. *In project's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
```ts | ||
declare module '*.json' { | ||
const value: any; | ||
export default value; | ||
} | ||
``` | ||
|
||
Create a simple file `withTests.ts`: | ||
|
||
```ts | ||
import * as results from '../.jest-test-results.json'; | ||
import { withTests } from '@storybook/addon-jest'; | ||
|
||
export const wTests = withTests({ | ||
results, | ||
filesExt: '((\\.specs?)|(\\.tests?))?(\\.ts)?$' | ||
}); | ||
``` | ||
|
||
Then in your story: | ||
|
||
```js | ||
// import your file | ||
import wTests from '.withTests'; | ||
|
||
storiesOf('MyComponent', module) | ||
.addDecorator(wTests('my.component', 'my-other.component')) | ||
.add('This story shows test results from MyComponent.test.js and MyOtherComponent.test.js', () => ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need only name. In
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean, you wrote above
So I think it's better to have the same naming There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
<div>Jest results in storybook</div> | ||
)); | ||
``` | ||
|
||
##### Example [here](https://github.com/storybooks/storybook/tree/master/examples/angular-cli) | ||
|
||
## TODO | ||
|
||
- [ ] Add coverage | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,4 @@ yarn-error.log | |
# System Files | ||
.DS_Store | ||
Thumbs.db | ||
.jest-test-results.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as results from '../.jest-test-results.json'; | ||
import { withTests } from '@storybook/addon-jest'; | ||
|
||
export const wTests = withTests({ | ||
results, | ||
filesExt: '((\\.specs?)|(\\.tests?))?(\\.ts)?$' | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const mock = () => { | ||
let storage = {}; | ||
return { | ||
getItem: key => (key in storage ? storage[key] : null), | ||
setItem: (key, value) => (storage[key] = value || ''), | ||
removeItem: key => delete storage[key], | ||
clear: () => (storage = {}) | ||
}; | ||
}; | ||
|
||
Object.defineProperty(window, 'localStorage', { | ||
value: mock() | ||
}); | ||
Object.defineProperty(window, 'sessionStorage', { | ||
value: mock() | ||
}); | ||
Object.defineProperty(window, 'getComputedStyle', { | ||
value: () => ['-webkit-appearance'] | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import 'jest-preset-angular'; | ||
import './globalMocks'; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,16 @@ | |
"license": "MIT", | ||
"scripts": { | ||
"build": "ng build", | ||
"build-storybook": "build-storybook -s src", | ||
"build-storybook": "npm run storybook:prebuild && build-storybook -s src", | ||
"e2e": "ng e2e", | ||
"ng": "ng", | ||
"start": "ng serve", | ||
"storybook": "start-storybook -p 9008 -s src/assets", | ||
"test": "ng test" | ||
"storybook:prebuild": "npm run test:generate-output", | ||
"storybook": "npm run storybook:prebuild && start-storybook -p 9008 -s src/assets", | ||
"test": "jest", | ||
"test:watch": "jest --watch", | ||
"test:coverage": "jest --coverage", | ||
"test:generate-output": "jest --json --outputFile=.jest-test-results.json || true" | ||
}, | ||
"dependencies": { | ||
"@angular/common": "^5.2.10", | ||
|
@@ -28,6 +32,7 @@ | |
"@angular/cli": "1.7.4", | ||
"@angular/compiler-cli": "^5.2.10", | ||
"@storybook/addon-actions": "4.0.0-alpha.4", | ||
"@storybook/addon-jest": "^3.4.3", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please update to the 4.0.0-alpha (and bootstrap again, because lock file is wrong now) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed) |
||
"@storybook/addon-knobs": "4.0.0-alpha.4", | ||
"@storybook/addon-links": "4.0.0-alpha.4", | ||
"@storybook/addon-notes": "4.0.0-alpha.4", | ||
|
@@ -38,19 +43,31 @@ | |
"@storybook/angular": "4.0.0-alpha.4", | ||
"@types/jasmine": "~2.8.6", | ||
"@types/jasminewd2": "^2.0.3", | ||
"@types/jest": "^22.2.3", | ||
"@types/node": "~9.6.7", | ||
"babel-core": "^6.26.3", | ||
"global": "^4.3.2", | ||
"jasmine-core": "~3.1.0", | ||
"jasmine-spec-reporter": "~4.2.1", | ||
"karma": "~2.0.2", | ||
"karma-chrome-launcher": "~2.2.0", | ||
"karma-cli": "~1.0.1", | ||
"karma-coverage-istanbul-reporter": "^1.4.2", | ||
"karma-jasmine": "~1.1.0", | ||
"karma-jasmine-html-reporter": "^1.0.0", | ||
"jest": "^22.4.3", | ||
"jest-preset-angular": "^5.2.2", | ||
"protractor": "~5.3.1", | ||
"react": "^16.3.2", | ||
"react-dom": "^16.3.2", | ||
"ts-node": "~6.0.2", | ||
"typescript": "^2.8.3" | ||
}, | ||
"jest": { | ||
"preset": "jest-preset-angular", | ||
"testPathIgnorePatterns": [ | ||
"/node_modules/", | ||
"/build/", | ||
"angularshots.test.js" | ||
], | ||
"coveragePathIgnorePatterns": [ | ||
"/jest-config/", | ||
"/node_modules/" | ||
], | ||
"setupTestFrameworkScriptFile": "./jest-config/setup.ts" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Storyshots Addon|Jest app.component with jest tests 1`] = ` | ||
<storybook-dynamic-app-root | ||
cfr={[Function CodegenComponentFactoryResolver]} | ||
data={[Function Object]} | ||
target={[Function ViewContainerRef_]} | ||
> | ||
<storybook-app-root> | ||
|
||
|
||
<div | ||
class="hide" | ||
style="color: red; font-size: 30px; text-align: center;" | ||
> | ||
|
||
This should be hidden, if not - scss is not loaded as needed. | ||
|
||
</div> | ||
|
||
|
||
<div | ||
style="text-align:center" | ||
> | ||
|
||
|
||
<h1> | ||
|
||
Welcome to app! | ||
|
||
</h1> | ||
|
||
|
||
<img | ||
src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxOS4xLjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjxzdmcgdmVyc2lvbj0iM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<h2> | ||
Here are some links to help you start: | ||
</h2> | ||
|
||
|
||
<ul> | ||
|
||
|
||
<li> | ||
|
||
|
||
<h2> | ||
<a | ||
href="https://angular.io/tutorial" | ||
target="_blank" | ||
> | ||
Tour of Heroes | ||
</a> | ||
</h2> | ||
|
||
|
||
</li> | ||
|
||
|
||
<li> | ||
|
||
|
||
<h2> | ||
<a | ||
href="https://github.com/angular/angular-cli/wiki" | ||
target="_blank" | ||
> | ||
CLI Documentation | ||
</a> | ||
</h2> | ||
|
||
|
||
</li> | ||
|
||
|
||
<li> | ||
|
||
|
||
<h2> | ||
<a | ||
href="https://blog.angular.io//" | ||
target="_blank" | ||
> | ||
Angular blog | ||
</a> | ||
</h2> | ||
|
||
|
||
</li> | ||
|
||
|
||
</ul> | ||
|
||
|
||
|
||
</storybook-app-root> | ||
</storybook-dynamic-app-root> | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { storiesOf } from '@storybook/angular'; | ||
import { AppComponent } from '../app/app.component'; | ||
import { wTests } from '../../.storybook/withTests'; | ||
|
||
storiesOf('Addon|Jest', module) | ||
.addDecorator(wTests('app.component')) | ||
.add('app.component with jest tests', () => ({ | ||
component: AppComponent, | ||
props: {}, | ||
})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*Configure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done