-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: add initial unit test suite #321
Conversation
cc6ae79
to
16bf8df
Compare
modifications made by agilgur5 to brekk's original commit: - rebase with `master` that is 3 years newer - fix conflicts with newer code, esp the `tsModule`/`tsProxy` changes in this commit - move `jest` config to the bottom and don't reformat `package.json` with tabs
- and ts-jest to v28 - @types/jest doesn't have a v28 yet - look ma, no more vulns!
- and add JSDoc typings to it - per https://jestjs.io/docs/configuration
- most of these options are either unused (like `modulePaths` etc) or covered by Jest's defaults (`moduleFileExtensions`, `testMatch`, etc) - also use `ts-jest` preset per its docs and migration docs - https://kulshekhar.github.io/ts-jest/docs/migration
- per ezolenko's request, though I also prefer to separate them - fix all relative imports to use `../src/` now formatting changes: - improve ordering of imports in tests -- externals first, newline, then internal imports - do this **consistently** - consistently put spaces around braces in destructured imports - really need better linting (and update to ESLint) - consistently add newline after imports and before declarations - in general, add more newlines for readability in several files
- no config needed and uses standard imports too - also required for future Jest ESM support I believe - I created the `jest-without-globals` package before this feature was released in Jest core and contributed a PR for it too, so might have a bias toward not using globals
- basically to match the changes I made when fixing declaration map sources ~1.5 years ago in ec0568b - also to add `cwd` to options - fix: use `toStrictEqual` everywhere, in order to actually check `undefined` properties, as this is necessary for some of the checks - I believe the original author may have intended to actually check `undefined` given that they wrote it in the test, but maybe did not know to use `toStrictEqual` instead of `toEqual`
- instead of re-writing the options multiple times, write up some defaults and use object spread - and refactor `makeDefaultConfig()` to just use `{ ...defaultConfig }` instead to match the rest - re-write normalizePaths to be a `for ... of` loop instead of using `Array.map` - simpler / more straightforward than a function with side-effects - feat: add typings in several places - that's why we're using TS and ts-jest right??
- LanguageServiceHost now requires 3rd argument, cwd, so add that to all usage - add afterDecalarations to transformers - Jest's test.skip can now use async/await, so do that - it was also giving a type error, and this is simpler anyway - change expected to use `__tests__` directory now - fix: use `expect.arrayContaining` for `host.getDirectories` test as there are more temp directories that get added here, such as `build-self` and `coverage` - in general, might be less fragile if this used a generated directory instead of an actual one - but save bigger refactors for later
- instead of using Promises with `done()`, use async/await for clearer code and less indentation too - remove the `truncateName` function as it's not needed; `local` will result in the same name - remove commented out test code - this seemed to just be there for testing the code, and not ready-for-production comments
- basically using pre-defined fixture vars instead of ones defined inside the test - which is more straightforward, easier to read, and less fragile - shorter names too - also use a __temp dir for auto-generated files instead of creating them in the same dir and confusing the editor file tree and potential watchers - change jest config to make sure only spec files are being watched - gitignore __tests__/__temp/ dir in case tests crash etc
- use PluginContext and IContext types instead of `any` - we're using TypeScript right?? - add `debug` level logging in a few places where it was missing - update stubbedContext to have latest PluginContext properties - watcher isn't in there anymore and a few new properties were added - fix type errors with stubbedContext - give it an intersection with IContext for `info` and `debug` verbosity levels - force the logging funcs to `any` as they don't quite match the Rollup types - force them to `any` when deleting them as well because they're not optional properties - Note: would be good to not force so much `any` if possible, but this may be difficult without more advanced internal Rollup mocks - couldn't find any testing packages for this :/ - test: add verbosity expect for debug too - refactor: context2 -> context - there wasn't another one, so just use the same name consistently - I'm guessing there was another one at some point in the development of this and then it was removed but not renamed
- surprisingly only in jest.config.js? - really need to update to @typescript-eslint ...
- run after all the builds for now - it can be done in parallel as a separate job, but then have to duplicate the NPM install etc, so may not be optimal that way - refactor: add test:watch and test:coverage scripts - shortcut scripts so don't have to `npm test -- --coverage" etc - also remove `--verbose` from `test` as that's not necessary
- increases coverage of get-options-overrides significantly - couldn't figure out `rootDirs` -- either the code or my tests are wrong, so just skip that one for now - refactor: move makeStubbedContext etc into a shared fixtures dir so that it can be used for both rollupcontext tests _and_ createFilter tests - in my stylistic opinion, I prefer to put nearly _all_ consts like these into fixtures dir - configure jest to ignore test helpers in coverage reporting such as fixture files - format: '' -> "", add semicolon in some places - I use single quotes and no semicolons, so missed that in a few places - lint didn't check for that and no prettier auto-format :/
- basically using pre-defined fixture vars instead of ones defined inside the test - which is more straightforward, easier to read, and less fragile - shorter names too - left a couple of ones as is where they were only used once very quickly -- could make them fixture vars too but 🤷 - use async/await instead of Promises with `done()` etc - also use more `fs-extra` functions that support Promises instead of synchronous `fs` functions (e.g. `existsSync` -> `pathExists`) - async should be a small optimization for tests too - fix: use __temp dir for auto-generated files instead of creating them in a fixtures dir and breaking actual fixtures - format: a few multi-line statements were able to be condensed to a single line, so do so - esp as multi-line was not helping readability since this is just irrelevant test data (may have hampered readability actually)
16bf8df
to
335b4bf
Compare
- goes over the different ways of running the tests (watch + coverage) - line about unit and integration tests was moved into this section - and altered to reflect the current state of the repo now that a good amount of unit tests exist - also move "Linting and Style" into its own section with a list - move "fix any failed PR checks" to the top as overall guidance on making a PR - previously it was in the middle of linting and style, which felt a bit disjointed (maybe made sense earlier before builds were in CI?) - name the last section "Building and Self-Build"; leave content as is - well mostly, change the first line as "fastest way to test" is not necessarily accurate anymore now that there are actual tests
- original author, brekk, had made these changes, likely because without them, the tests would throw in the source lines where `tsModule` was used with things like "Cannot read property 'ModuleKind' of undefined" - the actual fix for this is to instead use `setTypescriptModule` from tsproxy as this is how it is used in the source - it's mainly needed for when an alternate TS is substituted - brekk probably didn't know the codebase well enough to know that - add `setTypescriptModule` to all specs that need it - 100% test coverage of tsproxy now too! - this should hopefully fix the build errors we were getting as well
It doesn't seem like I have permissions to add Coverage Reporting myself; think you have to be repository "owner" or otherwise have access to the "Settings" panel of the repo, which I don't have access to. |
Summary
Adds an initial batch of unit tests, heavily updated and refactored from #135 (and several new tests added on top as well). Also adds tests to the CI process.
Description
deps: add
jest
for testing,ts-jest
for processing TS files, and@jest/globals
for importing from Jest within testsjest.config.js
with necessary configurationsfixtures/
dir, only match.spec.ts
files for tests (no test helper files)gitignore
thecoverage
directorytest: adds unit tests into
__tests__
dircheck-tsconfig
,context
,diagnostics-format-host
,get-options-override
,host
,nocache
,rollingcache
, androllupcontext
get-options-overrides
; couldn't getexpandIncludeWithDirs
to work, see in-line commentprint-diagnostics
,tscache
, andindex
index
really still needs heavy integration testing with the Rollup APItslib
andtsproxy
are workaround/hacky files, but may be able to test them tootsproxy
is now tested indirectly as it's needed in a few of the specstest: uses
__tests__/__temp/
dir for generated test files.gitignore
'dlint: adds
__tests__
dir to lint dirsci: adds
npm run test:coverage
tosteps
ci: add Node 14, 16, 18, and macOS, Windows to matrix
tslib
helper library #286) and OS differences like POSIX separators, case-insensitivity, etcdocs: adds a testing section to
CONTRIBUTING.md
Review Notes
jest.config.ts
if we addts-node
as a dep. I thought you may not want to add that as a dep so I left it out for nowNext Steps
Can add Coverage Reporting and a badge to the README with something like codecov. I use this in my own libraries, but this requires manual set-up so I didn't add it in the code yet. I might have permissions to do it myself now, but not sure; also not sure if you'd want it on my account vs. yours etc
Still need plenty of integration testing for the plugin itself /
index
. Should use the Rollup API for theseCan optimize the above as well as existing filesystem tests by using an in-memory filesystem /
tmpfs
, e.g.memfs
or with a RAMDiskImprove coverage to 100% of already tested files
get-options-overrides
(createFilter
) #329tslib.ts
(error case) #399Add unit tests for
print-diagnostics
,tscache
, maybe othersparse-tsconfig
spec #397