Skip to content

Commit e86e0ae

Browse files
committed
Check types for mocks
1 parent 0902ea8 commit e86e0ae

3 files changed

Lines changed: 29 additions & 7 deletions

File tree

packages/pluggable-widgets-tools/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
},
1313
"scripts": {
1414
"prepack": "shx rm -rf dist && tsc",
15-
"test": "jest"
15+
"typecheck:test-config": "tsc -p test-config/tsconfig.json",
16+
"test": "tsc -p test-config/tsconfig.json && jest"
1617
},
1718
"files": [
1819
"bin",

packages/pluggable-widgets-tools/test-config/__mocks__/mendix.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1-
// Runtime mock for the types-only `mendix` package, whose real entry throws
2-
// ("This package should not be used in the runtime"). `@swc/jest` can't inline
3-
// the ambient `const enum ValueStatus`, so it emits a live `require("mendix")`
4-
// that needs this safe module exporting the enum values widget code uses.
5-
//
6-
// Keep values in sync with node_modules/mendix/index.d.ts.
1+
// @ts-check
2+
// Runtime mock for the types-only `mendix` package (its real entry throws).
3+
// Stays plain JS because it ships inside the consumer's node_modules, where
4+
// jest won't transform TypeScript. The JSDoc `@type` annotations check the
5+
// values against `mendix`'s types (repo-only, via test-config/tsconfig.json)
6+
// and are erased at runtime.
7+
8+
/** @typedef {typeof import("mendix")} MendixModule */
9+
10+
/**
11+
* Map an enum's type to `{ [Member]: itsStringValue }`.
12+
* @template E
13+
* @typedef {{ [K in keyof E]: `${Extract<E[K], string>}` }} EnumMock
14+
*/
15+
716
module.exports = {
17+
/** @type {EnumMock<MendixModule["ValueStatus"]>} */
818
ValueStatus: {
919
Available: "available",
1020
Unavailable: "unavailable",
1121
Loading: "loading"
1222
},
23+
/** @type {EnumMock<MendixModule["FormatterType"]>} */
1324
FormatterType: {
1425
Number: "number",
1526
DateTime: "datetime"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../configs/tsconfig.base",
3+
"compilerOptions": {
4+
"noEmit": true,
5+
"allowJs": true,
6+
"checkJs": true,
7+
"moduleResolution": "bundler"
8+
},
9+
"include": ["__mocks__/mendix.js"]
10+
}

0 commit comments

Comments
 (0)