Skip to content

Commit

Permalink
add unit test and fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
ZachJW34 committed Jul 13, 2022
1 parent 7f1dc8b commit a049ed7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
6 changes: 4 additions & 2 deletions npm/webpack-dev-server/src/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ export function devServer (devServerConfig: WebpackDevServerConfig): Promise<Cyp
})
}

export type PresetHandlerResult = { frameworkConfig?: Configuration, sourceWebpackModulesResult: SourceRelativeWebpackResult }
export type PresetHandlerResult = { frameworkConfig: Configuration, sourceWebpackModulesResult: SourceRelativeWebpackResult }

async function getPreset (devServerConfig: WebpackDevServerConfig): Promise<PresetHandlerResult> {
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>

async function getPreset (devServerConfig: WebpackDevServerConfig): Promise<Optional<PresetHandlerResult, 'frameworkConfig'>> {
switch (devServerConfig.framework) {
case 'create-react-app':
return createReactAppHandler(devServerConfig)
Expand Down
3 changes: 2 additions & 1 deletion npm/webpack-dev-server/src/helpers/nextHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ function watchEntryPoint (webpackConfig: Configuration) {

const globalCssRe = [/(?<!\.module)\.css$/, /(?<!\.module)\.(scss|sass)$/]
const globalCssModulesRe = [/\.module\.css$/, /\.module\.(scss|sass)$/]
const allCssTests = [...globalCssRe, ...globalCssModulesRe]

export const allCssTests = [...globalCssRe, ...globalCssModulesRe]

// Next does not allow global styles to be loaded outside of the main <App /> component.
// We want users to be able to import the global styles into their component support file so we
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const expectEslintModifications = (webpackConfig: Configuration) => {
}

const expectModuleSourceInPlaceModifications = (webpackConfig: Configuration, projectRoot: string) => {
const moduleSourcePlugin: any = webpackConfig.resolve.plugins.find((plugin) => plugin.constructor.name === 'ModuleScopePlugin')
const moduleSourcePlugin: any = webpackConfig.resolve?.plugins?.find((plugin) => plugin.constructor.name === 'ModuleScopePlugin')

if (!moduleSourcePlugin) {
throw new Error('Expected to find ModuleScopePlugin in webpack config')
Expand All @@ -27,7 +27,7 @@ const expectModuleSourceInPlaceModifications = (webpackConfig: Configuration, pr
}

const expectBabelRuleModifications = (webpackConfig: Configuration, projectRoot: string) => {
const babelRule: any = (webpackConfig.module.rules as any).find((rule) => rule.oneOf)?.oneOf.find((oneOf) => oneOf.loader?.includes('babel-loader'))
const babelRule: any = (webpackConfig.module?.rules as any)?.find((rule) => rule.oneOf)?.oneOf.find((oneOf) => oneOf.loader?.includes('babel-loader'))

if (!babelRule) {
throw new Error('Expected to find BabelRule in webpack config')
Expand All @@ -37,7 +37,7 @@ const expectBabelRuleModifications = (webpackConfig: Configuration, projectRoot:
}

const expectReactScriptsFiveModifications = (webpackConfig: Configuration) => {
const definePlugin: any = webpackConfig.plugins.find((plugin) => plugin.constructor.name === 'DefinePlugin')
const definePlugin: any = webpackConfig.plugins?.find((plugin) => plugin.constructor.name === 'DefinePlugin')

if (!definePlugin) {
throw new Error('Expected to find DefinePlugin in webpack config')
Expand Down
29 changes: 24 additions & 5 deletions npm/webpack-dev-server/test/handlers/nextHandler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
import { scaffoldMigrationProject } from '../test-helpers/scaffoldProject'
import { expect } from 'chai'
import { nextHandler } from '../../src/helpers/nextHandler'
import type { Configuration } from 'webpack'
import { nextHandler, allCssTests } from '../../src/helpers/nextHandler'
import type { Configuration, RuleSetRule } from 'webpack'
import * as path from 'path'
import { WebpackDevServerConfig } from '../../src/devServer'
import '../support'

const expectWatchOverrides = (webpackConfig: Configuration) => {
expect(webpackConfig.watchOptions.ignored).to.contain('**/node_modules/!(@cypress/webpack-dev-server/dist/browser.js)**')
expect(webpackConfig.watchOptions?.ignored).to.contain('**/node_modules/!(@cypress/webpack-dev-server/dist/browser.js)**')
}

const expectPagesDir = (webpackConfig: Configuration, projectRoot: string) => {
const ReactLoadablePlugin: any = webpackConfig.plugins.find((plugin) => plugin.constructor.name === 'ReactLoadablePlugin')
const ReactLoadablePlugin: any = webpackConfig.plugins?.find((plugin) => plugin.constructor.name === 'ReactLoadablePlugin')

expect(ReactLoadablePlugin.pagesDir).eq(path.join(projectRoot, 'pages'))
}

const expectWebpackSpan = (webpackConfig: Configuration) => {
const ProfilingPlugin: any = webpackConfig.plugins.find((plugin) => plugin.constructor.name === 'ProfilingPlugin')
const ProfilingPlugin: any = webpackConfig.plugins?.find((plugin) => plugin.constructor.name === 'ProfilingPlugin')

expect(ProfilingPlugin.runWebpackSpan).to.exist
}

const expectGlobalStyleOverrides = (webpackConfig: Configuration) => {
const cssRules: RuleSetRule[] = []

for (const rule of webpackConfig.module?.rules as RuleSetRule[]) {
if (rule.oneOf) {
for (const oneOf of rule.oneOf) {
if (oneOf.test && allCssTests.some((re) => re.source === (oneOf as any).test?.source)) {
cssRules.push(oneOf)
}
}
}
}

expect(cssRules).to.have.length.greaterThan(0)
cssRules.forEach((rule) => expect(rule.issuer).to.be.undefined)
}

describe('nextHandler', function () {
// can take a while since we install node_modules
this.timeout(1000 * 60)
Expand All @@ -39,6 +56,7 @@ describe('nextHandler', function () {
expectWatchOverrides(webpackConfig)
expectPagesDir(webpackConfig, projectRoot)
expectWebpackSpan(webpackConfig)
expectGlobalStyleOverrides(webpackConfig)
})

it('sources from a next-11 project', async () => {
Expand All @@ -54,6 +72,7 @@ describe('nextHandler', function () {
expectWatchOverrides(webpackConfig)
expectPagesDir(webpackConfig, projectRoot)
expectWebpackSpan(webpackConfig)
expectGlobalStyleOverrides(webpackConfig)
})

it('throws if nodeVersion is set to bundled', async () => {
Expand Down
2 changes: 1 addition & 1 deletion npm/webpack-dev-server/test/handlers/nuxtHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('nuxtHandler', function () {
} as WebpackDevServerConfig)

// Verify it's a Vue-specific webpack config by seeing if VueLoader is present.
expect(webpackConfig.plugins.find((plug) => plug.constructor.name === 'VueLoader'))
expect(webpackConfig.plugins?.find((plug) => plug.constructor.name === 'VueLoader'))
expect(webpackConfig.performance).to.be.undefined
})
})
4 changes: 2 additions & 2 deletions npm/webpack-dev-server/test/handlers/vueCliHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('vueCliHandler', function () {
} as WebpackDevServerConfig)

// Verify it's a Vue-specific webpack config by seeing if VueLoader is present.
expect(webpackConfig.plugins.find((plug) => plug.constructor.name === 'VueLoader'))
expect(webpackConfig.plugins?.find((plug) => plug.constructor.name === 'VueLoader'))
})

it('sources from a @vue/[email protected] project with Vue 2', async () => {
Expand All @@ -31,6 +31,6 @@ describe('vueCliHandler', function () {
} as WebpackDevServerConfig)

// Verify it's a Vue-specific webpack config by seeing if VueLoader is present.
expect(webpackConfig.plugins.find((plug) => plug.constructor.name === 'VueLoader'))
expect(webpackConfig.plugins?.find((plug) => plug.constructor.name === 'VueLoader'))
})
})

1 comment on commit a049ed7

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on a049ed7 Jul 13, 2022

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.3.1/linux-x64/zachw/issue-22525-a049ed7f3a2f9a709e466f60bf2da775312af521/cypress.tgz

Please sign in to comment.