Last active
August 14, 2022 08:37
-
-
Save walkerdb/fd59a6740d80985112b6c4f38101eb68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { describe, expect, it } from 'vitest'; | |
import jscodeshift from 'jscodeshift'; | |
import migrateDefaultExportsToNamedImports from './migrate-default-exports-to-named-exports'; | |
const testData = { | |
fileWithDefaultExport: { | |
before: ` | |
const SomeOtherVar = 'should be unchanged'; | |
const MyExport = 'this should change'; | |
function MyFunctionExport() { return 'this should change' } | |
export default MyExport; | |
export default MyFunctionExport; | |
`, | |
after: ` | |
const SomeOtherVar = 'should be unchanged'; | |
export const MyExport = 'this should change'; | |
export function MyFunctionExport() { return 'this should change' } | |
`, | |
}, | |
fileImportingDefaultExport: { | |
before: ` | |
// should leave these unchanged | |
import styled from 'styled-components/native'; | |
import avatar from './avatar.png'; | |
import avatar2 from './avatar.jpg'; | |
import avatar3 from './avatar.jpeg'; | |
import README from './avatar.md'; | |
// should update these | |
import SomeDefaultExport from './relative/import'; | |
import AnotherDefaultExport, { NamedExport } from './relative/import'; | |
`, | |
after: ` | |
// should leave these unchanged | |
import styled from 'styled-components/native'; | |
import avatar from './avatar.png'; | |
import avatar2 from './avatar.jpg'; | |
import avatar3 from './avatar.jpeg'; | |
import README from './avatar.md'; | |
// should update these | |
import { SomeDefaultExport } from './relative/import'; | |
import { AnotherDefaultExport, NamedExport } from './relative/import'; | |
`, | |
}, | |
fileWithExportDefaultAs: { | |
before: ` | |
export { default as MyExport } from './some/path'; | |
export type { TypeOne, TypeTwo } from './some/path'; // should delete this line | |
export { default as AnotherExport } from './some/other/path'; | |
`, | |
after: ` | |
export * from './some/path'; | |
export * from './some/other/path'; | |
`, | |
}, | |
fileWithADefaultExportThatIsAlreadyANamedExport: { | |
before: ` | |
export const MyConst = () => 'should remain unchanged'; | |
export function MyFunction() { return 'should remain unchanged' } | |
export class MyClass {} | |
export default MyConst; | |
export default MyFunction; | |
export default MyClass; | |
`, | |
after: ` | |
export const MyConst = () => 'should remain unchanged'; | |
export function MyFunction() { return 'should remain unchanged' } | |
export class MyClass {} | |
` | |
}, | |
fileWithInlineDefaultExport: { | |
before: `export default () => "hello";`, | |
// assuming the name of the file is FileName.js | |
after: `export const FileName = () => "hello";`, | |
}, | |
fileWithInlineFunctionExport: { | |
before: `export default function hello() { return "hello" }`, | |
after: `export function hello() { return "hello" }`, | |
}, | |
fileWithInlineClassExport: { | |
before: `export default class Hello extends Object {}`, | |
after: `export class Hello extends Object {}`, | |
}, | |
}; | |
describe('migrate-default-exports-to-named-exports', () => { | |
const runMigration = (source) => | |
migrateDefaultExportsToNamedImports({ source, path: './some/FileName.js' }, { jscodeshift: jscodeshift }); | |
it('migrates files that contain a default export to use named exports instead', () => { | |
const { before, after } = testData.fileWithDefaultExport; | |
expect(runMigration(before)).to.equal(after); | |
}); | |
it('migrates files that use a local default export to use the named export instead', () => { | |
const { before, after } = testData.fileImportingDefaultExport; | |
expect(runMigration(before)).to.equal(after); | |
}); | |
it('migrates files with export-default-as to use export-* instead', () => { | |
const { before, after } = testData.fileWithExportDefaultAs; | |
expect(runMigration(before)).to.equal(after); | |
}); | |
it('migrates inline default exports', () => { | |
const { before, after } = testData.fileWithInlineDefaultExport; | |
expect(runMigration(before)).to.equal(after); | |
}); | |
it('migrates inline default function exports', () => { | |
const { before, after } = testData.fileWithInlineFunctionExport; | |
expect(runMigration(before)).to.equal(after); | |
}); | |
it('migrates inline default class exports', () => { | |
const { before, after } = testData.fileWithInlineClassExport; | |
expect(runMigration(before)).to.equal(after); | |
}); | |
it('does not add double export keywords to declarations that already have a default export AND a name export', () => { | |
const { before, after } = testData.fileWithADefaultExportThatIsAlreadyANamedExport; | |
expect(runMigration(before)).to.equal(after); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment