-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(test): heavily simplify the
context
helper (#404)
* refactor(test): heavily simplify the `context` helper - since we're type-casting it anyway, we can heavily simplify this and remove the stubs entirely - they're actually unused in the unit tests, so we don't need them at all - besides the type-checking, which we force with a cast anyway - the `as unknown as` is bad practice, and probably why I didn't use it initially (plus other typing issues), but it's much simpler this way and reflects the intent better -- just making it type-check with the few properties we use - we can also use Jest mocks directly instead of the hacky `contextualLogger` and passing `data` in - `makeContext` now creates the mocks, so we just need to check against `context.error` etc - this is _much_ more familiar as it's what we use in the source and follows idiomatic Jest - rewrite all the checks to test against the mocks instead - I thought this felt too complicated / verbose before, but I had left this as is from brekk's initial test structure - now that I understand all the tests and test intent much better, I could rewrite this to be a good bit simpler - make the `toBeFalsy()` checks more precise by checking that the mock _wasn't_ called - it returns `void` anyway, so `toBeFalsy()` _always_ returns true; it's not much of a test - checking that the low verbosity level didn't trigger the mock to be called actually checks the test's intent * make sure to check funcs in context calls too - `get-options-overrides`'s coverage func % decreased bc funcs passed to `context.debug` weren't being called - took me a bit to notice too since we have no coverage checks - and then another bit to realize _why_ it decreased
- Loading branch information
Showing
4 changed files
with
54 additions
and
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,21 @@ | ||
import { jest } from "@jest/globals"; | ||
import { PluginContext } from "rollup"; | ||
|
||
import { IContext } from "../../src/context"; | ||
|
||
const stub = (x: any) => x; | ||
// if given a function, make sure to call it (for code coverage etc) | ||
function returnText (message: string | (() => string)) { | ||
if (typeof message === "string") | ||
return message; | ||
|
||
const contextualLogger = (data: any): IContext => { | ||
return { | ||
warn: (x: any) => { | ||
data.warn = x; | ||
}, | ||
error: (x: any) => { | ||
data.error = x; | ||
}, | ||
info: (x: any) => { | ||
data.info = x; | ||
}, | ||
debug: (x: any) => { | ||
data.debug = x; | ||
}, | ||
}; | ||
}; | ||
return message(); | ||
} | ||
|
||
export function makeStubbedContext (data: any): PluginContext & IContext { | ||
const { warn, error, info, debug } = contextualLogger(data); | ||
export function makeContext(): PluginContext & IContext { | ||
return { | ||
addWatchFile: stub as any, | ||
getWatchFiles: stub as any, | ||
cache: stub as any, | ||
load: stub as any, | ||
resolve: stub as any, | ||
resolveId: stub as any, | ||
isExternal: stub as any, | ||
meta: stub as any, | ||
emitAsset: stub as any, | ||
emitChunk: stub as any, | ||
emitFile: stub as any, | ||
setAssetSource: stub as any, | ||
getAssetFileName: stub as any, | ||
getChunkFileName: stub as any, | ||
getFileName: stub as any, | ||
parse: stub as any, | ||
warn: warn as any, | ||
error: error as any, | ||
info: info as any, | ||
debug: debug as any, | ||
moduleIds: stub as any, | ||
getModuleIds: stub as any, | ||
getModuleInfo: stub as any | ||
}; | ||
error: jest.fn(returnText), | ||
warn: jest.fn(returnText), | ||
info: jest.fn(returnText), | ||
debug: jest.fn(returnText), | ||
} as unknown as PluginContext & IContext; | ||
}; |
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