-
|
Hi CAP community, Since CDS 9.9, vitest is officially supported as a test runner, so I wanted to try it out. The issue comes up when a test suite runs import cds from "@sap/cds";
const { GET, POST, defaults } = cds.test(__dirname + "/..");
defaults.auth = { username: "alice", password: "" };
describe("AdminService OData APIs", () => {
it("serves AdminService.Authors", async () => {
const { data } =
await GET`/odata/v4/admin/Authors ${{ params: { $select: "ID,name" } }}`;
expect(data.value).to.containSubset([{ ID: 101, name: "Emily Brontë" }]);
});
});the cds server is correctly finding the import cds from "@sap/cds"; // correctly loaded
import { Authors, Books } from "#cds-models/AdminService"; // correctly loaded
import { genid } from "./utils/id"; // module cannot be found
export class AdminService extends cds.ApplicationService {
init() {
this.before("NEW", Authors.drafts, genid);
this.before("NEW", Books.drafts, genid);
this.before("CREATE", Authors, genid);
this.before("CREATE", Books, genid);
return super.init();
}
}If I run I created a sample repository here where the issue occurs. The latest versions of vitest, TypeScript and cds are used. Any hints/help on this topic would be greatly appreciated Thx and Regards, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
The CDS server loads your service Fix: register tsx for the vitest process too, so CDS's loads resolve // package.json
"scripts": { "test": "NODE_OPTIONS='--import tsx' vitest" }I reproduced it on your sample repo (cds 9, vitest 4): without the flag |
Beta Was this translation helpful? Give feedback.
The CDS server loads your service
.tsfiles with its own runtime loader, not through vitest's transform, so vitest only transforms the test files. Yoursrv/server.ts(andadmin-service.ts) do the extensionlessimport { genid } from "./utils/id", and with no TS loader active for that load Node can't resolve./utils/id.ts, henceCannot find module .../srv/utils/id imported from .../srv/server.ts.cds wworks because it runs under tsx, which is the active loader there.Fix: register tsx for the vitest process too, so CDS's loads resolve
.ts:I reproduced it on your sample repo (cds 9, vitest 4): without the flag
cds.…