Skip to content

Commit

Permalink
fix(vite-dev-server): chain update all specs when changing child (#17693
Browse files Browse the repository at this point in the history
)

* fix: chain update all specs when changing child

closes #17691

* fix: align types with cypress standards

Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com>
  • Loading branch information
Barthélémy Ledoux and lmiller1990 authored Aug 12, 2021
1 parent b9b276f commit 66e8896
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Hello from './Hello.vue'

describe('Hello', () => {
it('shows error for short text', () => {
cy.viewport(300, 200)
cy.viewport(500, 800)
mount(Hello)
// use the component like a real user
cy.findByRole('textbox').type('abc')
Expand Down
4 changes: 4 additions & 0 deletions npm/vite-dev-server/cypress/components/vue/Hello/Hello.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
<input v-model="username" />
<div v-if="error" class="error">{{ error }}</div>
<img src="./logo.png" />
<Card/>
</div>
</template>

<script>
import Card from "../Card/Card.vue"
export default {
name: "Hello",
components: { Card },
data() {
return {
username: "",
Expand Down
26 changes: 19 additions & 7 deletions npm/vite-dev-server/src/makeCypressPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ interface Spec{

export const makeCypressPlugin = (
projectRoot: string,
supportFilePath: string,
devServerEvents: EventEmitter,
supportFilePath: string | false,
devServerEvents: NodeJS.EventEmitter,
specs: Spec[],
): Plugin => {
let base = '/'
Expand Down Expand Up @@ -93,8 +93,10 @@ export const makeCypressPlugin = (
let moduleImporters = server.moduleGraph.fileToModulesMap.get(file)
let iterationNumber = 0

const exploredFiles = new Set<string>()

// until we reached a point where the current module is imported by no other
while (moduleImporters && moduleImporters.size) {
while (moduleImporters?.size) {
if (iterationNumber > HMR_DEPENDENCY_LOOKUP_MAX_ITERATION) {
debug(`max hmr iteration reached: ${HMR_DEPENDENCY_LOOKUP_MAX_ITERATION}; Rerun will not happen on this file change.`)

Expand All @@ -108,19 +110,27 @@ export const makeCypressPlugin = (
debug('handleHotUpdate - support compile success')
devServerEvents.emit('dev-server:compile:success')

// if we update support we know we have to re-run it all
// no need to ckeck further
return []
}

if (mod.file && specsPathsSet.has(mod.file)) {
debug('handleHotUpdate - spec compile success', mod.file)
devServerEvents.emit('dev-server:compile:success', { specFile: mod.file })
// if we find one spec, does not mean we are done yet,
// there could be other spec files to re-run
// see https://github.com/cypress-io/cypress/issues/17691
}

return []
// to avoid circular updates, keep track of what we updated
if (mod.file) {
exploredFiles.add(mod.file)
}
}

// get all the modules that import the current one
moduleImporters = getImporters(moduleImporters)
moduleImporters = getImporters(moduleImporters, exploredFiles)
iterationNumber += 1
}

Expand All @@ -129,11 +139,13 @@ export const makeCypressPlugin = (
}
}

function getImporters (modules: Set<ModuleNode>): Set<ModuleNode> {
function getImporters (modules: Set<ModuleNode>, alreadyExploredFiles: Set<string>): Set<ModuleNode> {
const allImporters = new Set<ModuleNode>()

modules.forEach((m) => {
m.importers.forEach((imp) => allImporters.add(imp))
if (m.file && !alreadyExploredFiles.has(m.file)) {
m.importers.forEach((imp) => allImporters.add(imp))
}
})

return allImporters
Expand Down
9 changes: 1 addition & 8 deletions npm/vite-dev-server/src/startServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@ import { makeCypressPlugin } from './makeCypressPlugin'

const debug = Debug('cypress:vite-dev-server:start')

interface Options {
specs: Cypress.Cypress['spec'][]
config: Record<string, string>
devServerEvents: EventEmitter
[key: string]: unknown
}

export interface StartDevServerOptions {
/**
* the Cypress options object
*/
options: Options
options: Cypress.DevServerOptions
/**
* By default, vite will use your vite.config file to
* Start the server. If you need additional plugins or
Expand Down

0 comments on commit 66e8896

Please sign in to comment.