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

Commit 7da59b8

Browse files
committed
merged
2 parents 8efc866 + c91993b commit 7da59b8

File tree

6 files changed

+98
-3
lines changed

6 files changed

+98
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ Spec | Description
598598
--- | ---
599599
[access-component](cypress/component/advanced/access-component) | Access the mounted component directly from test
600600
[i18n](cypress/component/advanced/i18n) | Testing component that uses [Vue I18n](https://kazupon.github.io/vue-i18n/) plugin
601-
[mocking-axios](cypress/component/advanced/mocking-axios) | Mocking 3rd party module imports, like `axios` for fetching data, **help wanted [#346](https://github.com/bahmutov/cypress-vue-unit-test/issues/346)**
601+
[mocking-axios](cypress/component/advanced/mocking-axios) | Mocking 3rd party module imports, like `axios` for fetching data using a wrapper module
602602
[mocking-components](cypress/component/advanced/mocking-components) | Mocking locally registered child components during tests
603603
[mocking-imports](cypress/component/advanced/mocking-imports) | Stub ES6 imports from the tests
604604
[render-functions](cypress/component/advanced/render-functions) | Mounting components with a [render function](https://www.tutorialandexample.com/vue-js-render-functions/)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// an intermediate wrapper around "axios" CommonJS module
2+
export * from 'axios'
Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,44 @@
11
# mocking axios
22

3-
**Help wanted**
3+
**Help wanted [#346](https://github.com/bahmutov/cypress-vue-unit-test/issues/346)**
44

5-
How can `import ... from '...'` be mocked from Vue and from JS spec files? In plain JS files we use '@babel/plugin-transform-modules-commonjs' plugin so that all imports from file X are the same object and the individual properties can be stubbed using `cy.stub(X, 'import name')`. But the `vue-loader` used to transpile Vue files seems to not allow additional Babel plugins?
5+
How can `import ... from '...'` be mocked from Vue and from JS spec files if the import is from `node_modules`? In plain JS files we use '@babel/plugin-transform-modules-commonjs' plugin so that all imports from file X are the same object and the individual properties can be stubbed using `cy.stub(X, 'import name')`. But the `vue-loader` used to transpile Vue files seems to not allow additional Babel plugins?
66

77
See [mock-get-spec.js](mock-get-spec.js) and [AxiosGet.vue](AxiosGet.vue) for an open problem.
8+
9+
## Workaround
10+
11+
As a good workaround in this case, you can use an intermediate CommonJS wrapper module, like [AxiosApi.js](AxiosApi.js) that re-experts the CommonJS module; you can then mock those exports.
12+
13+
```js
14+
// AxiosApi.js
15+
export * from 'axios'
16+
```
17+
18+
[Users.vue](Users.vue) shows the component that imports from `AxiosApi.js`
19+
20+
```js
21+
import { get } from './AxiosApi'
22+
```
23+
24+
The test [mock-axios-wrapper-spec.js](mock-axios-wrapper-spec.js) mocks the "get" import.
25+
26+
```js
27+
import Users from './Users.vue'
28+
import * as AxiosApi from './AxiosApi'
29+
30+
cy.stub(AxiosApi, 'get')
31+
.resolves({
32+
data: [
33+
{
34+
id: 101,
35+
name: 'Test User',
36+
},
37+
],
38+
})
39+
.as('get')
40+
41+
mount(Users)
42+
```
43+
44+
![Mocking axios wrapper exports](./images/wrapper.png)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<template>
2+
<div>
3+
<h1>Using Axios Wrapper</h1>
4+
<ul v-if="users && users.length">
5+
<li v-for="user of users" v-bind:key="user.id">
6+
<p><strong>{{user.id}}</strong> - {{user.name}}</p>
7+
</li>
8+
</ul>
9+
</div>
10+
</template>
11+
12+
<script>
13+
import {get} from './AxiosApi';
14+
15+
export default {
16+
data() {
17+
return {
18+
users: []
19+
}
20+
},
21+
22+
// Fetches posts when the component is created.
23+
created() {
24+
get('https://jsonplaceholder.cypress.io/users?_limit=3')
25+
.then(response => {
26+
// JSON responses are automatically parsed.
27+
this.users = response.data
28+
})
29+
}
30+
}
31+
</script>
422 KB
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference types="cypress" />
2+
import { mount } from 'cypress-vue-unit-test'
3+
import Users from './Users.vue'
4+
import * as AxiosApi from './AxiosApi'
5+
6+
describe('Mocking imports from Axios Wrapper', () => {
7+
it('renders mocked data', () => {
8+
// stub export "get" that Users.vue imports and uses
9+
cy.stub(AxiosApi, 'get')
10+
.resolves({
11+
data: [
12+
{
13+
id: 101,
14+
name: 'Test User',
15+
},
16+
],
17+
})
18+
.as('get')
19+
20+
mount(Users)
21+
// the mock response is used 😀
22+
cy.get('li').should('have.length', 1)
23+
cy.get('@get').should('have.been.calledOnce')
24+
})
25+
})

0 commit comments

Comments
 (0)