Skip to content

Commit

Permalink
fix(transformer): fix interface call signature with undeclared return…
Browse files Browse the repository at this point in the history
… type (#533)
  • Loading branch information
Pmyl authored Sep 15, 2020
1 parent bfccb38 commit 55da89f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 19 deletions.
28 changes: 18 additions & 10 deletions definitelyTypedTests/src/processRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,14 @@ async function run(dir, processId) {

try {
const typeTsConfig = JSON.parse(typeTsConfigFileContent);

if (typeTsConfig && typeTsConfig.compilerOptions) {
if (typeTsConfig.compilerOptions.paths) {
config.compilerOptions.paths = typeTsConfig.compilerOptions.paths;
}

if (typeTsConfig.compilerOptions.lib) {
config.compilerOptions.lib = typeTsConfig.compilerOptions.lib;
}
}
copyTsConfigValues(typeTsConfig, config,
'paths',
'lib',
'types',
'esModuleInterop',
'target',
'module'
);
} catch {
console.warn('tsconfig.json of type found but failed to parse.');
}
Expand Down Expand Up @@ -138,4 +136,14 @@ createDefinitelyTypedMock<typeof pak>();
});
}

function copyTsConfigValues(typeTsConfig, config, ...props) {
if (typeTsConfig && typeTsConfig.compilerOptions) {
props.forEach(prop => {
if (typeTsConfig.compilerOptions[prop]) {
config.compilerOptions[prop] = typeTsConfig.compilerOptions[prop];
}
});
}
}

module.exports = runProcesses;
9 changes: 4 additions & 5 deletions src/transformer/descriptor/method/functionType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as ts from 'typescript';
import { Scope } from '../../scope/scope';
import { GetDescriptor } from '../descriptor';
import { PropertySignatureCache } from '../property/cache';
import { GetUndefinedDescriptor } from '../undefined/undefined';
import { GetMethodDescriptor } from './method';

export function GetFunctionTypeDescriptor(
Expand All @@ -13,11 +14,9 @@ export function GetFunctionTypeDescriptor(
): ts.Expression {
const property: ts.PropertyName = PropertySignatureCache.instance.get();

if (!node.type) {
throw new Error(`No type was declared for ${node.getText()}.`);
}

const returnValue: ts.Expression = GetDescriptor(node.type, scope);
const returnValue: ts.Expression = node.type
? GetDescriptor(node.type, scope)
: GetUndefinedDescriptor();

return GetMethodDescriptor(property, returnValue);
}
13 changes: 13 additions & 0 deletions test/transformer/descriptor/methods/methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ describe('for methods', () => {
});
});

describe('for interface call signature with undeclared return value', () => {
interface InterfaceWithCallSignature {
(a: number);
}

it('should set the function with undefined return value', () => {
const properties: InterfaceWithCallSignature = createMock<
InterfaceWithCallSignature
>();
expect(properties(1)).toBeUndefined();
});
});

describe('for interface construct signature', () => {
interface InterfaceWithConstructSignature {
new (a: number): { a: number };
Expand Down
6 changes: 4 additions & 2 deletions ui/src/views/config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ route: /config

# Config
```ts
tsAutoMockTransformer(program: ts.Program, options: TsAutoMockOptions)

interface TsAutoMockOptions {
debug: boolean | 'file' | 'console';
cacheBetweenTests: boolean;
Expand All @@ -31,12 +29,16 @@ options:

---

There are different ways to pass options to a transformer, every installation type has its own way, to know how to do it
find your configuration in the [Installation page](./installation).

## Debug
We currently support:
- Logs for [not supported types](./types-not-supported)

It will log any not supported type automatically converted to null.
This is useful to report an issue or to investigate a potential bug.

---

## CacheBetweenTests
Expand Down
66 changes: 64 additions & 2 deletions ui/src/views/installation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ There are different ways to do this depending on how you are currently compiling

## `jest` + `ts-jest` + `ttypescript`

### Steps

1. Install the dependencies

```
Expand Down Expand Up @@ -138,10 +140,24 @@ npm run test

... and you are all done!

### Config options
In the tsconfig.json you can add the config options next to the transformer name:
```json
"plugins": [
{
"transform": "ts-auto-mock/transformer",
"cacheBetweenTests": false,
"features": ["random"]
}
]
```

## Webpack

With Webpack, You can use any TypeScript-related loader that supports custom transformers, e.g. `awesome-typescript-loader` or `ts-loader`:

### Steps

#### **`webpack.config.js`**
```js
const tsAutoMockTransformer = require('ts-auto-mock/transformer').default;
Expand All @@ -166,9 +182,21 @@ module.exports = {
};
```

### Config options
In webpack.config.js you can pass a second parameter to the transformer:
```js
before: [
tsAutoMockTransformer(program, {
features: ['random']
})
]
```

## ttypescript
See [ttypescript's README](https://github.com/cevek/ttypescript/blob/master/README.md) for more information

### Steps

#### **`tsconfig.json`**
```json
{
Expand All @@ -182,10 +210,23 @@ See [ttypescript's README](https://github.com/cevek/ttypescript/blob/master/READ
}
```

### Config options
In the tsconfig.json you can add the config options next to the transformer name:
```json
"plugins": [
{
"transform": "ts-auto-mock/transformer",
"cacheBetweenTests": false,
"features": ["random"]
}
]
```

## ts-patch
See [ts-patch's README](https://github.com/nonara/ts-patch/blob/master/README.md)

### Steps

Command to run to install it:

```
Expand All @@ -207,10 +248,21 @@ tsconfig.json
}
```

## ts-node
### Config options
In the tsconfig.json you can add the config options next to the transformer name:
```json
"plugins": [
{
"transform": "ts-auto-mock/transformer",
"cacheBetweenTests": false,
"features": ["random"]
}
]
```

### Mocha
## ts-node + Mocha

### Steps
#### **`tsnode.js`**
```js
const tsAutoMockTransformer = require('ts-auto-mock/transformer').default;
Expand All @@ -229,3 +281,13 @@ Then inject the registration with the `--require` flag that Mocha passes on to N
```
mocha --require './tsnode.js' --watch-extensions ts,tsx "test/**/*.{ts,tsx}"
```

### Config options
In tsnode.js you can pass a second parameter to the transformer:
```js
before: [
tsAutoMockTransformer(program, {
features: ['random']
})
]
```

0 comments on commit 55da89f

Please sign in to comment.