Home > Enterprise >  "Error: Cross origin http://localhost forbidden"
"Error: Cross origin http://localhost forbidden"

Time:10-05

I'm testing login in react, the test is passing but this error is happening

Error: Cross origin http://localhost forbidden

I already searched and didn't find anything about it in react, only in node

it('make login', async () => {
server.use(
    rest.post('http://localhost:2001/backend_ihm/auth/login', async (req, res, ctx) => {
        const { username, password, keepLogin } = await req.json();
        return res(
            ctx.json(user)
        );
    }));

const { getByTestId } = render(
    <Router>
        <AuthProvider>
            <Login />
        </AuthProvider>
    </Router>
);

await screen.getByTestId('loading');

await waitForElementToBeRemoved(() => screen.getByTestId(/loading/i));

await screen.getByTestId('login-container');

const buttonLogin = getByTestId('button-login');
const checkboxKeepLogin = getByTestId('f');

const { inputUsername, inputPassword } = setup();

fireEvent.change(inputUsername, { target: { value: 'iplabs' } });
fireEvent.change(inputPassword, { target: { value: '123' } });

fireEvent.click(checkboxKeepLogin);
fireEvent.click(buttonLogin);

});

CodePudding user response:

You can control CORS from server. The web server can add CORS headers to enable browser to call your server from different domain.

Otherwise, If you use vite or cra to develop your react app, you can configure out of the box a proxy for all server requests.

CodePudding user response:

I see you use JEST JS.

As it is refered here : https://github.com/axios/axios/issues/1754

It visibly comes from JEST's default environment.

Add this to your jest.config.js file :
{
"testEnvironment": "node"
}

  • Related