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

Refactor webpack-dev-server middleware application logic #5149

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/Server.js
Expand Up @@ -552,6 +552,58 @@ class Server {
searchParams.set("password", webSocketURL.password);
}

// Initialize an array to keep track of applied middleware
const appliedMiddleware = [];

// Function to apply middleware only once
function applyMiddlewareOnce(app, middleware) {
// Check if the middleware has already been applied
if (!appliedMiddleware.includes(middleware)) {
// Apply the middleware
app.use(middleware);

// Add the middleware to the applied middleware array
appliedMiddleware.push(middleware);
}
}

// Test cases for applyMiddlewareOnce function
describe('applyMiddlewareOnce', () => {
let app;

beforeEach(() => {
// Mock Express app
app = {
use: jest.fn()
};
});

afterEach(() => {
jest.clearAllMocks();
});

it('should apply middleware only once', () => {
const middlewareFunction = jest.fn();
applyMiddlewareOnce(app, middlewareFunction);
applyMiddlewareOnce(app, middlewareFunction);

expect(app.use).toHaveBeenCalledTimes(1);
expect(app.use).toHaveBeenCalledWith(middlewareFunction);
});

it('should apply different middleware functions separately', () => {
const middlewareFunction1 = jest.fn();
const middlewareFunction2 = jest.fn();
applyMiddlewareOnce(app, middlewareFunction1);
applyMiddlewareOnce(app, middlewareFunction2);

expect(app.use).toHaveBeenCalledTimes(2);
expect(app.use).toHaveBeenCalledWith(middlewareFunction1);
expect(app.use).toHaveBeenCalledWith(middlewareFunction2);
});
});
Copy link
Member

Choose a reason for hiding this comment

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

We should not store it here

Copy link
Author

Choose a reason for hiding this comment

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

So please suggest for proceedings

Copy link
Member

Choose a reason for hiding this comment

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

We have the test directory

Copy link
Author

Choose a reason for hiding this comment

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

can you suggest me file name , it can be big help

Copy link
Author

Choose a reason for hiding this comment

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

open-option.test.js or proxy-option.test.js



/** @type {string} */
let hostname;

Expand Down