rsnext/test/unit/eslint-plugin-next/no-head-element.test.ts
Balázs Orbán 73b473991c
chore: add build step to eslint-plugin-next (#38647)
Follow-up on https://github.com/vercel/next.js/pull/38534#pullrequestreview-1035020450

This lets us use modern JS/TS syntax in ESLint rules and avoid issues like #38530 and #36693

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-09-30 01:37:59 +00:00

117 lines
2.5 KiB
TypeScript

import rule from '@next/eslint-plugin-next/dist/rules/no-head-element'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
modules: true,
jsx: true,
},
},
})
const ruleTester = new RuleTester()
ruleTester.run('no-head-element', rule, {
valid: [
{
code: `import Head from 'next/head';
export class MyComponent {
render() {
return (
<div>
<Head>
<title>My page title</title>
</Head>
</div>
);
}
}
`,
filename: 'pages/index.js',
},
{
code: `import Head from 'next/head';
export class MyComponent {
render() {
return (
<div>
<Head>
<title>My page title</title>
</Head>
</div>
);
}
}
`,
filename: 'pages/index.tsx',
},
{
code: `
export default function Layout({ children }) {
return (
<html>
<head>
<title>layout</title>
</head>
<body>{children}</body>
</html>
);
}
`,
filename: './app/layout.js',
},
],
invalid: [
{
code: `
export class MyComponent {
render() {
return (
<div>
<head>
<title>My page title</title>
</head>
</div>
);
}
}`,
filename: './pages/index.js',
errors: [
{
message:
'Do not use `<head>` element. Use `<Head />` from `next/head` instead. See: https://nextjs.org/docs/messages/no-head-element',
type: 'JSXOpeningElement',
},
],
},
{
code: `import Head from 'next/head';
export class MyComponent {
render() {
return (
<div>
<head>
<title>My page title</title>
</head>
<Head>
<title>My page title</title>
</Head>
</div>
);
}
}`,
filename: 'pages/index.ts',
errors: [
{
message:
'Do not use `<head>` element. Use `<Head />` from `next/head` instead. See: https://nextjs.org/docs/messages/no-head-element',
type: 'JSXOpeningElement',
},
],
},
],
})