Skip to content

Commit

Permalink
chore(test): Add feature-gated and matrix based unit testing in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjlowm committed May 19, 2020
1 parent 7469bbd commit a06ff79
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 85 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ jobs:
strategy:
matrix:
node-version: [10.x]
feature: ['', 'transformOverloads']

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
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
Expand All @@ -25,5 +26,4 @@ jobs:
npm test
env:
CI: true


FEATURE: ${{ matrix.feature }}
3 changes: 2 additions & 1 deletion config/karma/karma.config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
},
Expand Down
10 changes: 8 additions & 2 deletions config/test/webpack.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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: [
{
Expand All @@ -27,7 +33,7 @@ module.exports = function (debug, disableCache) {
before: [transformer.default(program, {
debug: debug ? debug : false,
cacheBetweenTests: disableCache !== 'true',
transformOverloads: true,
features: [feature],
})]
})
}
Expand Down
100 changes: 100 additions & 0 deletions test/transformer/descriptor/methods/feature.overloads.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof exportedDeclaredOverloadedFunction>();

// 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<typeof ExportedDeclaredClass>();

const functionMock: typeof exportedDeclaredOverloadedFunction = createMock<typeof exportedDeclaredOverloadedFunction>();

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<InterfaceWithConstructSignatureOverload>();

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<InterfaceWithCallSignature>();

expect(properties.b).toBe('');
expect(properties(2)).toBe(0);

if (isFeatureEnabled) {
expect(properties('2')).toBe('');
} else {
// @ts-ignore
expect(properties('2')).toBe(0);
}
});
});

});
});
77 changes: 0 additions & 77 deletions test/transformer/descriptor/methods/overloads.test.ts

This file was deleted.

3 changes: 1 addition & 2 deletions tsconfig.playground.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
"transform": "./dist/transformer",
"debug": "console",
"transformOverloads": true
"features": ["transformOverloads"]
}
]
},
Expand All @@ -15,4 +15,3 @@
],
"include": []
}

0 comments on commit a06ff79

Please sign in to comment.