Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cypress test #20

Merged
merged 5 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ jobs:

- name: Test
run: pnpm run test

- name: Cypress Test
run: pnpm run cypress:run
10 changes: 10 additions & 0 deletions cypress/component/Test.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import { TestComponent } from './TestComponent'

describe('Test.cy.tsx', () => {
it('playground', () => {
cy.mount(<TestComponent title="TitleText" text="BodyText" />)
cy.contains('h1', 'TitleText').should('be.visible')
cy.contains('p', 'BodyText').should('be.visible')
})
})
11 changes: 11 additions & 0 deletions cypress/component/TestComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'

export const TestComponent = ({ title, text }) => {
return (
<div>
<h1>{title}</h1>

<p>{text}</p>
</div>
)
}
18 changes: 18 additions & 0 deletions cypress/config/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineConfig } from 'cypress'
import { devServer } from '../../dist/devServer'

export default defineConfig({
component: {
video: false,
supportFile: './support/component.ts',
specPattern: './component/*.cy.{tsx,jsx,js,ts}',
indexHtmlFile: '../support/component-index.html',
devServer(devServerConfig) {
return devServer({
...devServerConfig,
framework: 'react',
rspackConfig: require('./rspack.config.js'),
})
},
},
})
5 changes: 5 additions & 0 deletions cypress/config/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const rspack = require('@rspack/core')

module.exports = {
plugins: [new rspack.ProvidePlugin({ process: 'process' })],
}
15 changes: 15 additions & 0 deletions cypress/support/component-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Components App</title>
</head>

<body>
<div data-cy-root></div>
</body>

</html>
36 changes: 36 additions & 0 deletions cypress/support/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// ***********************************************************
// This example support/component.ts is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

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

// Alternatively you can use CommonJS syntax:
// require('./commands')

import { mount } from 'cypress/react18'

// Augment the Cypress namespace to include type definitions for
// your custom command.
// Alternatively, can be defined in cypress/support/component.d.ts
// with a <reference path="./component" /> at the top of your spec.
declare global {
namespace Cypress {
interface Chainable {
mount: typeof mount
}
}
}

