Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit ba0d4e0

Browse files
committed
feat: add webpack preprocessor, close #2
1 parent b320269 commit ba0d4e0

File tree

5 files changed

+141
-75
lines changed

5 files changed

+141
-75
lines changed

README.md

Lines changed: 79 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -289,58 +289,8 @@ p {
289289
</style>
290290
```
291291

292-
How do we load this Vue file into the testing code? Using [@cypress/webpack-preprocessor](https://github.com/cypress-io/cypress-webpack-preprocessor#readme) and [vue-loader](https://github.com/vuejs/vue-loader).
293-
You can use [cypress/plugins/index.js](cypress/plugins/index.js) to load `.vue` files
294-
using `vue-loader`.
295-
296-
```js
297-
// cypress/plugins/index.js
298-
const webpack = require('@cypress/webpack-preprocessor')
299-
const webpackOptions = {
300-
module: {
301-
rules: [
302-
{
303-
test: /\.vue$/,
304-
loader: 'vue-loader'
305-
}
306-
]
307-
}
308-
}
309-
310-
const options = {
311-
// send in the options from your webpack.config.js, so it works the same
312-
// as your app's code
313-
webpackOptions,
314-
watchOptions: {}
315-
}
316-
317-
module.exports = on => {
318-
on('file:preprocessor', webpack(options))
319-
}
320-
```
321-
322-
Install dev dependencies
323-
324-
```shell
325-
npm i -D @cypress/webpack-preprocessor \
326-
vue-loader vue-template-compiler css-loader
327-
```
328-
329-
And write a test
330-
331-
```js
332-
import Hello from '../../components/Hello.vue'
333-
const mountVue = require('cypress-vue-unit-test')
334-
335-
/* eslint-env mocha */
336-
describe('Hello.vue', () => {
337-
beforeEach(mountVue(Hello))
338-
339-
it('shows hello', () => {
340-
cy.contains('Hello World!')
341-
})
342-
})
343-
```
292+
**note** to learn how to load Vue component files in Cypress, see
293+
[Bundling](#bundling) section.
344294

345295
Do you want to interact with the component? Go ahead! Do you want
346296
to have multiple components? No problem!
@@ -438,6 +388,83 @@ and is emitting an event.
438388

439389
[cypress.io]: https://www.cypress.io/
440390

391+
## Bundling
392+
393+
How do we load this Vue file into the testing code? Using webpack preprocessor.
394+
395+
### Short way
396+
397+
Your project probably already has `webpack.config.js` setup to transpile
398+
`.vue` files. To load these files in the Cypress tests, grab the webpack
399+
processor included in this module, and load it from the `cypress/plugins/index.js`
400+
file.
401+
402+
```js
403+
const {
404+
onFilePreprocessor
405+
} = require('cypress-vue-unit-test/preprocessor/webpack')
406+
module.exports = on => {
407+
on('file:preprocessor', onFilePreprocessor('../path/to/webpack.config'))
408+
}
409+
```
410+
411+
Cypress should be able to import `.vue` files in the tests
412+
413+
### Manual
414+
415+
Using [@cypress/webpack-preprocessor](https://github.com/cypress-io/cypress-webpack-preprocessor#readme) and [vue-loader](https://github.com/vuejs/vue-loader).
416+
You can use [cypress/plugins/index.js](cypress/plugins/index.js) to load `.vue` files
417+
using `vue-loader`.
418+
419+
```js
420+
// cypress/plugins/index.js
421+
const webpack = require('@cypress/webpack-preprocessor')
422+
const webpackOptions = {
423+
module: {
424+
rules: [
425+
{
426+
test: /\.vue$/,
427+
loader: 'vue-loader'
428+
}
429+
]
430+
}
431+
}
432+
433+
const options = {
434+
// send in the options from your webpack.config.js, so it works the same
435+
// as your app's code
436+
webpackOptions,
437+
watchOptions: {}
438+
}
439+
440+
module.exports = on => {
441+
on('file:preprocessor', webpack(options))
442+
}
443+
```
444+
445+
Install dev dependencies
446+
447+
```shell
448+
npm i -D @cypress/webpack-preprocessor \
449+
vue-loader vue-template-compiler css-loader
450+
```
451+
452+
And write a test
453+
454+
```js
455+
import Hello from '../../components/Hello.vue'
456+
const mountVue = require('cypress-vue-unit-test')
457+
458+
/* eslint-env mocha */
459+
describe('Hello.vue', () => {
460+
beforeEach(mountVue(Hello))
461+
462+
it('shows hello', () => {
463+
cy.contains('Hello World!')
464+
})
465+
})
466+
```
467+
441468
### Small print
442469

443470
Author: Gleb Bahmutov &lt;[email protected]&gt; &copy; 2017

cypress/plugins/index.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1-
// https://github.com/cypress-io/cypress-webpack-preprocessor
2-
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
1+
// default webpack file preprocessor is good for simple cases
2+
const { onFileDefaultPreprocessor } = require('../../preprocessor/webpack')
33

4-
// Cypress webpack options or just require from
5-
// an existing webpack.config.js
6-
const webpackOptions = {
7-
module: {
8-
rules: [
9-
{
10-
test: /\.vue$/,
11-
loader: 'vue-loader'
12-
}
13-
]
14-
}
4+
module.exports = on => {
5+
on('file:preprocessor', onFileDefaultPreprocessor)
156
}
167

17-
const options = {
18-
webpackOptions
19-
}
8+
/*
9+
for more complex cases, when the project already includes webpack.config.js
2010
21-
module.exports = on => {
22-
on('file:preprocessor', webpackPreprocessor(options))
23-
}
11+
const {
12+
onFilePreprocessor
13+
} = require('cypress-vue-unit-test/preprocessor/webpack')
14+
module.exports = on => {
15+
on('file:preprocessor', onFilePreprocessor('../path/to/webpack.config'))
16+
}
17+
*/

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
},
3131
"files": [
3232
"src/*.js",
33+
"preprocessor/*.js",
3334
"!src/*-spec.js"
3435
],
3536
"homepage": "https://github.com/bahmutov/cypress-vue-unit-test#readme",
@@ -52,10 +53,10 @@
5253
"deps": "deps-ok && dependency-check --no-dev .",
5354
"issues": "git-issues",
5455
"license": "license-checker --production --onlyunknown --csv",
55-
"lint": "standard --verbose --fix src/*.js",
56+
"lint": "standard --verbose --fix src/*.js preprocessor/*.js",
5657
"prelint": "npm run pretty",
5758
"pretest": "npm run lint",
58-
"pretty": "prettier-standard 'src/*.js'",
59+
"pretty": "prettier-standard 'src/*.js' 'preprocessor/*.js'",
5960
"secure": "nsp check",
6061
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
6162
"test": "npm run cy:run",

preprocessor/webpack.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Cypress webpack bundler adaptor
2+
// https://github.com/cypress-io/cypress-webpack-preprocessor
3+
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
4+
5+
// default Cypress webpack options - good for basic projects
6+
const webpackOptions = {
7+
module: {
8+
rules: [
9+
{
10+
test: /\.vue$/,
11+
loader: 'vue-loader'
12+
}
13+
]
14+
}
15+
}
16+
17+
/**
18+
* Basic Cypress Vue Webpack file loader for .vue files
19+
*/
20+
const onFileDefaultPreprocessor = webpackPreprocessor({ webpackOptions })
21+
22+
/**
23+
* Custom Vue loader from the client projects that already have `webpack.config.js`
24+
*
25+
* @example
26+
* const {
27+
* onFilePreprocessor
28+
* } = require('cypress-vue-unit-test/preprocessor/webpack')
29+
* module.exports = on => {
30+
* on('file:preprocessor', onFilePreprocessor('../path/to/webpack.config'))
31+
* }
32+
*/
33+
const onFilePreprocessor = webpackOptions => {
34+
if (typeof webpackOptions === 'string') {
35+
// load webpack config from the given path
36+
webpackOptions = require(webpackOptions)
37+
}
38+
return webpackPreprocessor({
39+
webpackOptions
40+
})
41+
}
42+
43+
module.exports = { onFilePreprocessor, onFileDefaultPreprocessor }

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { stripIndent } = require('common-tags')
44
// and "losing" styles when the next test starts
55
const stylesCache = new Map()
66

7-
function copyStyles (component) {
7+
const copyStyles = component => {
88
let styles
99
if (stylesCache.has(component)) {
1010
styles = stylesCache.get(component)
@@ -35,7 +35,7 @@ function copyStyles (component) {
3535

3636
const deleteConstructor = comp => delete comp._Ctor
3737

38-
function deleteCachedConstructors (component) {
38+
const deleteCachedConstructors = component => {
3939
if (!component.components) {
4040
return
4141
}
@@ -68,6 +68,7 @@ const mountVue = (component, options = {}) => () => {
6868
document.write(vueHtml)
6969
document.close()
7070
})
71+
7172
cy
7273
.window()
7374
.its('Vue')

0 commit comments

Comments
 (0)