Skip to content

Commit a06ff79

Browse files
committed
chore(test): Add feature-gated and matrix based unit testing in CI
1 parent 7469bbd commit a06ff79

File tree

6 files changed

+114
-85
lines changed

6 files changed

+114
-85
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ jobs:
99
strategy:
1010
matrix:
1111
node-version: [10.x]
12+
feature: ['', 'transformOverloads']
1213

1314
steps:
1415
- uses: actions/checkout@v2
1516
- name: Use Node.js ${{ matrix.node-version }}
1617
uses: actions/setup-node@v1
1718
with:
1819
node-version: ${{ matrix.node-version }}
19-
- name: install ts auto mock and run test
20+
- name: install ts auto mock and run test - ${{ matrix.feature }}
2021
run: |
2122
sudo apt-get update
2223
sudo apt-get install -y libgbm-dev
@@ -25,5 +26,4 @@ jobs:
2526
npm test
2627
env:
2728
CI: true
28-
29-
29+
FEATURE: ${{ matrix.feature }}

config/karma/karma.config.base.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ module.exports = function(config, url) {
66
const processService = ProcessService(process);
77
const debug = processService.getArgument('DEBUG');
88
const disableCache = processService.getArgument('DISABLECACHE');
9+
const feature = processService.getArgument('FEATURE') || process.env.FEATURE;
910

1011
return {
1112
basePath: '',
1213
frameworks: ['jasmine'],
13-
webpack: webpackConfig(debug, disableCache),
14+
webpack: webpackConfig(debug, disableCache, feature),
1415
webpackMiddleware: {
1516
stats: 'errors-only'
1617
},

config/test/webpack.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const transformer = require('../../dist/transformer');
22
const path = require('path');
3+
const webpack = require('webpack');
34

4-
module.exports = function (debug, disableCache) {
5+
module.exports = function (debug, disableCache, feature = '') {
56
return {
67
mode: "development",
78
resolve: {
@@ -12,6 +13,11 @@ module.exports = function (debug, disableCache) {
1213
['ts-auto-mock/extension']: path.join(__dirname, '../../dist/extension'),
1314
}
1415
},
16+
plugins: [
17+
new webpack.DefinePlugin({
18+
'process.env.FEATURE': `"${feature}"`,
19+
}),
20+
],
1521
module: {
1622
rules: [
1723
{
@@ -27,7 +33,7 @@ module.exports = function (debug, disableCache) {
2733
before: [transformer.default(program, {
2834
debug: debug ? debug : false,
2935
cacheBetweenTests: disableCache !== 'true',
30-
transformOverloads: true,
36+
features: [feature],
3137
})]
3238
})
3339
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { createMock } from 'ts-auto-mock';
2+
3+
import {
4+
exportedDeclaredOverloadedFunction,
5+
ExportedDeclaredClass,
6+
} from '../utils/typeQuery/typeQueryUtils';
7+
8+
const isFeatureEnabled: boolean = process.env.FEATURE === 'transformOverloads';
9+
10+
describe('feature', () => {
11+
12+
describe('for overloads', () => {
13+
14+
describe('for type query', () => {
15+
16+
it('should assign the correct function mock for literal inputs', () => {
17+
const functionMock: typeof exportedDeclaredOverloadedFunction = createMock<typeof exportedDeclaredOverloadedFunction>();
18+
19+
// eslint-disable-next-line
20+
const expectations = [
21+
['', 0, false],
22+
[false, '', 0],
23+
[0, false, ''],
24+
[false, false, false],
25+
[''],
26+
[false],
27+
[0],
28+
];
29+
30+
for (const args of expectations) {
31+
// eslint-disable-next-line
32+
const [first] = args;
33+
34+
// @ts-ignore
35+
expect(functionMock(...args)).toEqual(isFeatureEnabled ? first : false);
36+
}
37+
});
38+
39+
it('should assign the correct function mock for mockable inputs', () => {
40+
const classMock: typeof ExportedDeclaredClass = createMock<typeof ExportedDeclaredClass>();
41+
42+
const functionMock: typeof exportedDeclaredOverloadedFunction = createMock<typeof exportedDeclaredOverloadedFunction>();
43+
44+
if (isFeatureEnabled) {
45+
expect(functionMock(new classMock()).prop).toBe(0);
46+
} else {
47+
expect(functionMock(new classMock()).prop).toBeUndefined();
48+
}
49+
});
50+
51+
});
52+
53+
describe('for interface', () => {
54+
describe('for construct signature', () => {
55+
interface InterfaceWithConstructSignatureOverload {
56+
new (a: number): { a: number };
57+
new (b: string): { b: string };
58+
new (): { c: Date };
59+
}
60+
61+
it('should use the correct signature as requested by input', () => {
62+
const properties: InterfaceWithConstructSignatureOverload = createMock<InterfaceWithConstructSignatureOverload>();
63+
64+
expect((new properties(0)).a).toBe(0);
65+
66+
if (isFeatureEnabled) {
67+
expect((new properties('')).b).toBe('');
68+
expect((new properties()).c).toBeInstanceOf(Date);
69+
} else {
70+
expect((new properties('')).b).toBeUndefined();
71+
expect((new properties()).c).toBeUndefined();
72+
}
73+
});
74+
});
75+
});
76+
77+
describe('call signature', () => {
78+
interface InterfaceWithCallSignature {
79+
(a: number): number;
80+
(a: string): string;
81+
b: string;
82+
}
83+
84+
it('should consider all signature declarations and properties', () => {
85+
const properties: InterfaceWithCallSignature = createMock<InterfaceWithCallSignature>();
86+
87+
expect(properties.b).toBe('');
88+
expect(properties(2)).toBe(0);
89+
90+
if (isFeatureEnabled) {
91+
expect(properties('2')).toBe('');
92+
} else {
93+
// @ts-ignore
94+
expect(properties('2')).toBe(0);
95+
}
96+
});
97+
});
98+
99+
});
100+
});

test/transformer/descriptor/methods/overloads.test.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.

tsconfig.playground.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{
77
"transform": "./dist/transformer",
88
"debug": "console",
9-
"transformOverloads": true
9+
"features": ["transformOverloads"]
1010
}
1111
]
1212
},
@@ -15,4 +15,3 @@
1515
],
1616
"include": []
1717
}
18-

0 commit comments

Comments
 (0)