Skip to content

Commit c289b79

Browse files
authored
chore: enable api mocking for e2e tests (#74)
1 parent 5c76fcd commit c289b79

File tree

5 files changed

+22
-54
lines changed

5 files changed

+22
-54
lines changed

cypress/integration/App.spec.ts

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const terminalLog = (violations: Result[]): void => {
88
} detected`,
99
);
1010

11-
// pluck specific keys to keep the table readable
1211
const violationData = violations.map(({ id, impact, description, nodes }) => ({
1312
id,
1413
impact,
@@ -76,7 +75,8 @@ describe('Todo App', () => {
7675
cy.findByText(/save/i).click();
7776
});
7877

79-
cy.findByText(testValue).should('be.visible').deleteTask(testValue);
78+
cy.findByText(testValue).should('be.visible');
79+
cy.deleteTask(testValue);
8080
});
8181

8282
it('creates todo item requires validation', () => {
@@ -112,32 +112,7 @@ describe('Todo App', () => {
112112
it('deletes todo item', () => {
113113
const testValue = `cy ${Date.now()}`;
114114
cy.createTask(testValue);
115-
cy.findByText(/todo app/i).should('be.visible');
116-
117-
cy.findAllByRole('listitem')
118-
.first()
119-
.trigger('touchstart', {
120-
touches: [{ clientY: 40, clientX: 0 }],
121-
waitForAnimations: false,
122-
})
123-
.trigger('touchmove', {
124-
touches: [{ clientY: 40, clientX: -50 }],
125-
waitForAnimations: false,
126-
})
127-
.trigger('touchmove', {
128-
touches: [{ clientY: 40, clientX: -100 }],
129-
waitForAnimations: false,
130-
})
131-
.trigger('touchmove', {
132-
touches: [{ clientY: 40, clientX: -150 }],
133-
waitForAnimations: false,
134-
})
135-
.trigger('touchend', {
136-
touches: [{ clientY: 40, clientX: -160 }],
137-
waitForAnimations: false,
138-
});
139-
140-
cy.findByText(testValue).should('not.be.visible');
115+
cy.deleteTask(testValue).should('not.exist');
141116
});
142117
});
143118

cypress/support/commands.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ Cypress.Commands.add('deleteTask', (task: string) => {
5858
.trigger('touchend', {
5959
touches: [{ clientY: 40, clientX: -160 }],
6060
waitForAnimations: false,
61-
});
62-
63-
cy.findByText(task).should('not.be.visible');
61+
})
62+
.should('not.exist');
6463
});

cypress/support/index.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,3 @@
1515

1616
import './commands';
1717
import 'cypress-axe';
18-
19-
// TODO: msw & cypress combo triggers test runner endless loop
20-
// https://github.com/mswjs/msw/issues/744
21-
// https://github.com/cypress-io/cypress/issues/16742
22-
// import { worker } from '../../src/mocks/msw/browser';
23-
24-
// before(async () => {
25-
// return worker.start({
26-
// serviceWorker: {
27-
// url: `/react-todo-app-ts/mockServiceWorker.js`,
28-
// },
29-
// });
30-
// });

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@
9191
"prettier": "prettier --check src",
9292
"graphql:codegen": "graphql-codegen --config .graphqlrc.yml",
9393
"cy:open": "npx cypress open",
94-
"cypress": "start-server-and-test start http://localhost:3000 cy:open",
94+
"cypress": "REACT_APP_API_MOCKING=enabled start-server-and-test start http://localhost:3000 cy:open",
9595
"cy:run": "npx cypress run",
96-
"cypress:ci": "start-server-and-test start http://localhost:3000 cy:run"
96+
"cypress:ci": "REACT_APP_API_MOCKING=enabled start-server-and-test start http://localhost:3000 cy:run"
9797
},
9898
"eslintConfig": {
9999
"extends": [

src/index.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@ import { queryClient } from './queryClient';
1111
import { reportWebVitals } from './reportWebVitals';
1212

1313
const main = async (): Promise<void> => {
14-
// if (process.env.NODE_ENV === 'development') {
15-
// const { worker } = await import('./mocks/msw/browser');
16-
// await worker.start({
17-
// serviceWorker: {
18-
// url: `${process.env.PUBLIC_URL}/mockServiceWorker.js`,
19-
// },
20-
// });
21-
// }
14+
if (process.env.REACT_APP_API_MOCKING === 'enabled') {
15+
const { worker } = await import('./mocks/msw/browser');
16+
await worker.start({
17+
serviceWorker: {
18+
url: `${process.env.PUBLIC_URL}/mockServiceWorker.js`,
19+
},
20+
onUnhandledRequest: ({ url }, print) => {
21+
if (!url.pathname.startsWith('/graphql')) {
22+
return;
23+
}
24+
25+
print.error();
26+
},
27+
});
28+
}
2229

2330
ReactDOM.render(
2431
<React.StrictMode>

0 commit comments

Comments
 (0)