generated from bennycode/ts-node-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add snapshot test for barrel files (#90)
- Loading branch information
Showing
20 changed files
with
176 additions
and
53 deletions.
There are no files selected for viewing
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import path from 'node:path'; | ||
import {ProjectUtil} from '../util/ProjectUtil.js'; | ||
import {convertFile} from './convertFile.js'; | ||
|
||
describe('convertFile', () => { | ||
const fixtures = path.join(process.cwd(), 'src', 'test', 'fixtures'); | ||
|
||
it('fixes imports from index files', async () => { | ||
const projectDir = path.join(fixtures, 'index-import'); | ||
const projectConfig = path.join(projectDir, 'tsconfig.json'); | ||
const snapshot = path.join(projectDir, 'src', 'main.snap.ts'); | ||
const project = ProjectUtil.getProject(projectConfig); | ||
|
||
const sourceFile = project.getSourceFile('main.ts')!; | ||
const modifiedFile = convertFile(projectConfig, sourceFile, true); | ||
|
||
await expect(modifiedFile.getText()).toMatchFileSnapshot(snapshot); | ||
}); | ||
|
||
it('fixes imports when tsconfig has an "include" property', async () => { | ||
const projectDir = path.join(fixtures, 'tsconfig-include'); | ||
const projectConfig = path.join(projectDir, 'tsconfig.json'); | ||
const snapshot = path.join(projectDir, 'src', 'consumer.snap.ts'); | ||
const project = ProjectUtil.getProject(projectConfig); | ||
|
||
const sourceFile = project.getSourceFile('consumer.ts')!; | ||
const modifiedFile = convertFile(projectConfig, sourceFile, true); | ||
|
||
await expect(modifiedFile.getText()).toMatchFileSnapshot(snapshot); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {SourceFile, SyntaxKind} from 'ts-morph'; | ||
import {rewrite} from '../main.js'; | ||
import {ProjectUtil} from '../util/ProjectUtil.js'; | ||
|
||
export function convertFile(tsConfigFilePath: string, sourceFile: SourceFile, dryRun: boolean) { | ||
const filePath = sourceFile.getFilePath(); | ||
const project = ProjectUtil.getProject(tsConfigFilePath); | ||
const paths = ProjectUtil.getPaths(project); | ||
const projectDirectory = ProjectUtil.getRootDirectory(tsConfigFilePath); | ||
|
||
let madeChanges: boolean = false; | ||
|
||
sourceFile.getImportDeclarations().forEach(importDeclaration => { | ||
importDeclaration.getDescendantsOfKind(SyntaxKind.StringLiteral).forEach(stringLiteral => { | ||
const hasAttributesClause = !!importDeclaration.getAttributes(); | ||
const adjustedImport = rewrite({ | ||
hasAttributesClause, | ||
paths, | ||
projectDirectory, | ||
sourceFilePath: sourceFile.getFilePath(), | ||
stringLiteral, | ||
}); | ||
madeChanges ||= adjustedImport; | ||
}); | ||
}); | ||
|
||
sourceFile.getExportDeclarations().forEach(exportDeclaration => { | ||
exportDeclaration.getDescendantsOfKind(SyntaxKind.StringLiteral).forEach(stringLiteral => { | ||
const hasAttributesClause = !!exportDeclaration.getAttributes(); | ||
const adjustedExport = rewrite({ | ||
hasAttributesClause, | ||
paths, | ||
projectDirectory, | ||
sourceFilePath: filePath, | ||
stringLiteral, | ||
}); | ||
madeChanges ||= adjustedExport; | ||
}); | ||
}); | ||
|
||
if (madeChanges) { | ||
if (!dryRun) { | ||
sourceFile.saveSync(); | ||
console.log(` Modified (🔧): ${filePath}`); | ||
} | ||
} | ||
|
||
return sourceFile; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {MY_CONSTANT} from './src/index'; | ||
|
||
console.log(MY_CONSTANT); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const MY_CONSTANT = 1337; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {MY_CONSTANT} from './lib/index.js'; | ||
|
||
console.log(MY_CONSTANT); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// @ts-ignore | ||
import {MY_CONSTANT} from './lib'; | ||
|
||
console.log(MY_CONSTANT); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "Node16", | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"target": "es2016" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {MY_CONSTANT} from './lib/producer.js'; | ||
|
||
console.log(MY_CONSTANT); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {MY_CONSTANT} from './lib/producer.js'; | ||
|
||
console.log(MY_CONSTANT); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// @ts-ignore | ||
import {MY_CONSTANT} from './lib/producer'; | ||
|
||
console.log(MY_CONSTANT); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const MY_CONSTANT = 1337; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "Node16", | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"target": "es2016" | ||
}, | ||
"include": ["src"] | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import path from 'node:path'; | ||
import {Project} from 'ts-morph'; | ||
|
||
export const ProjectUtil = { | ||
getPaths: (project: Project) => { | ||
// Note: getCompilerOptions() cannot be cached and has to be used everytime the config is accessed | ||
return project.getCompilerOptions().paths; | ||
}, | ||
getProject: (tsConfigFilePath: string) => { | ||
return new Project({ | ||
// Limit the scope of source files to those directly listed as opposed to also all | ||
// of the dependencies that may be imported. Never want to modify dependencies. | ||
skipFileDependencyResolution: true, | ||
tsConfigFilePath, | ||
}); | ||
}, | ||
getRootDirectory: (tsConfigFilePath: string): string => { | ||
return path.dirname(tsConfigFilePath); | ||
}, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/tsconfig", | ||
"extends": "@tstv/tsconfig-common/tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"types": ["vitest/globals"] | ||
}, | ||
"exclude": ["src/test/fixtures/**"], | ||
"extends": "@tstv/tsconfig-common/tsconfig.json", | ||
"include": ["src/**/*", "vitest.config.ts"] | ||
} |