Skip to content

Commit

Permalink
Use msw with Cypress (#871)
Browse files Browse the repository at this point in the history
  • Loading branch information
majakomel authored Sep 15, 2023
1 parent bbea950 commit 4f94cc2
Show file tree
Hide file tree
Showing 11 changed files with 2,328 additions and 1,448 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Cypress run
uses: cypress-io/github-action@v5
uses: cypress-io/github-action@v6
with:
build: npm run build
start: npm start
Expand Down
4 changes: 3 additions & 1 deletion cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { defineConfig } = require('cypress')

module.exports = defineConfig({
video: false,
chromeWebSecurity: false,
e2e: {
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
Expand All @@ -12,4 +11,7 @@ module.exports = defineConfig({
baseUrl: 'http://localhost:3100',
testIsolation: false,
},
env: {
apiUrl: 'https://ams-pg-test.ooni.org',
}
})
33 changes: 9 additions & 24 deletions cypress/e2e/measurement.e2e.cy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { recurse } = require('cypress-recurse')
import { failedAccountMetadata } from '/cypress/mocks/handlers'

describe('Measurement Page Tests', () => {

Expand Down Expand Up @@ -315,40 +315,25 @@ describe('Measurement Page Tests', () => {
})

describe('User Feedback', () => {
let userEmail

before(() => {
cy.clearLocalStorage()
cy.intercept('GET', 'https://ams-pg-test.ooni.org/api/v1/user_login*').as('userLogin')
// get and check the test email only once before the tests
cy.task('getUserEmail').then((email) => {
expect(email).to.be.a('string')
userEmail = email
})
})

it('can login and submit feedback', () => {
it('can login', () => {
cy.interceptRequest(failedAccountMetadata)

const measurementUrl = '/m/20230307142542.625294_US_webconnectivity_9215f30cf2412f49'
cy.visit(measurementUrl)
cy.findByText('VERIFY').click()

cy.findByRole('textbox').click().type(userEmail)
cy.findByRole('textbox').click().type('[email protected]')
cy.findByText('Login').click()
cy.findByText('Login link sent')
})


recurse(
() => cy.task('getLastEmail'), // Cypress commands to retry
Cypress._.isObject, // keep retrying until the task returns an object
{
timeout: 60000, // retry up to 1 minute
delay: 5000, // wait 5 seconds between attempts
},
).then(({ loginLink }) => {
cy.visit(loginLink)
})

cy.url().should('contain', '/login')
cy.wait('@userLogin')
it('can submit feedback', () => {
const measurementUrl = '/m/20230307142542.625294_US_webconnectivity_9215f30cf2412f49'

cy.visit(measurementUrl)
cy.findByText('VERIFY').click()
Expand Down
48 changes: 48 additions & 0 deletions cypress/mocks/handlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { rest } from 'msw'

const apiBase = Cypress.env('apiUrl')

export const handlers = [
rest.get(`${apiBase}/api/_/account_metadata`, (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
logged_in: true,
role: 'admin',
}),
)
}),

rest.post(`${apiBase}/api/v1/user_register`, (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
role: 'admin',
}),
)
}),

rest.get(`${apiBase}/api/v1/user_login`, (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
bearer: 'abc123',
redirect_to: 'https://explorer.org/'
}),
)
}),

rest.post(`${apiBase}/api/_/measurement_feedback`, (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
}),
)
}),
]

export const failedAccountMetadata = rest.get('https://ams-pg-test.ooni.org/api/_/account_metadata', (req, res, ctx) => {
return res(
ctx.status(401),
)
})
69 changes: 0 additions & 69 deletions cypress/plugins/email-account.js

This file was deleted.

