Skip to content

Commit

Permalink
fix(nextjs): Adding styles to nextjs cypress should not fail. (#22170)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndcunningham committed Mar 5, 2024
1 parent bf1dcdc commit 391f3ab
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
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

0 comments on commit 391f3ab

Please sign in to comment.