Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lmiller1990 committed Sep 15, 2022
1 parent b9f4e33 commit 723e306
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 12 deletions.
3 changes: 1 addition & 2 deletions cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3067,8 +3067,7 @@ declare namespace Cypress {
type DevServerFn<ComponentDevServerOpts = any> = (cypressDevServerConfig: DevServerConfig, devServerConfig: ComponentDevServerOpts) => ResolvedDevServerConfig | Promise<ResolvedDevServerConfig>

type ConfigHandler<T> = T
| ((baseConfig: T) => T)
| ((baseConfig: T) => Promise<T>)
| (() => T | Promise<T>)

type DevServerConfigOptions = {
bundler: 'webpack'
Expand Down
4 changes: 1 addition & 3 deletions npm/vite-dev-server/src/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ const debug = debugFn('cypress:vite-dev-server:devServer')

const ALL_FRAMEWORKS = ['react', 'vue'] as const

type ConfigHandler = UserConfig
| ((baseConfig: InlineConfig) => Promise<UserConfig>)
| ((baseConfig: InlineConfig) => UserConfig)
type ConfigHandler = UserConfig | (() => UserConfig | Promise<UserConfig>)

export type ViteDevServerConfig = {
specs: Cypress.Spec[]
Expand Down
2 changes: 1 addition & 1 deletion npm/vite-dev-server/src/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const createViteDevServerConfig = async (config: ViteDevServerConfig, vit
let resolvedOverrides: UserConfig = {}

if (typeof viteOverrides === 'function') {
resolvedOverrides = await viteOverrides(viteBaseConfig)
resolvedOverrides = await viteOverrides()
} else if (typeof viteOverrides === 'object') {
resolvedOverrides = viteOverrides
}
Expand Down
29 changes: 28 additions & 1 deletion npm/vite-dev-server/test/resolveConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { expect } from 'chai'
import Chai, { expect } from 'chai'
import { EventEmitter } from 'events'
import * as vite from 'vite'
import { scaffoldSystemTestProject } from './test-helpers/scaffoldProject'
import { createViteDevServerConfig } from '../src/resolveConfig'
import sinon from 'sinon'
import SinonChai from 'sinon-chai'
import type { ViteDevServerConfig } from '../src/devServer'

Chai.use(SinonChai)

const getViteDevServerConfig = (projectRoot: string) => {
return {
specs: [],
Expand All @@ -21,6 +25,29 @@ const getViteDevServerConfig = (projectRoot: string) => {
describe('resolveConfig', function () {
this.timeout(1000 * 60)

it('calls viteConfig if it is a function, passing in the base config', async () => {
const viteConfigFn = sinon.spy(async () => {
return {
server: {
fs: {
allow: ['some/other/file'],
},
},
}
})

const projectRoot = await scaffoldSystemTestProject('vite-inspect')
const viteDevServerConfig = {
...getViteDevServerConfig(projectRoot),
viteConfig: viteConfigFn,
}

const viteConfig = await createViteDevServerConfig(viteDevServerConfig, vite)

expect(viteConfigFn).to.be.called
expect(viteConfig.server.fs.allow).to.include('some/other/file')
})

context('inspect plugin', () => {
it('should not include inspect plugin by default', async () => {
const projectRoot = await scaffoldSystemTestProject('vite-inspect')
Expand Down
6 changes: 3 additions & 3 deletions npm/webpack-dev-server/src/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ type FrameworkConfig = {
}
}

export type ConfigHandler = Partial<Configuration>
| ((baseConfig: Configuration) => Promise<Configuration>)
| ((baseConfig: Configuration) => Configuration)
export type ConfigHandler =
Partial<Configuration>
| (() => Partial<Configuration> | Promise<Partial<Configuration>>)

export type WebpackDevServerConfig = {
specs: Cypress.Spec[]
Expand Down
2 changes: 1 addition & 1 deletion npm/webpack-dev-server/src/makeWebpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export async function makeWebpackConfig (
}

userWebpackConfig = typeof userWebpackConfig === 'function'
? await userWebpackConfig(frameworkWebpackConfig)
? await userWebpackConfig()
: userWebpackConfig

const userAndFrameworkWebpackConfig = modifyWebpackConfigForCypress(
Expand Down
48 changes: 47 additions & 1 deletion npm/webpack-dev-server/test/makeWebpackConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { expect } from 'chai'
import Chai, { expect } from 'chai'
import EventEmitter from 'events'
import snapshot from 'snap-shot-it'
import { IgnorePlugin } from 'webpack'
import { WebpackDevServerConfig } from '../src/devServer'
import { sourceDefaultWebpackDependencies } from '../src/helpers/sourceRelativeWebpackModules'
import { CYPRESS_WEBPACK_ENTRYPOINT, makeWebpackConfig } from '../src/makeWebpackConfig'
import { createModuleMatrixResult } from './test-helpers/createModuleMatrixResult'
import sinon from 'sinon'
import SinonChai from 'sinon-chai'

Chai.use(SinonChai)

describe('makeWebpackConfig', () => {
it('ignores userland webpack `output.publicPath` and `devServer.overlay` with webpack-dev-server v3', async () => {
Expand Down Expand Up @@ -136,4 +141,45 @@ describe('makeWebpackConfig', () => {
'cypress-entry': CYPRESS_WEBPACK_ENTRYPOINT,
})
})

it('calls webpackConfig if it is a function, passing in the base config', async () => {
const testPlugin = new IgnorePlugin({
contextRegExp: /aaa/,
resourceRegExp: /bbb/,
})

const modifyConfig = sinon.spy(async () => {
return {
plugins: [testPlugin],
}
})

const devServerConfig: WebpackDevServerConfig = {
specs: [],
cypressConfig: {
isTextTerminal: false,
projectRoot: '.',
supportFile: '/support.js',
devServerPublicPathRoute: '/test-public-path', // This will be overridden by makeWebpackConfig.ts
} as Cypress.PluginConfigOptions,
webpackConfig: modifyConfig,
devServerEvents: new EventEmitter(),
}

const actual = await makeWebpackConfig({
devServerConfig,
sourceWebpackModulesResult: createModuleMatrixResult({
webpack: 4,
webpackDevServer: 4,
}),
})

expect(actual.plugins.length).to.eq(3)
expect(modifyConfig).to.have.been.called
// merged plugins get added at the top of the chain by default
// should be merged, not overriding existing plugins
expect(actual.plugins[0].constructor.name).to.eq('IgnorePlugin')
expect(actual.plugins[1].constructor.name).to.eq('HtmlWebpackPlugin')
expect(actual.plugins[2].constructor.name).to.eq('CypressCTWebpackPlugin')
})
})

0 comments on commit 723e306

Please sign in to comment.