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

Commit 35d2092

Browse files
authored
feat: mocking ES6 imports (#329)
```js import Hello from './Hello.vue' import { mount } from 'cypress-vue-unit-test' import * as GreetingModule from './greeting' cy.stub(GreetingModule, 'greeting').returns('Cypress').as('greeting') mount(Hello) cy.contains('Hello, Cypress!') // confirm the stub was called cy.get('@Greeting').should('have.been.calledOnce') ```
1 parent d9e9f45 commit 35d2092

File tree

11 files changed

+252
-45
lines changed

11 files changed

+252
-45
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,20 @@ npm install
718718
npm run cy:open
719719
```
720720

721+
### Debugging
722+
723+
Run Cypress with environment variable
724+
725+
```
726+
DEBUG=cypress-vue-unit-test
727+
```
728+
729+
If some deeply nested objects are abbreviated and do not print fully, set the maximum logging depth
730+
731+
```
732+
DEBUG=cypress-vue-unit-test DEBUG_DEPTH=10
733+
```
734+
721735
<a name="#faq"/>
722736

723737
## FAQ

cypress/component/advanced/mocking-imports/Hello.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default {
99
name: "Hello",
1010
data() {
1111
return {
12-
greeting
12+
greeting: greeting()
1313
};
1414
}
1515
};

cypress/component/advanced/mocking-imports/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
Vue component in [Hello.vue](Hello.vue) imports a named ES6 import from [greeting.js](greeting.js). From the test [spec.js](spec.js) we can mock that import to make testing simpler.
44

5-
Compare no mocking
5+
![Test with mocking and without](images/mocking.png)
66

7-
![Test without mocking](images/no-mocking.png)
7+
The imports mocking is done using `@babel/plugin-transform-modules-commonjs` inserted as a `babel-loader` plugin automatically.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const greeting = 'world'
1+
export const greeting = () => 'world'
253 KB
Loading
-163 KB
Binary file not shown.

cypress/component/advanced/mocking-imports/spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ describe('Mocking ES6 imports', () => {
1414
})
1515

1616
// https://github.com/bahmutov/cypress-vue-unit-test/issues/328
17-
it.skip('shows mocked greeting', () => {
18-
cy.stub(GreetingModule, 'greeting').returns('Cypress')
17+
it('shows mocked greeting', () => {
18+
cy.stub(GreetingModule, 'greeting').returns('Cypress').as('greeting')
1919
mount(Hello)
2020
cy.contains('Hello, Cypress!')
21+
// confirm the stub was called
22+
cy.get('@greeting').should('have.been.calledOnce')
2123
})
2224
})

package-lock.json

Lines changed: 197 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"@babel/core": "7.9.0",
5757
"@babel/preset-env": "7.9.5",
5858
"axios": "0.19.2",
59+
"babel-loader": "8.1.0",
5960
"ban-sensitive-files": "1.9.7",
6061
"css-loader": "3.4.2",
6162
"cypress": "4.9.0",
@@ -68,11 +69,11 @@
6869
"semantic-release": "^17.1.1",
6970
"standard": "12.0.1",
7071
"tailwindcss": "1.1.4",
72+
"vue": "2.6.11",
7173
"vue-i18n": "7.8.1",
7274
"vue-loader": "14.2.4",
7375
"vue-router": "3.0.5",
7476
"vue-template-compiler": "2.6.11",
75-
"vue": "2.6.11",
7677
"vuex": "3.1.1",
7778
"webpack": "4.42.0"
7879
},
@@ -85,12 +86,15 @@
8586
},
8687
"peerDependencies": {
8788
"vue": "2.x",
88-
"cypress": ">=4.5.0"
89+
"cypress": ">=4.5.0",
90+
"babel-loader": "8"
8991
},
9092
"dependencies": {
93+
"@babel/plugin-transform-modules-commonjs": "7.10.4",
9194
"@cypress/webpack-preprocessor": "5.4.1",
9295
"@kazupon/vue-i18n-loader": "0.5.0",
9396
"common-tags": "1.8.0",
97+
"debug": "4.1.1",
9498
"find-webpack": "2.0.0"
9599
}
96100
}

preprocessor/webpack.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const webpack = require('webpack')
33
// Cypress webpack bundler adaptor
44
// https://github.com/cypress-io/cypress-webpack-preprocessor
55
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
6+
const debug = require('debug')('cypress-vue-unit-test')
67

78
const fw = require('find-webpack')
89
const webpackOptions = fw.getWebpackOptions()
@@ -48,9 +49,33 @@ function compileTemplate (options = {}) {
4849
options.resolve.alias = options.resolve.alias || {}
4950
options.resolve.alias['vue$'] = 'vue/dist/vue.esm.js'
5051
}
52+
53+
/**
54+
* Warning: modifies the input object
55+
*/
56+
function insertBabelLoader(options) {
57+
options.module.rules.push({
58+
test: /\.js$/,
59+
loader: 'babel-loader',
60+
options: {
61+
plugins: [
62+
[
63+
'@babel/plugin-transform-modules-commonjs',
64+
{
65+
loose: true,
66+
}
67+
]
68+
]
69+
},
70+
})
71+
}
72+
5173
inlineUrlLoadedAssets(webpackOptions)
5274
preventChunking(webpackOptions)
5375
compileTemplate(webpackOptions)
76+
insertBabelLoader(webpackOptions)
77+
78+
debug('final webpack %o', webpackOptions)
5479

5580
/**
5681
* Basic Cypress Vue Webpack file loader for .vue files

0 commit comments

Comments
 (0)