Skip to content

Commit

Permalink
feat(import): add import equals support and make sure transformer tes…
Browse files Browse the repository at this point in the history
…t run in a context so the cache system will work

* move import test into his own folder and remove unnecessary wording in readme for slack link

* add import alias tests

* add import equals support

* remove duplicated test

* rename import changed by mistake in previous commit

* make sure transformer test run with a context so the cache feature can be tested
  • Loading branch information
uittorio authored Dec 8, 2019
1 parent b0aa18a commit f23039d
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 65 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![npm version](https://badge.fury.io/js/ts-auto-mock.svg)](https://badge.fury.io/js/ts-auto-mock)
[![Downloads](https://img.shields.io/npm/dt/ts-auto-mock.svg)](https://www.npmjs.com/package/ts-auto-mock) [![Greenkeeper badge](https://badges.greenkeeper.io/Typescript-TDD/ts-auto-mock.svg)](https://greenkeeper.io/)

![slack](docs/slack_small.png) Need help? Join us on Slack [link](https://join.slack.com/t/typescripttdd/shared_invite/enQtODM3MzExODE0NTk2LTNmYzRhM2M1ZDc5ODVkMmVlZWFjMTM4ZDFhNWU2NDdiYWY1MGMxZjE2ZDE0ZDZlYjY1MTkyYjRhYTQ1NjA1MWQ)
![slack](docs/slack_small.png) Need help? Join us on [Slack](https://join.slack.com/t/typescripttdd/shared_invite/enQtODM3MzExODE0NTk2LTNmYzRhM2M1ZDc5ODVkMmVlZWFjMTM4ZDFhNWU2NDdiYWY1MGMxZjE2ZDE0ZDZlYjY1MTkyYjRhYTQ1NjA1MWQ)


A Typescript transformer that will allow you to create mock for any types (Interfaces, Classes, ...) without need to create manual fakes/mocks.
Expand Down
2 changes: 1 addition & 1 deletion config/karma/karma.config.transformer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const karmaBaseConfig = require('./karma.config.base');

module.exports = function(config) {
const karmaConfig = karmaBaseConfig(config, '../../test/transformer/**/*.test.ts');
const karmaConfig = karmaBaseConfig(config, '../../test/transformer/context.ts');

config.set(karmaConfig);
};
49 changes: 34 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"tslint": "^5.20.1",
"tslint-loader": "^3.5.4",
"ttypescript": "1.5.8",
"typescript": "^3.7.2",
"typescript": "^3.7.3",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10",
"webpack-merge": "^4.2.2",
Expand All @@ -66,7 +66,7 @@
"winston": "^3.2.1"
},
"peerDependencies": {
"typescript": "^3.7.2"
"typescript": "^3.7.3"
},
"husky": {
"hooks": {
Expand Down
3 changes: 3 additions & 0 deletions src/transformer/descriptor/descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { GetEnumDeclarationDescriptor } from './enum/enumDeclaration';
import { GetExpressionWithTypeArgumentsDescriptor } from './expression/expressionWithTypeArguments';
import { GetIdentifierDescriptor } from './identifier/identifier';
import { GetImportDescriptor } from './import/import';
import { GetImportEqualsDescriptor } from './import/importEquals';
import { GetInterfaceDeclarationDescriptor } from './interface/interfaceDeclaration';
import { GetIntersectionDescriptor } from './intersection/intersection';
import { GetLiteralDescriptor } from './literal/literal';
Expand Down Expand Up @@ -109,6 +110,8 @@ export function GetDescriptor(node: ts.Node, scope: Scope): ts.Expression {
return GetMockPropertiesFromSymbol([], [], scope);
case ts.SyntaxKind.NullKeyword:
return GetNullDescriptor();
case ts.SyntaxKind.ImportEqualsDeclaration:
return GetImportEqualsDescriptor(node as ts.ImportEqualsDeclaration, scope);
case ts.SyntaxKind.AnyKeyword:
case ts.SyntaxKind.NeverKeyword:
case ts.SyntaxKind.UnknownKeyword:
Expand Down
4 changes: 4 additions & 0 deletions src/transformer/descriptor/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export namespace TypescriptHelper {
return GetDeclarationForImport(declaration) as ts.Declaration;
}

if (ts.isImportEqualsDeclaration(declaration)) {
return GetDeclarationFromNode(declaration.moduleReference);
}

return declaration;
}

Expand Down
9 changes: 9 additions & 0 deletions src/transformer/descriptor/import/importEquals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as ts from 'typescript';
import { Scope } from '../../scope/scope';
import { GetDescriptor } from '../descriptor';
import { TypescriptHelper } from '../helper/helper';

export function GetImportEqualsDescriptor(node: ts.ImportEqualsDeclaration, scope: Scope): ts.Expression {
const declaration: ts.Declaration = TypescriptHelper.GetDeclarationFromNode(node.moduleReference);
return GetDescriptor(declaration, scope);
}
1 change: 0 additions & 1 deletion src/transformer/mockDefiner/mockDefiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export class MockDefiner {
this._factoryCache = new DeclarationCache();
this._declarationCache = new DeclarationCache();
this._factoryIntersectionCache = new DeclarationListCache();
this._factoryUniqueName = new FactoryUniqueName();
}
this._factoryRegistrationsPerFile[sourceFile.fileName] = [];
this._factoryIntersectionsRegistrationsPerFile[sourceFile.fileName] = [];
Expand Down
2 changes: 2 additions & 0 deletions test/transformer/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const frameworkContext: __WebpackModuleApi.RequireContext = require.context('./', true, /\.test\.ts$/);
frameworkContext.keys().map(frameworkContext);
41 changes: 0 additions & 41 deletions test/transformer/descriptor/create-mock-list-values.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createMock } from 'ts-auto-mock';
import ImportDefaultInterface from './utils/interfaces/importDefaultInterface';
import { ImportInterface, ImportInterfaceWithNestedInterface } from './utils/interfaces/importInterface';
import { ImportNamespace } from './utils/interfaces/importNameSpace';
import { ImportType, Type } from './utils/types/type';
import ImportDefaultInterface from '../utils/interfaces/importDefaultInterface';
import { ImportInterface, ImportInterfaceWithNestedInterface } from '../utils/interfaces/importInterface';
import { ImportNamespace } from '../utils/interfaces/importNameSpace';
import { ImportType, Type } from '../utils/types/type';

describe('with import', () => {
describe('for interfaces', () => {
Expand Down
17 changes: 17 additions & 0 deletions test/transformer/descriptor/import/importAlias.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createMock } from 'ts-auto-mock';
import { Interface as InterfaceAlias } from '../utils/interfaces/basic';
import { InterfaceAliasExport } from '../utils/interfaces/importAndExport';

describe('importAlias', () => {
it('should use the correct import for an alias', () => {
const mock: InterfaceAlias = createMock<InterfaceAlias>();
expect(mock.a).toBe('');
expect(mock.b).toBe(0);
});

it('should use the correct import for an internal alias', () => {
const mock: InterfaceAliasExport = createMock<InterfaceAliasExport>();
expect(mock.a).toBe('');
expect(mock.b).toBe(0);
});
});
25 changes: 25 additions & 0 deletions test/transformer/descriptor/import/importEqual.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createMock } from 'ts-auto-mock';
import { NameSpaceInterfaceImport } from '../utils/namespace/namespace';
import Interface = NameSpaceInterfaceImport.Interface;
import SubInterface = NameSpaceInterfaceImport.SubNamespace.SubInterface;
import Enum = NameSpaceInterfaceImport.Enum;

describe('import equal', () => {
it('should use the correct import for an interface', () => {
const mock: Interface = createMock<Interface>();
expect(mock.a).toBe('');
});

it('should use the correct import for a literal', () => {
interface InterfaceWithEnumFromModule {
enum: Enum;
}
const mock: InterfaceWithEnumFromModule = createMock<InterfaceWithEnumFromModule>();
expect(mock.enum).toBe(Enum.A);
});

it('should use the correct import for a sub module interface', () => {
const mock: SubInterface = createMock<SubInterface>();
expect(mock.a).toBe('');
});
});
4 changes: 4 additions & 0 deletions test/transformer/descriptor/utils/interfaces/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Interface {
a: string;
b: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Interface as InterfaceAliasExport } from './basic';

export { InterfaceAliasExport };
11 changes: 11 additions & 0 deletions test/transformer/descriptor/utils/namespace/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@ export namespace NameSpaceInterfaceImport {
export interface Interface {
a: string;
}

export enum Enum {
A,
B,
}

export namespace SubNamespace {
export interface SubInterface {
a: string;
}
}
}

0 comments on commit f23039d

Please sign in to comment.