I want to test my typescript library.
It has two parts: core and client. client
instance core
to use its functions.
import { Core } from "@lib/core";
class Client {
constructor() {
new Core();
}
}
To make these two parts independent, I use lerna
as if they were separated npm packages: @lib/core
and @lib/client
.
Everything runs ok in local environment, but in gitlab CI this error is thrown before the client tests start:
Cannot find module '@lib/core' from 'src/lib/client.spec.ts'
3 |
> 4 | jest.mock('@lib/core');
| ^
lerna.json
{
"packages": ["packages/core", "packages/client"],
"version": "independent",
"npmClient": "npm"
}
.gitlab-ci.yml
image: node:14
cache:
paths:
- node_modules
- '**/node_modules'
- .npm
- dist
---
unit:
stage: test
script:
- npx jest --clear-cache
- npm run test
Inside packages/client
I have a node_modules folder with a symlink to @lib/core
.
packages
├── client
│ ├── ng-package.json
│ ├── node_modules
│ │ └── @lib
│ │ └── core -> ../../../core
│ ├── package-lock.json
│ ├── package.json
│ └── src
│ ├── lib
│ │ ├── client.ts
│ └── public-api.ts
└── core
├── ng-package.json
├── package-lock.json
├── package.json
└── src
├── lib
│ ├── core.ts
└── public-api.ts
When the code is compiled, in dist
folder there are the two libraries (client, core). Inside the compiled code of client, the reference to core is like this: require('@lib/core')
.
In local environment it works because I have these paths in tsconfig:
"paths": {
"@lib/core": ["dist/core"],
"@lib/client": ["dist/client"]
}
In ci it tries to download lib/core from the registry and it does not exist.
I would like to make client
import core
code and provide only one library (all together). So the import of core
is transparent to the final user.