Skip to content

Commit e421fa8

Browse files
authored
test: remove yarn pack workaround (#232)
Remove the `yarn pack` workaround for testing. Using yarn link: or portal: protocol was not enough, we had to also link the ts-node dependency directly into the package node_modules so it could resolve it
1 parent 780a320 commit e421fa8

File tree

8 files changed

+70
-51
lines changed

8 files changed

+70
-51
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,9 @@ jobs:
3939

4040
- run: yarn build
4141

42-
- name: Generate package archive
43-
run: yarn pack
44-
4542
- name: Install dependencies for testing
4643
run: yarn install
4744
working-directory: test
48-
env:
49-
YARN_ENABLE_IMMUTABLE_INSTALLS: false
5045

5146
- name: Test
5247
working-directory: test

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,3 @@ fabric.properties
7575

7676
# Editor-based Rest Client
7777
.idea/httpRequests
78-
79-
package.tgz

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
"types": "types/index.d.ts",
77
"scripts": {
88
"compile": "tsc",
9-
"build": "rm -rf dist && yarn run clean && yarn run compile",
10-
"test": "yarn test/ install && yarn test/ add-package && yarn test/ test",
9+
"build": "yarn clean && yarn compile",
10+
"test": "yarn test/ test",
1111
"release": "changelogen --release --push",
1212
"--------------": "",
1313
"format": "prettier --write .",
14-
"clean": "rm -rf dist \"**/*.tsbuildinfo\" ./test/projects/nx/dist",
15-
"clean:all": "yarn run clean && rm -rf node_modules \"**/node_modules\" \"**/yarn.lock\" yarn.lock",
16-
"reset": "yarn run clean:all && yarn install && yarn build",
14+
"clean": "rm -rf \"**/dist\"",
15+
"clean:all": "yarn clean && rm -rf node_modules \"**/node_modules\" \"**/yarn.lock\" yarn.lock",
16+
"reset": "yarn clean:all && yarn install && yarn build",
1717
"-------------- ": "",
1818
"prepack": "yarn build"
1919
},

test/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"scripts": {
55
"test": "jest",
66
"g:ts-node": "cd $INIT_CWD && ts-node",
7-
"add-package": "yarn ../ pack && yarn add ../package.tgz",
8-
"postinstall": "node prepare.js"
7+
"postinstall": "node prepare.mjs"
98
},
109
"devDependencies": {
1110
"@nrwl/cli": "^15.9.7",
1211
"@nrwl/js": "^15.9.7",
1312
"@nrwl/node": "^15.9.7",
1413
"@nrwl/workspace": "^15.9.7",
14+
"@tsconfig/node18": "^18.2.4",
1515
"@types/jest": "^29.5.12",
1616
"jest": "^29.7.0",
1717
"nx": "^15.9.7",
@@ -25,7 +25,7 @@
2525
"typescript": "^5.5.4",
2626
"typescript-four-seven": "npm:[email protected]",
2727
"typescript-three": "npm:[email protected]",
28-
"typescript-transform-paths": "../package.tgz"
28+
"typescript-transform-paths": "portal:../"
2929
},
3030
"workspaces": [
3131
"projects/*"

test/prepare.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/prepare.mjs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// @ts-check
2+
import { existsSync } from "node:fs";
3+
import { symlink } from "node:fs/promises";
4+
import { dirname, join, resolve } from "node:path";
5+
import { fileURLToPath } from "node:url";
6+
import { patch } from "ts-patch";
7+
import { patch as patch1 } from "tsp1";
8+
import { patch as patch2 } from "tsp2";
9+
10+
const __dirname = dirname(fileURLToPath(import.meta.url)); // https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules
11+
12+
async function symlinkTsNode() {
13+
// we need to patch the root package node_modules in order for it to locate ts-node for the register tests.
14+
// installing ts-node as a depenency in the root is not an option since it would create two copies of ts-node
15+
// thus messing with the mocks in the tests
16+
const target = resolve(__dirname, "node_modules/ts-node");
17+
const path = resolve(__dirname, "../node_modules/ts-node");
18+
19+
if (!existsSync(path)) await symlink(target, path, "junction");
20+
}
21+
22+
function patchTsModules() {
23+
/* ****************************************************************************************************************** *
24+
* Config
25+
* ****************************************************************************************************************** */
26+
const rootDir = __dirname;
27+
const tsDirs = ["typescript-three", "typescript-four-seven", "typescript"];
28+
/* ****************************************************************************************************************** *
29+
* Patch TS Modules
30+
* ****************************************************************************************************************** */
31+
32+
const baseDirs = new Map();
33+
34+
for (const tsDirName of tsDirs) {
35+
const mainDir = resolve(rootDir, "node_modules", tsDirName);
36+
if (!existsSync(join(mainDir, "lib-backup"))) baseDirs.set(tsDirName, mainDir);
37+
}
38+
39+
// Patch discovered modules
40+
for (const [dirName, dir] of baseDirs)
41+
if (dirName === "typescript-three") patch1(["tsc.js", "typescript.js"], { basedir: dir });
42+
else if (dirName === "typescript-four-seven") patch2(["tsc.js", "typescript.js"], { dir });
43+
else patch(["tsc.js", "typescript.js"], { dir });
44+
}
45+
46+
patchTsModules();
47+
await symlinkTsNode();

test/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
2-
"extends": "../tsconfig.base",
2+
"extends": "@tsconfig/node18",
33
"include": ["tests", "utils"],
44

55
"compilerOptions": {
6+
"types": ["jest", "node"],
67
"noEmit": true,
78
"strict": true,
89
"esModuleInterop": true

test/yarn.lock

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,6 +2210,13 @@ __metadata:
22102210
languageName: node
22112211
linkType: hard
22122212

2213+
"@tsconfig/node18@npm:^18.2.4":
2214+
version: 18.2.4
2215+
resolution: "@tsconfig/node18@npm:18.2.4"
2216+
checksum: 10c0/cdfd17f212660374eb2765cd5907b2252e43cfa2623cd52307a49f004327ef49bbe7d53c78b0aca57f33e9a5cb0d7d2eb5ded9be1235e6212f65c9f0699322b6
2217+
languageName: node
2218+
linkType: hard
2219+
22132220
"@types/babel__core@npm:^7.1.14":
22142221
version: 7.20.5
22152222
resolution: "@types/babel__core@npm:7.20.5"
@@ -8065,6 +8072,7 @@ __metadata:
80658072
"@nrwl/js": "npm:^15.9.7"
80668073
"@nrwl/node": "npm:^15.9.7"
80678074
"@nrwl/workspace": "npm:^15.9.7"
8075+
"@tsconfig/node18": "npm:^18.2.4"
80688076
"@types/jest": "npm:^29.5.12"
80698077
jest: "npm:^29.7.0"
80708078
nx: "npm:^15.9.7"
@@ -8078,7 +8086,7 @@ __metadata:
80788086
typescript: "npm:^5.5.4"
80798087
typescript-four-seven: "npm:[email protected]"
80808088
typescript-three: "npm:[email protected]"
8081-
typescript-transform-paths: ../package.tgz
8089+
typescript-transform-paths: "portal:../"
80828090
languageName: unknown
80838091
linkType: soft
80848092

@@ -9243,16 +9251,15 @@ __metadata:
92439251
languageName: node
92449252
linkType: hard
92459253

9246-
"typescript-transform-paths@file:../package.tgz::locator=root-workspace-0b6124%40workspace%3A.":
9247-
version: 3.4.9
9248-
resolution: "typescript-transform-paths@file:../package.tgz#../package.tgz::hash=396016&locator=root-workspace-0b6124%40workspace%3A."
9254+
"typescript-transform-paths@portal:../::locator=root-workspace-0b6124%40workspace%3A.":
9255+
version: 0.0.0-use.local
9256+
resolution: "typescript-transform-paths@portal:../::locator=root-workspace-0b6124%40workspace%3A."
92499257
dependencies:
92509258
minimatch: "npm:^3.1.2"
92519259
peerDependencies:
92529260
typescript: ">=3.6.5"
9253-
checksum: 10c0/14b7936f4491a916375e3aa18bc771d3b02c8267526ad04e5562414098b29e07be9fa2ca0b46207c412ba5c5d0a35ae9543d56634a6c8672d92ca8ff46e3610f
92549261
languageName: node
9255-
linkType: hard
9262+
linkType: soft
92569263

92579264
"typescript@npm:^5.5.4":
92589265
version: 5.5.4

0 commit comments

Comments
 (0)