From dd559d9862b30ffb87fa7ce492deb98d333356ff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Barth=C3=A9l=C3=A9my=20Ledoux?=
Date: Thu, 7 Jan 2021 18:30:52 -0600
Subject: [PATCH] feat(component-testing): changes to the driver and reporter
preparing for runner-ct (#14434)
* chore: update driver with component testing
* feat: bring ct changes in reporter
* test: update script utils test
* fix: type issue
* test: add test for new ct behavior in driver
runScript can now use promises instead of files.
Thi test this new behavior
* tests(ct): tests for the reporter runable store
* fix: remove changes on event handling in driver
* build: augment zip size to avoid zip errors
* test: add renderin tests for reporter multispec
* test: better matcher for runnableHistory
Co-authored-by: Jessica Sachs
* test: make the runscripts eval tests clearer
* refactor(reporter): main interface instead of type
* fix(reporter): runInAction when specRunId changes
* refactor(driver): remove restartRunner function
Co-authored-by: Jessica Sachs
---
.../integration/cypress/script_utils_spec.js | 16 +++-
packages/driver/src/cy/commands/navigation.js | 2 +-
packages/driver/src/cypress/script_utils.js | 17 +++-
.../cypress/integration/runnables_spec.ts | 15 ++++
.../integration/unit/runnables_store_spec.ts | 23 +++++
packages/reporter/src/header/header.tsx | 4 +-
packages/reporter/src/main.tsx | 87 +++++++++++++++----
.../reporter/src/runnables/runnables-store.ts | 25 ++++++
.../reporter/src/runnables/runnables.scss | 14 +++
packages/reporter/src/runnables/runnables.tsx | 14 ++-
scripts/binary/zip.js | 2 +-
11 files changed, 188 insertions(+), 31 deletions(-)
diff --git a/packages/driver/cypress/integration/cypress/script_utils_spec.js b/packages/driver/cypress/integration/cypress/script_utils_spec.js
index ac58e2e051..f71ade84a5 100644
--- a/packages/driver/cypress/integration/cypress/script_utils_spec.js
+++ b/packages/driver/cypress/integration/cypress/script_utils_spec.js
@@ -1,3 +1,4 @@
+const Promise = require('bluebird')
const $scriptUtils = require('@packages/driver/src/cypress/script_utils')
const $networkUtils = require('@packages/driver/src/cypress/network_utils')
const $sourceMapUtils = require('@packages/driver/src/cypress/source_map_utils')
@@ -42,10 +43,19 @@ describe('src/cypress/script_utils', () => {
it('evals each script', () => {
return $scriptUtils.runScripts(scriptWindow, scripts)
.then(() => {
- expect($sourceMapUtils.extractSourceMap).to.be.calledTwice
- expect($sourceMapUtils.extractSourceMap).to.be.calledWith(scripts[0], 'the script contents')
- expect($sourceMapUtils.extractSourceMap).to.be.calledWith(scripts[1], 'the script contents')
+ expect(scriptWindow.eval).to.be.calledTwice
+ expect(scriptWindow.eval).to.be.calledWith('the script contents\n//# sourceURL=http://localhost:3500cypress/integration/script1.js')
+ expect(scriptWindow.eval).to.be.calledWith('the script contents\n//# sourceURL=http://localhost:3500cypress/integration/script2.js')
})
})
})
+
+ context('#runPromises', () => {
+ it('handles promises and doesnt try to fetch + eval manually', async () => {
+ const scriptsAsPromises = [Promise.resolve(), Promise.resolve()]
+ const result = await $scriptUtils.runScripts({}, scriptsAsPromises)
+
+ expect(result).to.have.length(scriptsAsPromises.length)
+ })
+ })
})
diff --git a/packages/driver/src/cy/commands/navigation.js b/packages/driver/src/cy/commands/navigation.js
index 15f67911e3..cd44b4d66f 100644
--- a/packages/driver/src/cy/commands/navigation.js
+++ b/packages/driver/src/cy/commands/navigation.js
@@ -352,7 +352,7 @@ module.exports = (Commands, Cypress, cy, state, config) => {
Cypress.on('test:before:run:async', () => {
// reset any state on the backend
- Cypress.backend('reset:server:state')
+ return Cypress.backend('reset:server:state')
})
Cypress.on('test:before:run', reset)
diff --git a/packages/driver/src/cypress/script_utils.js b/packages/driver/src/cypress/script_utils.js
index b78f18b623..b488b6f1fb 100644
--- a/packages/driver/src/cypress/script_utils.js
+++ b/packages/driver/src/cypress/script_utils.js
@@ -1,5 +1,5 @@
const _ = require('lodash')
-const Promise = require('bluebird')
+const Bluebird = require('bluebird')
const $networkUtils = require('./network_utils')
const $sourceMapUtils = require('./source_map_utils')
@@ -28,13 +28,24 @@ const evalScripts = (specWindow, scripts = []) => {
return null
}
-const runScripts = (specWindow, scripts) => {
- return Promise
+const runScriptsFromUrls = (specWindow, scripts) => {
+ return Bluebird
.map(scripts, (script) => fetchScript(specWindow, script))
.map(extractSourceMap)
.then((scripts) => evalScripts(specWindow, scripts))
}
+// Supports either scripts as objects or as async import functions
+const runScripts = (specWindow, scripts) => {
+ // if scripts contains at least one promise
+ if (scripts.length && typeof scripts[0].then === 'function') {
+ // merge the awaiting of the promises
+ return Bluebird.all(scripts)
+ }
+
+ return runScriptsFromUrls(specWindow, scripts)
+}
+
module.exports = {
runScripts,
}
diff --git a/packages/reporter/cypress/integration/runnables_spec.ts b/packages/reporter/cypress/integration/runnables_spec.ts
index aeab033ff1..5bc2db0a4c 100644
--- a/packages/reporter/cypress/integration/runnables_spec.ts
+++ b/packages/reporter/cypress/integration/runnables_spec.ts
@@ -58,6 +58,21 @@ describe('runnables', () => {
cy.percySnapshot()
})
+ it('displays multi-spec reporters', () => {
+ start({ runMode: 'multi', allSpecs: [
+ {
+ relative: 'fizz',
+ },
+ {
+ relative: 'buzz',
+ },
+ ] })
+
+ // ensure the page is loaded before taking snapshot
+ cy.contains('buzz').should('be.visible')
+ cy.percySnapshot()
+ })
+
it('displays the "No test" error when there are no tests', () => {
runnables.suites = []
start()
diff --git a/packages/reporter/cypress/integration/unit/runnables_store_spec.ts b/packages/reporter/cypress/integration/unit/runnables_store_spec.ts
index 502a582466..778fa1d092 100644
--- a/packages/reporter/cypress/integration/unit/runnables_store_spec.ts
+++ b/packages/reporter/cypress/integration/unit/runnables_store_spec.ts
@@ -234,6 +234,22 @@ describe('runnables store', () => {
})
})
+ context('#setRunningSpec', () => {
+ it('sets the current runnable as the path passed', () => {
+ instance.setRunnables({ tests: [createTest('1')] })
+ instance.setRunningSpec('specPath')
+ expect(instance.runningSpec).to.equal('specPath')
+ })
+
+ it('add the previous path to the spec history', () => {
+ instance.setRunnables({ tests: [createTest('1')] })
+ instance.setRunningSpec('previousSpecPath')
+ instance.setRunningSpec('nextSpecPath')
+ expect(instance.runningSpec).to.equal('nextSpecPath')
+ expect(instance.runnablesHistory['previousSpecPath']).not.to.be.undefined
+ })
+ })
+
context('#reset', () => {
it('resets flags to default values', () => {
instance.setRunnables({ tests: [createTest('1')] })
@@ -259,5 +275,12 @@ describe('runnables store', () => {
instance.reset()
expect(instance.testById('1')).to.be.undefined
})
+
+ it('resets runnablesHistory', () => {
+ instance.setRunnables({ tests: [createTest('1')] })
+ instance.setRunningSpec('previous')
+ instance.reset()
+ expect(instance.runnablesHistory).to.be.empty
+ })
})
})
diff --git a/packages/reporter/src/header/header.tsx b/packages/reporter/src/header/header.tsx
index 605799126e..f4b0dde331 100644
--- a/packages/reporter/src/header/header.tsx
+++ b/packages/reporter/src/header/header.tsx
@@ -10,13 +10,13 @@ import Controls from './controls'
import Stats from './stats'
import { StatsStore } from './stats-store'
-interface Props {
+export interface ReporterHeaderProps {
appState: AppState
events?: Events
statsStore: StatsStore
}
-const Header = observer(({ appState, events = defaultEvents, statsStore }: Props) => (
+const Header = observer(({ appState, events = defaultEvents, statsStore }: ReporterHeaderProps) => (
} wrapperClassName='focus-tests' className='cy-tooltip'>