Cypress.Commands.add('mount', mount)
12 changes: 0 additions & 12 deletions dist/helpers/sourceRelativeRspackModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,6 @@ function sourceDefaultRspackDependencies(config) {
return { framework, rspack, rspackDevServer };
}
exports.sourceDefaultRspackDependencies = sourceDefaultRspackDependencies;
// export function getMajorVersion<T extends number>(json: PackageJson, acceptedVersions: T[]): T {
// const major = Number(json.version.split('.')[0])
// if (!acceptedVersions.includes(major as T)) {
// throw new Error(
// `Unexpected major version of ${json.name}. ` +
// `Cypress-rspack-dev-server works with ${json.name} versions ${acceptedVersions.join(
// ', ',
// )} - saw ${json.version}`,
// )
// }
// return Number(major) as T
// }
function restoreLoadHook() {
;
module_1.default._load = originalModuleLoad;
Expand Down
2 changes: 1 addition & 1 deletion dist/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function buildSpecs(projectRoot, files = []) {
// Runs the tests inside the iframe
function loader() {
const ctx = this;
// In Webpack 5, a spec added after the dev-server is created won't
// A spec added after the dev-server is created won't
// be included in the compilation. Disabling the caching of this loader ensures
// we regenerate our specs and include any new ones in the compilation.
ctx.cacheable(false);
Expand Down
2 changes: 1 addition & 1 deletion dist/makeRspackConfig.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { CreateFinalRspackConfig } from './createRspackDevServer';
export declare const CYPRESS_RSPACK_ENTRYPOINT: string;
/**
* Creates a rspack 0 compatible rspack "configuration"
* Creates a rspack compatible rspack "configuration"
* to pass to the sourced rspack function
*/
export declare function makeRspackConfig(config: CreateFinalRspackConfig): Promise<{
Expand Down
4 changes: 2 additions & 2 deletions dist/makeRspackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const removeList = [
exports.CYPRESS_RSPACK_ENTRYPOINT = path.resolve(__dirname, 'browser.js');
/**
* Removes and/or modifies certain plugins known to conflict
* when used with cypress-rspack-dev-server.
* when used with @rspack/dev-server.
*/
function modifyRspackConfigForCypress(rspackConfig) {
if (rspackConfig === null || rspackConfig === void 0 ? void 0 : rspackConfig.plugins) {
Expand All @@ -40,7 +40,7 @@ async function getRspackConfigFromProjectRoot(projectRoot) {
return await findUp(constants_1.configFiles, { cwd: projectRoot });
}
/**
* Creates a rspack 0 compatible rspack "configuration"
* Creates a rspack compatible rspack "configuration"
* to pass to the sourced rspack function
*/
async function makeRspackConfig(config) {
Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
"build": "pnpm tsc || echo 'built, with type errors'",
"build-prod": "pnpm build",
"check-ts": "pnpm tsc --noEmit",
"dev": "DEBUG=cypress:rspack-dev-server:* tsc --watch",
"dev": "pnpm with:comment tsc --watch",
"clean": "rimraf dist",
"cypress:run": "pnpm cypress:run-cypress-in-cypress node ../../scripts/cypress run --project . --browser chrome",
"cypress:run-cypress-in-cypress": "cross-env CYPRESS_INTERNAL_E2E_TESTING_SELF_PARENT_PROJECT=1 HTTP_PROXY_TARGET_FOR_ORIGIN_REQUESTS=http://localhost:4455 CYPRESS_REMOTE_DEBUGGING_PORT=6666 TZ=America/New_York",
"cypress:open": "pnpm cypress:run-cypress-in-cypress gulp open --project .",
"cypress:run": "cypress run --component --project ./cypress --browser chrome --config-file config/cypress.config.ts",
"cypress:run-with-comment": "pnpm with:comment cypress run --component --project ./cypress --browser chrome --config-file config/cypress.config.ts",
"cypress:open": "cypress open --component --project ./cypress --browser chrome --config-file config/cypress.config.ts",
"with:comment": "cross-env DEBUG=cypress-rspack-dev-server:*",
"lint": "eslint .",
"test": "jest"
},
Expand Down Expand Up @@ -50,6 +51,8 @@
"path": "^0.12.7",
"prettier": "^3.1.0",
"proxyquire": "2.1.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sinon": "^13.0.1",
"snap-shot-it": "^7.9.6",
"ts-node": "^10.9.1",
Expand Down
36 changes: 36 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions src/helpers/sourceRelativeRspackModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,6 @@ export function sourceDefaultRspackDependencies(
return { framework, rspack, rspackDevServer }
}

// export function getMajorVersion<T extends number>(json: PackageJson, acceptedVersions: T[]): T {
// const major = Number(json.version.split('.')[0])

// if (!acceptedVersions.includes(major as T)) {
// throw new Error(
// `Unexpected major version of ${json.name}. ` +
// `Cypress-rspack-dev-server works with ${json.name} versions ${acceptedVersions.join(
// ', ',
// )} - saw ${json.version}`,
// )
// }

// return Number(major) as T
// }

export function restoreLoadHook() {
;(Module as ModuleClass)._load = originalModuleLoad
;(Module as ModuleClass)._resolveFilename = originalModuleResolveFilename
Expand Down
2 changes: 1 addition & 1 deletion src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function buildSpecs(projectRoot: string, files: Cypress.Cypress['spec'][] = []):
export default function loader(this: unknown) {
const ctx = this as CypressCTRspackContext & LoaderContext<void>

// In Webpack 5, a spec added after the dev-server is created won't
// A spec added after the dev-server is created won't
// be included in the compilation. Disabling the caching of this loader ensures
// we regenerate our specs and include any new ones in the compilation.
ctx.cacheable(false)
Expand Down
4 changes: 2 additions & 2 deletions src/makeRspackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const CYPRESS_RSPACK_ENTRYPOINT = path.resolve(__dirname, 'browser.js')

/**
* Removes and/or modifies certain plugins known to conflict
* when used with cypress-rspack-dev-server.
* when used with @rspack/dev-server.
*/
function modifyRspackConfigForCypress(rspackConfig: Partial<Configuration>) {
if (rspackConfig?.plugins) {
Expand All @@ -52,7 +52,7 @@ async function getRspackConfigFromProjectRoot(projectRoot: string) {
}

/**
* Creates a rspack 0 compatible rspack "configuration"
* Creates a rspack compatible rspack "configuration"
* to pass to the sourced rspack function
*/
export async function makeRspackConfig(config: CreateFinalRspackConfig) {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
"baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
Expand Down