diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5619ff249..a346131d6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,6 +9,7 @@ jobs: strategy: matrix: node-version: [10.x] + feature: ['', 'transformOverloads'] steps: - uses: actions/checkout@v2 @@ -16,7 +17,7 @@ jobs: uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - - name: install ts auto mock and run test + - name: install ts auto mock and run test - ${{ matrix.feature }} run: | sudo apt-get update sudo apt-get install -y libgbm-dev @@ -25,5 +26,4 @@ jobs: npm test env: CI: true - - + FEATURE: ${{ matrix.feature }} diff --git a/config/karma/karma.config.base.js b/config/karma/karma.config.base.js index ced84a659..09fea030a 100644 --- a/config/karma/karma.config.base.js +++ b/config/karma/karma.config.base.js @@ -6,11 +6,12 @@ module.exports = function(config, url) { const processService = ProcessService(process); const debug = processService.getArgument('DEBUG'); const disableCache = processService.getArgument('DISABLECACHE'); + const feature = processService.getArgument('FEATURE') || process.env.FEATURE; return { basePath: '', frameworks: ['jasmine'], - webpack: webpackConfig(debug, disableCache), + webpack: webpackConfig(debug, disableCache, feature), webpackMiddleware: { stats: 'errors-only' }, diff --git a/config/test/webpack.js b/config/test/webpack.js index b8b6de91a..7bd368194 100644 --- a/config/test/webpack.js +++ b/config/test/webpack.js @@ -1,7 +1,8 @@ const transformer = require('../../dist/transformer'); const path = require('path'); +const webpack = require('webpack'); -module.exports = function (debug, disableCache) { +module.exports = function (debug, disableCache, feature = '') { return { mode: "development", resolve: { @@ -12,6 +13,11 @@ module.exports = function (debug, disableCache) { ['ts-auto-mock/extension']: path.join(__dirname, '../../dist/extension'), } }, + plugins: [ + new webpack.DefinePlugin({ + 'process.env.FEATURE': `"${feature}"`, + }), + ], module: { rules: [ { @@ -27,7 +33,7 @@ module.exports = function (debug, disableCache) { before: [transformer.default(program, { debug: debug ? debug : false, cacheBetweenTests: disableCache !== 'true', - transformOverloads: true, + features: [feature], })] }) } diff --git a/test/transformer/descriptor/methods/feature.overloads.test.ts b/test/transformer/descriptor/methods/feature.overloads.test.ts new file mode 100644 index 000000000..4fc9a25a0 --- /dev/null +++ b/test/transformer/descriptor/methods/feature.overloads.test.ts @@ -0,0 +1,100 @@ +import { createMock } from 'ts-auto-mock'; + +import { + exportedDeclaredOverloadedFunction, + ExportedDeclaredClass, +} from '../utils/typeQuery/typeQueryUtils'; + +const isFeatureEnabled: boolean = process.env.FEATURE === 'transformOverloads'; + +describe('feature', () => { + + describe('for overloads', () => { + + describe('for type query', () => { + + it('should assign the correct function mock for literal inputs', () => { + const functionMock: typeof exportedDeclaredOverloadedFunction = createMock(); + + // eslint-disable-next-line + const expectations = [ + ['', 0, false], + [false, '', 0], + [0, false, ''], + [false, false, false], + [''], + [false], + [0], + ]; + + for (const args of expectations) { + // eslint-disable-next-line + const [first] = args; + + // @ts-ignore + expect(functionMock(...args)).toEqual(isFeatureEnabled ? first : false); + } + }); + + it('should assign the correct function mock for mockable inputs', () => { + const classMock: typeof ExportedDeclaredClass = createMock(); + + const functionMock: typeof exportedDeclaredOverloadedFunction = createMock(); + + if (isFeatureEnabled) { + expect(functionMock(new classMock()).prop).toBe(0); + } else { + expect(functionMock(new classMock()).prop).toBeUndefined(); + } + }); + + }); + + describe('for interface', () => { + describe('for construct signature', () => { + interface InterfaceWithConstructSignatureOverload { + new (a: number): { a: number }; + new (b: string): { b: string }; + new (): { c: Date }; + } + + it('should use the correct signature as requested by input', () => { + const properties: InterfaceWithConstructSignatureOverload = createMock(); + + expect((new properties(0)).a).toBe(0); + + if (isFeatureEnabled) { + expect((new properties('')).b).toBe(''); + expect((new properties()).c).toBeInstanceOf(Date); + } else { + expect((new properties('')).b).toBeUndefined(); + expect((new properties()).c).toBeUndefined(); + } + }); + }); + }); + + describe('call signature', () => { + interface InterfaceWithCallSignature { + (a: number): number; + (a: string): string; + b: string; + } + + it('should consider all signature declarations and properties', () => { + const properties: InterfaceWithCallSignature = createMock(); + + expect(properties.b).toBe(''); + expect(properties(2)).toBe(0); + + if (isFeatureEnabled) { + expect(properties('2')).toBe(''); + } else { + // @ts-ignore + expect(properties('2')).toBe(0); + } + }); + }); + + }); +}); diff --git a/test/transformer/descriptor/methods/overloads.test.ts b/test/transformer/descriptor/methods/overloads.test.ts deleted file mode 100644 index 40b335e18..000000000 --- a/test/transformer/descriptor/methods/overloads.test.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { createMock } from 'ts-auto-mock'; - -import { - exportedDeclaredOverloadedFunction, - ExportedDeclaredClass, -} from '../utils/typeQuery/typeQueryUtils'; - -describe('for overloads', () => { - - describe('for type query', () => { - - it('should assign the correct function mock for literal inputs', () => { - const functionMock: typeof exportedDeclaredOverloadedFunction = createMock(); - - // eslint-disable-next-line - const expectations = [ - ['', 0, false], - [false, '', 0], - [0, false, ''], - [false, false, false], - [''], - [false], - [0], - ]; - - for (const args of expectations) { - // eslint-disable-next-line - const [first] = args; - - // @ts-ignore - expect(functionMock(...args)).toEqual(first); - } - }); - - it('should assign the correct function mock for mockable inputs', () => { - const classMock: typeof ExportedDeclaredClass = createMock(); - - const functionMock: typeof exportedDeclaredOverloadedFunction = createMock(); - - expect(functionMock(new classMock()).prop).toBe(0); - }); - - }); - - describe('for interface', () => { - describe('for construct signature', () => { - interface InterfaceWithConstructSignatureOverload { - new (a: number): { a: number }; - new (b: string): { b: string }; - new (): { c: Date }; - } - - it('should use the correct signature as requested by input', () => { - const properties: InterfaceWithConstructSignatureOverload = createMock(); - expect((new properties(0)).a).toBe(0); - expect((new properties('')).b).toBe(''); - expect((new properties()).c).toBeInstanceOf(Date); - }); - }); - }); - - describe('call signature', () => { - interface InterfaceWithCallSignature { - (a: number): number; - (a: string): string; - b: string; - } - - it('should consider all signature declarations and properties', () => { - const properties: InterfaceWithCallSignature = createMock(); - expect(properties(2)).toBe(0); - expect(properties('2')).toBe(''); - expect(properties.b).toBe(''); - }); - }); - -}); diff --git a/tsconfig.playground.json b/tsconfig.playground.json index 7291fbba8..3ded2334e 100644 --- a/tsconfig.playground.json +++ b/tsconfig.playground.json @@ -6,7 +6,7 @@ { "transform": "./dist/transformer", "debug": "console", - "transformOverloads": true + "features": ["transformOverloads"] } ] }, @@ -15,4 +15,3 @@ ], "include": [] } -