rsnext/test/unit/eslint-plugin-next/no-unwanted-polyfillio.test.ts
Julien Deniau 8ebd1a24cb
Update polyfill for eslint no-unwanted-polyfillio rule (#33170)
Fixes #33072

I documented all `esXXX` features to be sure that they were already polyfilled. Only `es2019` feature `Object.fromEntries` is not already polyfilled by nextjs.

I added some unwanted polyfill (that are polyfilled by nextjs).

I kept the `es5`, `es6` and `es2015` "as-is" as they contain functions that does not seem to be explicitly polyfilled (all `Math` functions or `Date.now` for example) in the [polyfill file](https://github.com/vercel/next.js/blob/master/packages/next-polyfill-nomodule/src/index.js)

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-08-08 05:56:31 +00:00

135 lines
3.9 KiB
TypeScript

import rule from '@next/eslint-plugin-next/lib/rules/no-unwanted-polyfillio'
import { RuleTester } from 'eslint'
;(RuleTester as any).setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
modules: true,
jsx: true,
},
},
})
const ruleTester = new RuleTester()
ruleTester.run('unwanted-polyfillsio', rule, {
valid: [
`import {Head} from 'next/document';
export class Blah extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<script src='https://polyfill.io/v3/polyfill.min.js?features=AbortController'></script>
</div>
);
}
}`,
`import {Head} from 'next/document';
export class Blah extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<script src='https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver'></script>
</div>
);
}
}`,
`import Script from 'next/script';
export function MyApp({ Component, pageProps }) {
return (
<div>
<Component {...pageProps} />
<Script src='https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver' />
</div>
);
}`,
],
invalid: [
{
code: `import {Head} from 'next/document';
export class Blah extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<script src='https://polyfill.io/v3/polyfill.min.js?features=WeakSet%2CPromise%2CPromise.prototype.finally%2Ces2015%2Ces5%2Ces6'></script>
</div>
);
}
}`,
errors: [
{
message:
'No duplicate polyfills from Polyfill.io are allowed. WeakSet, Promise, Promise.prototype.finally, es2015, es5, es6 are already shipped with Next.js. See: https://nextjs.org/docs/messages/no-unwanted-polyfillio',
type: 'JSXOpeningElement',
},
],
},
{
code: `
export class Blah {
render() {
return (
<div>
<h1>Hello title</h1>
<script src='https://polyfill.io/v3/polyfill.min.js?features=Array.prototype.copyWithin'></script>
</div>
);
}
}`,
errors: [
{
message:
'No duplicate polyfills from Polyfill.io are allowed. Array.prototype.copyWithin is already shipped with Next.js. See: https://nextjs.org/docs/messages/no-unwanted-polyfillio',
type: 'JSXOpeningElement',
},
],
},
{
code: `import NextScript from 'next/script';
export function MyApp({ Component, pageProps }) {
return (
<div>
<Component {...pageProps} />
<NextScript src='https://polyfill.io/v3/polyfill.min.js?features=Array.prototype.copyWithin' />
</div>
);
}`,
errors: [
{
message:
'No duplicate polyfills from Polyfill.io are allowed. Array.prototype.copyWithin is already shipped with Next.js. See: https://nextjs.org/docs/messages/no-unwanted-polyfillio',
type: 'JSXOpeningElement',
},
],
},
{
code: `import {Head} from 'next/document';
export class ES2019Features extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<script src='https://polyfill.io/v3/polyfill.min.js?features=Object.fromEntries'></script>
</div>
);
}
}`,
errors: [
{
message:
'No duplicate polyfills from Polyfill.io are allowed. Object.fromEntries is already shipped with Next.js. See: https://nextjs.org/docs/messages/no-unwanted-polyfillio',
},
],
},
],
})