12 changes: 0 additions & 12 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// <reference types="cypress" />
const makeEmailAccount = require('./email-account')
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
Expand All @@ -19,15 +18,4 @@ const makeEmailAccount = require('./email-account')
module.exports = async (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
const emailAccount = await makeEmailAccount()

on('task', {
getUserEmail() {
return emailAccount.email
},

getLastEmail() {
return emailAccount.getLastEmail()
},
})
}
1 change: 1 addition & 0 deletions cypress/support/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

// Import commands.js using ES2015 syntax:
import './commands'
import './msw'

// Alternatively you can use CommonJS syntax:
// require('./commands')
Expand Down
18 changes: 18 additions & 0 deletions cypress/support/msw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

import { setupWorker, rest} from 'msw'
import { handlers } from '/cypress/mocks/handlers'

let worker

before(() => {
worker = setupWorker(...handlers)
cy.wrap(worker.start({ onUnhandledRequest: 'bypass' }), { log: false })
})
Cypress.on('test:before:run', () => {
if (!worker) return
worker.resetHandlers()
})

Cypress.Commands.add('interceptRequest', (...params) => {
worker.use(...params)
})
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,27 @@
"victory": "^32.0.2"
},
"resolutions": {
"hoist-non-react-statics": "3.0.1",
"rebass": "4.0.7",
"styled-components": "5.1.1",
"styled-system": "5.1.5",
"js-yaml": "^3.13.1",
"https-proxy-agent": "^2.2.3",
"minimist": "^1.2.6",
"node-fetch": "^2.6.7",
"lodash": "^4.17.21"
},
"devDependencies": {
"@svgr/webpack": "^6.5.0",
"@testing-library/cypress": "^9.0.0",
"@svgr/webpack": "^8.1.0",
"@testing-library/cypress": "^10.0.0",
"@welldone-software/why-did-you-render": "^7.0.1",
"cypress": "^12.6.0",
"cypress-recurse": "^1.27.0",
"eslint": "^8.22.0",
"eslint-config-next": "^12.2.5",
"eslint-plugin-cypress": "^2.12.1",
"glob": "^8.0.3",
"cypress": "^13.2.0",
"cypress-recurse": "^1.35.2",
"eslint": "^8.49.0",
"eslint-config-next": "^13.4.19",
"eslint-plugin-cypress": "^2.14.0",
"glob": "^10.3.4",
"imap-simple": "^5.1.0",
"jsdom": "^21.1.0",
"mailparser": "^3.6.3",
"jsdom": "^22.1.0",
"mailparser": "^3.6.5",
"msw": "^1.3.0",
"mustache": "^4.2.0",
"nodemailer": "^6.9.1",
"nodemailer": "^6.9.5",
"start-server-and-test": "^2.0.0"
},
"scripts": {
Expand All @@ -91,14 +87,18 @@
"lint": "next lint",
"start:testServer": "NODE_ENV=test yarn start",
"build:test": "NODE_ENV=test yarn build",
"test:e2e": "yarn build:test && start-server-and-test start:testServer http://localhost:3100 cypress:run",
"cy:run": "NODE_ENV=test yarn run dev & cypress open",
"cypress:run": "cypress run",
"test:e2e": "yarn build:test && start-server-and-test start:testServer http://localhost:3100 cypress:run",
"test": "yarn run test:e2e",
"script:build-translations": "node ./scripts/build-translations.js",
"script:extract-i18n-keys": "yarn build && node ./scripts/extract-react-intl-keys.js",
"git:getReleasesAndTags": "git tag --points-at HEAD",
"git:getCommitSHA": "git rev-parse HEAD",
"git:getCommitSHA:short": "git rev-parse --short HEAD",
"git:getCommitRef": "git symbolic-ref HEAD"
},
"msw": {
"workerDirectory": "public"
}
}
Loading

1 comment on commit 4f94cc2

@vercel
Copy link

@vercel vercel bot commented on 4f94cc2 Sep 15, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

explorer – ./

explorer-one.vercel.app
explorer-ooni1.vercel.app
explorer-git-master-ooni1.vercel.app

Please sign in to comment.