dsherret / dsherret/ts-morph

Official testing framework/utilities

Open
#686 1 comment 13 reactions 0 assignees View on GitHub
suggestion
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

**Is your feature request related to a problem? Please describe.**

A lack of clear way to test code that uses `ts-morph` to manipulate/generate code.

**Describe the solution you'd like**

Import something from `ts-morph/testing` or such, set it up with some configuration such as project configuration (ES version to use, etc), and then use its method to easily test code.

These functions would be optimized for quick testing.

**Describe alternatives you've considered**

I've quickly written a most straightforward test setup I could think of.

Setup


```ts
import * as tsm from 'ts-morph'
import * as chai from 'chai'
import * as tags from 'common-tags'

export function assertTransform (sourceFileText: string, doTest: (sourceFile: tsm.SourceFile) => void, expectedResultFileText: string) {
const project = new tsm.Project({ useVirtualFileSystem: true })
const file = project.createSourceFile('test.ts', tags.stripIndent(sourceFileText))
doTest(file)
const actualResultFileText = file.getText()
chai.assert.equal(actualResultFileText, tags.stripIndent(expectedResultFileText))
}
```

`common-tags` is a utility module that I use to strips away indentation from the multiline literals (since my code indentation affects them).


Here's how I use it:

Usage


```ts
it(`injects all arguments into arrow function`, () => {
assertTransform(
`
const add = (x, y) => x + y
`,
file => {
const arrowFunction = file
.getVariableDeclarationOrThrow('add')
.getInitializerIfKindOrThrow(tsm.SyntaxKind.ArrowFunction)
injectArguments(arrowFunction, ['first', 'second'])
},
`
const add = (x, y) => (first) + (second)
`,
)
})
```


I noticed that using the virtual system greatly improved performance of tests (~80ms to ~30ms), even though I never saved a file. I suspect that a lot of other things could be improved if the library internally knew that I would only create these files to quickly test something. I'm also unsure if I need to do some clean-up to avoid memory leaks, etc. Also, a new project is created on every test, which I'm pretty sure does a lot of useless work.

Of course, I _could_ build a factory where I would re-use the same file and change its text for every unit test, but this is precisely what I'm opening this issue for: it would be nice for the library to provide _the_ way to test common scenarios for unit testing.

---

I have no idea how other people are using the library, but if a few other people chimed in with their own testing utilities and experiences with testing codemods in general, we might be able come up with a better testing framework by looking at various use-cases that people have.

I understand that this is not an easy task, but I wanted to start the discussion. It was difficult to search through issues the keywords like "test" are overused, so please direct me if a similar one exists already.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.