Skip to content

Commit 99463ce

Browse files
committed
Enable jest multi-root
1. Jest requires CJS exported module from interface. 2. Jest startup is quite slow :( 3. Made a workspace at the root to develop this package set.
1 parent 346cfba commit 99463ce

14 files changed

+68
-12
lines changed

.vscode/settings.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"editor.tabSize": 4,
2424
},
2525
"eslint.useFlatConfig": true,
26-
"jest.enable": true,
27-
"jest.rootPath": "packages/",
26+
"jest.enable": false,
2827
"vite.autoStart": false,
2928
}

package.cjs-template.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"//": "See fast-check for example of exporting both CJS and ESM. https://github.com/dubzzz/fast-check/blob/main/packages/vitest/package.json",
3+
"type": "commonjs",
4+
"sideEffects": false
5+
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"private": "true",
1515
"workspaces": [
1616
"packages/interface",
17-
"packages/backend",
18-
"packages/frontend"
17+
"packages/frontend",
18+
"packages/backend"
1919
],
2020
"scripts": {
2121
"dev": "concurrently -k -n backend,frontend -c magenta,blue \"npm run dev -w backend\" \"npm run dev -w frontend\"",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"jest.enable": true
3+
}

packages/backend/jest.config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ const pathAliases = {
1313
[packageJson.name]: ['src'],
1414
};
1515

16-
// See here for more info https://kulshekhar.github.io/ts-jest/docs/getting-started/presets/#advanced
16+
/**
17+
* To get this to work for importing external ESM modules (i.e., interface being
18+
* solely ESM, ) You currently need to run NODE_OPTIONS="$NODE_OPTIONS
19+
* --experimental-vm-modules" npm test See
20+
* https://jestjs.io/docs/ecmascript-modules Tracking jest issue
21+
* https://github.com/jestjs/jest/issues/9430 See here for more presets
22+
* https://kulshekhar.github.io/ts-jest/docs/getting-started/presets/#advanced
23+
*/
24+
1725
const preset = createDefaultEsmPreset({
1826
tsconfig: './tsconfig.test.json',
1927
});

packages/backend/jest.setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* https://github.com/dubzzz/fast-check/blob/1ceb2b982b754b99aef8d763723206605b67451e/example/jest.setup.js
44
*/
55

6+
import { jest } from '@jest/globals';
67
import fc from 'fast-check';
78

89
// Default timeout of 60s

packages/backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "backend",
3+
"private": true,
34
"version": "1.0.0",
45
"main": "dist/src/index.js",
56
"type": "module",

packages/backend/tsconfig.test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "./tsconfig.json",
2+
"extends": "./tsconfig.node.json",
33
"compilerOptions": {
44
"outDir": "./build-test",
55
"types": ["node", "jest"],

packages/interface/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
"import": {
1010
"default": "./dist/src/index.js",
1111
"types": "./dist/src/index.d.ts"
12+
},
13+
"require": {
14+
"default": "./dist/cjs/src/index.js",
15+
"types": "./dist/cjs/src/index.d.ts"
1216
}
1317
}
1418
},
@@ -18,7 +22,9 @@
1822
"description": "Interface between frontend and backend",
1923
"scripts": {
2024
"test": "tsc --build ./tsconfig.test.json",
21-
"build": "tsc --build ./tsconfig.prod.json"
25+
"build": "npm run build:prod:esm && npm run build:prod:cjs",
26+
"build:prod:esm": "tsc --build tsconfig.esm.json",
27+
"build:prod:cjs": "tsc --build tsconfig.cjs.json && cp ../../package.cjs-template.json dist/cjs/package.json"
2228
},
2329
"author": "Alex Recuenco",
2430
"license": "MIT",

packages/interface/tsconfig.cjs.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "./tsconfig.esm.json",
3+
"compilerOptions": {
4+
"outDir": "./dist/cjs",
5+
"module": "CommonJS",
6+
"moduleResolution": null,
7+
},
8+
}

0 commit comments

Comments
 (0)