-
Notifications
You must be signed in to change notification settings - Fork 5
Types #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
daogrady
wants to merge
23
commits into
main
Choose a base branch
from
feat/types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Types #17
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0c00c93
First few types
daogrady 2c19746
Reuse Axios types
daogrady 6b129cf
Formatting
daogrady 9112ec4
More types
daogrady 64651b9
Merge branch 'main' into feat/types
daogrady 9aea8a8
Restore types
daogrady cec36d4
More types
daogrady 3099e43
Add globals
daogrady a6992c4
More types
daogrady d4727ef
Types
daogrady e064709
Types
daogrady 61f782e
Types
daogrady d780811
Types
daogrady 99a219d
Types
daogrady 1af46bf
Types
daogrady 31d21e6
Types
daogrady 48d97dc
Types
daogrady 96250b0
Types
daogrady 64f7214
Types
daogrady c977026
Types
daogrady 365fd8e
Types
daogrady f4855ec
Types
daogrady 3553c93
Types
daogrady File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import type { HookFn, mock } from 'node:test' | ||
| import type each_type from './lib/fixtures/test-each.js' | ||
|
|
||
| declare global { | ||
| var _cds_test_fixture: any; | ||
|
|
||
| // when extending global, only var can be used | ||
| var describe: { | ||
| (message: string, method: Function): void, | ||
| each?: typeof each_type; | ||
| skip: { | ||
| (...xs:any[]): unknown | ||
| each?: () => void; | ||
| } | ||
| }; | ||
| var xdescribe: typeof describe['skip']; | ||
| var it: { | ||
| each?: typeof each_type, | ||
| skip: () => void | ||
| } | ||
| var test: typeof it; | ||
| var xtest: typeof it['skip']; | ||
| function before(method: Function): void; | ||
| function before(message: string & Function?, method: Function): void; | ||
| function beforeEach(method: Function): void; | ||
| function beforeAll(method: Function): void; | ||
| function beforeAll(message: string & Function?, method: Function): void; | ||
| function after(method: Function): void; | ||
| function after(message: string & Function?, method: Function): void; | ||
| function afterEach(method: HookFn): void; | ||
| function afterAll(method: Function): void; | ||
| function afterAll(message: string & Function?, method: Function): void; | ||
| function expect(_:any): void; | ||
|
|
||
| var chai: { | ||
| expect: typeof expect, | ||
| should?: () => void, | ||
| fake?: boolean, | ||
| } | ||
|
|
||
| var jest: { | ||
| fn: () => void; | ||
| spyOn: typeof mock.method; | ||
| restoreAllMocks: () => void; | ||
| resetAllMocks: () => void; | ||
| clearAllMocks: () => void; | ||
| clearAllTimers: () => void; | ||
| mock: ( | ||
| module: string | unknown, | ||
| fn?: Function, | ||
| o?: { virtual?: boolean } | ||
| ) => void; | ||
| setTimeout: () => void; | ||
| } | ||
|
|
||
| // cds-dk types | ||
| var cds: { | ||
| repl: unknown | ||
| } | ||
| } | ||
| export {}; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,14 +7,21 @@ class Test extends require('./axios') { | |
| * Allows: const { GET, expect, test } = cds.test() | ||
| */ | ||
| test = this | ||
| /** @type {import('node:timers/promises').setTimeout | undefined} */ | ||
| #sleep | ||
| /** @type {import('./data') | undefined} */ | ||
| #data | ||
|
|
||
| /** @returns {import('@sap/cds')} */ | ||
| get cds() { return require('@sap/cds/lib') } | ||
| get sleep() { return super.sleep = require('node:timers/promises').setTimeout } | ||
| get data() { return super.data = new (require('./data'))} | ||
| get sleep() { return this.#sleep = require('node:timers/promises').setTimeout } | ||
| get data() { return this.#data = new (require('./data'))} | ||
|
|
||
| /** | ||
| * Launches a cds server with arbitrary port and returns a subclass which | ||
| * also acts as an axios lookalike, providing methods to send requests. | ||
| * @param {string} folder_or_cmd - either a folder to serve or the command 'serve' or 'run' | ||
| * @param {...string} args - additional arguments, e.g. '--project', 'myapp' | ||
| */ | ||
| run (folder_or_cmd, ...args) { | ||
|
|
||
|
|
@@ -29,14 +36,16 @@ class Test extends require('./axios') { | |
| before (async ()=>{ | ||
| process.env.cds_test_temp = cds.utils.path.resolve (cds.root,'_out',''+process.pid) | ||
| if (!args.includes('--port')) args.push ('--port', '0') | ||
| // @ts-expect-error - cds.exec is not in types (cds-dk?) | ||
| let { server, url } = await cds.exec (...args) | ||
| this.server = server | ||
| this.url = url | ||
| }) | ||
|
|
||
| // gracefully shutdown cds server... | ||
| after (()=> Promise.all([ | ||
| cds.utils.rimraf (process.env.cds_test_temp), | ||
| cds.utils.rimraf (/** @type {string} */(process.env.cds_test_temp)), | ||
| // @ts-expect-error - cds.shutdown not in types (cds-dk?) | ||
| cds.shutdown(), | ||
| ])) | ||
|
|
||
|
|
@@ -47,6 +56,8 @@ class Test extends require('./axios') { | |
| * Serving projects from subfolders under the root specified by a sequence | ||
| * of path components which are concatenated with path.resolve(). | ||
| * Checks conflicts with cds.env loaded in other folder before. | ||
| * @param {string} folder - folder name | ||
| * @param {...string} paths - additional path components | ||
| */ | ||
| in (folder, ...paths) { | ||
| if (!folder) return this | ||
|
|
@@ -63,36 +74,47 @@ class Test extends require('./axios') { | |
| if (process.env.CDS_TEST_ENV_CHECK) { | ||
| const env = Reflect.getOwnPropertyDescriptor(cds,'env')?.value | ||
| if (env && env._home !== folder && env.stack) { | ||
| let filter = line => !line.match(/node_modules\/jest-|node:internal/) | ||
| let filter = (/** @type {string} */line) => !line.match(/node_modules\/jest-|node:internal/) | ||
| let err = new Error; err.message = | ||
| `Detected cds.env loaded before running cds.test in different folder: \n` + | ||
| `1. cds.env loaded from: ${local(cds.env._home)||'./'} \n` + | ||
| `2. cds.test running in: ${local(folder)} \n\n` + | ||
| err.stack.split('\n').filter(filter).slice(1).join('\n') | ||
| err.stack?.split('\n').filter(filter).slice(1).join('\n') | ||
| err.stack = env.stack.split('\n').filter(filter).slice(1).join('\n') | ||
| throw err | ||
| } | ||
| } | ||
| // @ts-expect-error - cds.root is readonly in types | ||
| cds.root = folder | ||
| return this | ||
| } | ||
|
|
||
| /** | ||
| * Method to spy on a function in an object, similar to jest.spyOn(). | ||
| * @template {any} T | ||
| * @param {T} o - object | ||
| * @param {keyof T} f - function name | ||
| */ | ||
| spy (o,f) { | ||
| const origin = o[f] | ||
| const origin = /** @type {Function} */(o[f]) | ||
| /** | ||
| * @this {Test} | ||
| * @param {...any} args - arguments | ||
| */ | ||
| const fn = function (...args) { | ||
| ++fn.called | ||
| return origin.apply(this,args) | ||
| return origin.apply(this, args) | ||
| } | ||
| fn.called = 0 | ||
| // @ts-expect-error - | ||
| fn.restore = ()=> o[f] = origin | ||
| // @ts-expect-error - | ||
| return o[f] = fn | ||
| } | ||
|
|
||
| /** | ||
| * For usage in repl, e.g. var test = await cds.test() | ||
| * @param {(args: { server: import('http').Server, url: string }) => void} resolve - see cds.once(..., resolve) | ||
| */ | ||
| then (resolve) { | ||
| if (this.server) { | ||
|
|
@@ -104,18 +126,20 @@ class Test extends require('./axios') { | |
|
|
||
| /** | ||
| * Captures console.log output. | ||
| * @param {(message?: any, ...optionalParams: any[]) => void} capture | ||
| */ | ||
| log (_capture) { | ||
| log (capture) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed, as underscore is generally used for "variables I don't use", "don't care", or "throwaway" (both, in Python, as well as in the ESLint rules |
||
| const {console} = global, {format} = require('util') | ||
| const log = { output: '' } | ||
| const log = { output: '', release: ()=>{}, clear: ()=>{} } | ||
| // @ts-expect-error - __proto__ hack | ||
| beforeAll(()=> global.console = { __proto__: console, | ||
| log: _capture ??= (..._)=> log.output += format(..._)+'\n', | ||
| info: _capture, | ||
| warn: _capture, | ||
| debug: _capture, | ||
| trace: _capture, | ||
| error: _capture, | ||
| timeEnd: _capture, time: ()=>{}, | ||
| log: capture ??= (..._)=> log.output += format(..._)+'\n', | ||
| info: capture, | ||
| warn: capture, | ||
| debug: capture, | ||
| trace: capture, | ||
| error: capture, | ||
| timeEnd: capture, time: ()=>{}, | ||
| }) | ||
| afterAll (log.release = ()=>{ log.output = ''; global.console = console }) | ||
| afterEach (log.clear = ()=>{ log.output = '' }) | ||
|
|
@@ -126,6 +150,7 @@ class Test extends require('./axios') { | |
| * Silences all console log output, e.g.: CDS_TEST_SILENT=y jest/mocha ... | ||
| */ | ||
| silent(){ | ||
| // @ts-expect-error - __proto__ hack | ||
| global.console = { __proto__: console, | ||
| log: ()=>{}, | ||
| info: ()=>{}, | ||
|
|
@@ -152,6 +177,10 @@ class Test extends require('./axios') { | |
| const chaip = require('chai-as-promised') | ||
| chai.use (chaip.default/*v8 on ESM*/ ?? chaip/*v7*/) | ||
| return chai | ||
|
|
||
| /** | ||
| * @param {string} mod | ||
| */ | ||
| function require (mod) { try { return module.require(mod) } catch(e) { | ||
| if (e.code === 'MODULE_NOT_FOUND') | ||
| throw new Error (`Failed to load required package '${mod}'. Please add it thru:` | ||
|
|
@@ -172,8 +201,13 @@ class Test extends require('./axios') { | |
| } | ||
|
|
||
|
|
||
| /** @type Test & ()=>Test */ | ||
| module.exports = exports = Object.assign ((..._) => (new Test).run(..._), { Test }) | ||
| /** @type {import('node:module').Module} */(module).exports = exports = Object.assign ( | ||
| /** | ||
| * @param {any[]} _ | ||
| */ | ||
| (..._) => (new Test).run(..._) | ||
| , { Test } | ||
| ) | ||
|
|
||
| // Set prototype to allow usages like cds.test.in(), cds.test.log(), ... | ||
| Object.setPrototypeOf (exports, Test.prototype) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the signature for this parameter? I derived it from
node-test.jswhere it is used asexpect(this), but no idea what the proper type should be there.