From e8d08d039e7d8ad20af5dd74b59935d73027d279 Mon Sep 17 00:00:00 2001 From: Christian Georgi Date: Fri, 4 Jun 2021 14:30:12 +0200 Subject: [PATCH] Add example for starting cds with ts-node --- hello/package.json | 5 +++-- hello/world.ts | 5 +++++ package.json | 8 ++++++-- test/hello-world-ts.test.ts | 13 +++++++++++++ 4 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 hello/world.ts create mode 100644 test/hello-world-ts.test.ts diff --git a/hello/package.json b/hello/package.json index ef9c6602..9ba3a3b7 100644 --- a/hello/package.json +++ b/hello/package.json @@ -2,6 +2,7 @@ "name": "@capire/hello-world", "version": "1.0.0", "scripts": { - "watch": "cds serve world.cds" + "watch": "cds serve world.cds", + "watch:ts": "npx ts-node ../node_modules/@sap/cds/bin/cds serve world.cds" } -} \ No newline at end of file +} diff --git a/hello/world.ts b/hello/world.ts new file mode 100644 index 00000000..956946ac --- /dev/null +++ b/hello/world.ts @@ -0,0 +1,5 @@ +module.exports = class say { + hello(req: any) { + return `Hello ${req.data.to} from a typescript file!` + } +} diff --git a/package.json b/package.json index 0d0b406c..df5c3c1e 100644 --- a/package.json +++ b/package.json @@ -16,11 +16,14 @@ "express": "^4" }, "devDependencies": { + "@types/jest": "^26.0.23", + "@types/node": "^15.12.0", "cds-swagger-ui-express": "^0.2.0", "chai": "^4.2.0", "chai-as-promised": "^7.1.1", "chai-subset": "^1.6.0", - "sqlite3": "^5.0.0" + "sqlite3": "^5.0.0", + "ts-jest": "^27.0.2" }, "scripts": { "cleanup": "rm -rf node_modules && rm -rf */node_modules && rm -rf */*/node_modules", @@ -30,7 +33,8 @@ "media": "cds watch media", "mocha": "npx mocha || echo", "jest": "npx jest", - "test": "npm run jest --silent" + "test": "npm run jest --silent --testMatch test/*.test.js && npm run test:ts", + "test:ts": "npm run jest --silent --preset ts-jest --testMatch test/*.test.ts" }, "mocha": { "parallel": true diff --git a/test/hello-world-ts.test.ts b/test/hello-world-ts.test.ts new file mode 100644 index 00000000..09ca3ef2 --- /dev/null +++ b/test/hello-world-ts.test.ts @@ -0,0 +1,13 @@ +const testKit = require('../test') +process.env.TYPESCRIPT_SUPPORT = 'true'; + +const {GET} = testKit.run('serve', 'hello/world.cds') + +describe('Hello world!', () => { + + it('should say hello with class impl from a typescript file', async () => { + const {data} = await GET`/say/hello(to='world')` + expect(data.value).toEqual('Hello world from a typescript file!') + }) + +})