Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

chore: enable cucumber, rewrite existing tests #513

Merged
merged 15 commits into from Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -28,3 +28,4 @@ bundle.stats.json

# cypress
cypress/screenshots
cypress/videos
4 changes: 2 additions & 2 deletions .storybook/config.js
Expand Up @@ -14,15 +14,15 @@ addDecorator(FrameStylesWrapper)
const includeTesting = 'STORYBOOK_INCLUDE_TESTING' in process.env

function loadStories() {
const req = require.context('../stories', false, /\.stories\.js$/)
const req = require.context('../stories', true, /\.stories\.js$/)
req.keys().forEach(filename => req(filename))
}

function loadStoriesInclTesting() {
const req = require.context(
'../stories',
true,
/(\.testing)?\.stories\.js$/
/\.stories(\.testing)?\.js$/
)
req.keys().forEach(filename => req(filename))
}
Expand Down
24 changes: 16 additions & 8 deletions README.md
Expand Up @@ -71,19 +71,27 @@ automatically determine the next version.

## Testing

In order to run the cypress tests:
1. Run `yarn start:test`
(This will start the storybook, including testing stories
and doesn't launch the browser)
2. Run `yarn cypress:open`
Testing is done with cypress & cucumber.

Once the storybook is available, you can run the tests from within
the cypress application that has been launched in step 2.
* Run `yarn cypress:run`<br />
This will run cypress and exit with either 0 or 1

* Run `yarn cypress:open`<br />
This will open the cypress gui, which is useful for writing tests

### Recording videos and taking screenshots

When running `yarn cypress:run`, by default no video is recorded and no
screenshot will be taken.
* Recording videos can be enabled by supplying the
`CYPRESS_VIDEO=true` env var.
* Taking screenshots can be enabled by supplying the
`CYPRESS_SCREENSHOT=true` env var.

### Storybook stories for testing

Sometimes it's required to add stateful stories to test certain behavior.
That's why you can add files with the following file name format: `*.testing.stories.js`
That's why you can add files with the following file name format: `*.stories.testing.js`
These stories will not be used when generating the docs storybook and can
contain more sophisticated scenarios for testing.

Expand Down
7 changes: 3 additions & 4 deletions cypress.json
@@ -1,6 +1,5 @@
{
"screenshots": {
"screenshotOnRunFailure": false
},
"projectId": "65oh1u"
"testFiles": "**/*.feature",
"video": false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we setting video: false here? Seems like we want videos when doing recorded CI runs...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reason as for the screenshots: I think this should be explicitly turned on for CI. During development this won't be needed as we're probably debugging in the browser anyways.

These files will be git-ignored, so on the one hand devs won't necessarily notice these files have been created and on the other hand is the amount of disk space used by videos quite a bit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you're right, totally fair! I'd used the --video false flag for that, but putting it in the config file is definitely better if we can override it explicitly when running in CI. Nice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.. seems like right now that's not an option: kimmobrunfeldt/concurrently#33
So I'm inclined to remove these options from cypress.json until this 3.5 year old issue has been solved..... Do you agree?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit more complicated. We'd have to set the config with code, depending on an env var, and I wasn't successful in doing so (I didn't try extensively though).

I think for now it's ok to remove it from the config and do this on a new/another branch/PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works (with "video": false in cypress.json)

CYPRESS_VIDEO=true cypress run

I think you're right, it's better not to create screenshots or videos on a dev machine so let's default those to false in cypress.json (as you had done) and override them with the env vars in .travis.yml

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(no configuration with code required)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ @Mohammer5 (and yeah, not sure about the Semantic status check, think @varl might have changed the settings for this repo to require all commits to be semantic, since this is merge-rebased?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this functionality for taking screenshots manually as that didn't work.
Works for videos out of the box.

"projectId": "65oh1u"
}
10 changes: 10 additions & 0 deletions cypress/.eslintrc.js
@@ -0,0 +1,10 @@
const config = require('../.eslintrc.js')

module.exports = {
...config,
env: { es6: true },
globals: {
Cypress: 'readonly',
cy: 'readonly',
},
}
Empty file added cypress/fixtures/.gitkeep
Empty file.
39 changes: 0 additions & 39 deletions cypress/integration/AlertBar.spec.js

This file was deleted.

@@ -0,0 +1,6 @@
Feature: The AlertBar will hide when the user clicks an action

Scenario: The user clicks the "Cancel" action
Given an AlertBar has a "Cancel" action
When the user clicks on the "Cancel" button
Then the AlertBar will not be visible
@@ -0,0 +1,14 @@
import '../common/index'
import { Given, When } from 'cypress-cucumber-preprocessor/steps'

Given('an AlertBar has a "Cancel" action', () => {
cy.visitStory('AlertBar', 'With actions')
.get('.info')
.as('AlertBar')

cy.get('@AlertBar').should('be.visible')
})

When('the user clicks on the "Cancel" button', () => {
cy.get('span:contains("Cancel")').click()
})
@@ -0,0 +1,11 @@
Feature: The AlertBars will hide automatically

Scenario: AlertBars hides automatically after the default time
Given an AlertBar will hide after the default time
When the default time has passed
Then the AlertBar will not be visible

Scenario: AlertBars hides automatically after a custom time
Given an AlertBar will hide after a custom time
When the custom time has passed
Then the AlertBar will not be visible
@@ -0,0 +1,14 @@
import '../common/index'
import { Given, When } from 'cypress-cucumber-preprocessor/steps'

Given('an AlertBar will hide after a custom time', () => {
cy.visitStory('AlertBar', 'Auto hiding')
.get('.fourth')
.as('AlertBar')

cy.get('@AlertBar').should('be.visible')
})

When('the custom time has passed', () => {
cy.wait(2000)
})
@@ -0,0 +1,15 @@
import '../common/index'
import { Given, When } from 'cypress-cucumber-preprocessor/steps'
import { AlertBar } from '../../../../src'

Given('an AlertBar will hide after the default time', () => {
cy.visitStory('AlertBar', 'auto hiding')
.get('.fifth')
.as('AlertBar')

cy.get('@AlertBar').should('be.visible')
})

When('the default time has passed', () => {
cy.wait(AlertBar.defaultProps.duration)
})
5 changes: 5 additions & 0 deletions cypress/integration/AlertBar/common/index.js
@@ -0,0 +1,5 @@
import { Then } from 'cypress-cucumber-preprocessor/steps'

Then('the AlertBar will not be visible', () => {
cy.get('@AlertBar').should('not.exist')
})
7 changes: 6 additions & 1 deletion cypress/plugins/index.js
@@ -1 +1,6 @@
module.exports = () => {}
const cucumber = require('cypress-cucumber-preprocessor').default

// eslint-disable-next-line no-unused-vars
module.exports = (on, config) => {
on('file:preprocessor', cucumber())
}
2 changes: 2 additions & 0 deletions cypress/support/index.js
@@ -0,0 +1,2 @@
import './screenshots'
import './visitStory'
5 changes: 5 additions & 0 deletions cypress/support/screenshots.js
@@ -0,0 +1,5 @@
const takeScreenshots = !!Cypress.env('SCREENSHOT')

Cypress.Screenshot.defaults({
screenshotOnRunFailure: takeScreenshots,
})
6 changes: 6 additions & 0 deletions cypress/support/visitStory.js
@@ -0,0 +1,6 @@
Cypress.Commands.add('visitStory', (namespace, storyName) => {
const formattedNamespace = namespace.toLowerCase()
const formattedStoryName = storyName.replace(/ /g, '-').toLowerCase()
const id = `${formattedNamespace}--${formattedStoryName}`
cy.visit(`iframe.html?id=${id}`)
})
20 changes: 15 additions & 5 deletions package.json
Expand Up @@ -18,9 +18,13 @@
"build": "NODE_ENV=production yarn build:commonjs && NODE_ENV=production yarn build:modules",
"postbuild": "NODE_ENV=production yarn build-storybook",
"build-storybook": "build-storybook -c .storybook -o ./build/docs && cp -r ./stories/info/images ./build/docs",
"start-storybook": "start-storybook --port 5000 -s ./stories/info",
"start": "yarn start-storybook",
"cypress:open": "cypress open"
"start-storybook": "start-storybook -s ./stories/info",
"start": "yarn start-storybook --port 5000",
"start:testing": "STORYBOOK_INCLUDE_TESTING=1 yarn start --port $PORT --ci",
"cypress:browser": "cypress open --config baseUrl=http://localhost:$PORT",
"cypress:ci": "NO_COLOR=1 cypress run -b $BROWSER --config baseUrl=http://localhost:$PORT",
"cypress:open": "PORT=12345 concurrently --kill-others -n storybook,cypress 'yarn start:testing' 'wait-on http-get://localhost:$PORT && yarn cypress:browser'",
"cypress:run": "PORT=12345 concurrently --kill-others --success first -n storybook,cypress 'yarn start:testing' 'wait-on http-get://localhost:$PORT && yarn cypress:ci'"
},
"devDependencies": {
"@babel/cli": "^7.6.4",
Expand All @@ -37,14 +41,17 @@
"@storybook/react": "^5.2.5",
"babel-eslint": "10.x",
"babel-loader": "^8.0.6",
"concurrently": "^5.0.0",
"cypress": "^3.4.1",
"cypress-cucumber-preprocessor": "^1.16.2",
"eslint-plugin-react": "^7.16.0",
"react": "16.3",
"react-dev-utils": "^9.1.0",
"react-dom": "16.3",
"storybook-addon-jsx": "^7.1.6",
"storybook-addon-react-docgen": "^1.2.28",
"typeface-roboto": "^0.0.75"
"typeface-roboto": "^0.0.75",
"wait-on": "^3.3.0"
},
"peerDependencies": {
"prop-types": "^15",
Expand All @@ -58,5 +65,8 @@
},
"files": [
"build"
]
],
"cypress-cucumber-preprocessor": {
"nonGlobalStepDefinitions": true
}
}