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

fix(nextjs): Adding styles to nextjs cypress should not fail. #22170

Merged
merged 1 commit into from
Mar 5, 2024
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
51 changes: 51 additions & 0 deletions e2e/next-extensions/src/next-component-tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ describe('NextJs Component Testing', () => {
);
}
});

it('should test a NextJs server component that uses router', async () => {
const lib = uniq('next-lib');
createLibWithCtCypress(lib);
if (runE2ETests()) {
expect(runCLI(`component-test ${lib}`)).toContain('All specs passed!');
}
}, 600_000);
});

function addBabelSupport(path: string) {
Expand Down Expand Up @@ -203,6 +211,49 @@ export default Button;
`generate @nx/next:cypress-component-configuration --project=${libName} --generate-tests --no-interactive`
);
}

function createLibWithCtCypress(libName: string) {
runCLI(
`generate @nx/next:lib ${libName} --no-interactive --projectNameAndRootFormat=as-provided`
);

runCLI(
`generate @nx/next:cypress-component-configuration --project=${libName} --no-interactive`
);

updateFile(`${libName}/src/lib/hello-server.tsx`, () => {
return `import { useRouter } from 'next/router';

export function HelloServer() {
useRouter();

return <h1>Hello Server</h1>;
}
`;
});
// Add cypress component test
createFile(
`${libName}/src/lib/hello-server.cy.tsx`,
`import * as Router from 'next/router';
import { HelloServer } from './hello-server';

describe('HelloServer', () => {
context('stubbing out \`useRouter\` hook', () => {
let router;
beforeEach(() => {
router = cy.stub();

cy.stub(Router, 'useRouter').returns(router);
});
it('should work', () => {
cy.mount(<HelloServer />);
});
});
});

`
);
}
function addTailwindToLib(libName: string) {
createFile(`libs/${libName}/src/lib/styles.css`, ``);
runCLI(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function normalizeOptions(
return {
addPlugin,
...options,
framework: options.framework ?? null,
directory: options.directory ?? 'cypress',
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title><%= project %> Components App</title>

<% if(framework === 'next') { %>
<!-- Used by Next.js to inject CSS. -->
<div id="__next_css__DO_NOT_USE__"></div>
<% } %>
</head>
<body>
<div data-cy-root></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface CypressComponentConfigurationSchema {
* @internal
*/
addExplicitTargets?: boolean;
framework?: 'next';
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function cypressComponentConfigurationInternal(
await baseCyCtConfig(tree, {
project: options.project,
skipFormat: true,
framework: 'next',
jsx: true,
addPlugin: options.addPlugin,
})
Expand Down