Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support abstract classes when mocking statics #37

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module.exports = {
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
],
parser: '@typescript-eslint/parser',
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Build

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
workflow_dispatch:

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build-release
17 changes: 17 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: NPM Publish
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v2
with:
node-version: '14.x'
- run: npm ci
- uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.NPM_TOKEN }}
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion karma.base.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require('path');

process.env.CHROME_BIN = require('puppeteer').executablePath();

module.exports = function(config) {
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ console.log(`testPath: ${testEntryFile}`);
console.log(`configFile: ${configFile}`);
console.log(`coverage: ${coverage}`);

module.exports = function(config) {
module.exports = function (config) {
baseConfig(config);

const reporters = ['progress', 'kjhtml', 'junit'];
Expand Down
2 changes: 1 addition & 1 deletion main/helper/module-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const MockMap = new Map<string, any[] | undefined>();
export function proxyModule<T>(originalModule: T): WrappedModule & T {
const wrappedModule = { ___moduleId: v4() } as WrappedModule & T;

Object.keys(originalModule).forEach(key => {
Object.keys(originalModule).forEach((key) => {
const originalValue = (originalModule as any)[key];
(wrappedModule as any)[key] =
typeof originalValue === 'function' ? wrapFunction(wrappedModule, key, originalValue) : originalValue;
Expand Down
6 changes: 3 additions & 3 deletions main/helper/property-replacement-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* We need to do this as webpack builds immutable export objects that uses Object.defineProperty with configurable set to false
* This means we are unable to replace the imports at runtime and mock them.
*/
(defineProperty => {
((defineProperty) => {
Object.defineProperty = (obj, prop, desc) => {
desc.configurable = true;
return defineProperty(obj, prop, desc);
Expand Down Expand Up @@ -89,7 +89,7 @@ export function replacePropertiesBeforeEach(callback: () => IImportReplacement<a
beforeEach(() => {
const mockedImports = callback();

mockedImports.forEach(importReplacement => {
mockedImports.forEach((importReplacement) => {
importCopies.push({
packageWithReplacements: importReplacement.package,
descriptors: getDescriptors(importReplacement.package, importReplacement.mocks),
Expand All @@ -100,7 +100,7 @@ export function replacePropertiesBeforeEach(callback: () => IImportReplacement<a
});

afterEach(() => {
importCopies.forEach(copy => revertImports(copy.packageWithReplacements, copy.descriptors));
importCopies.forEach((copy) => revertImports(copy.packageWithReplacements, copy.descriptors));
});
}

Expand Down
4 changes: 2 additions & 2 deletions main/mock/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type LookupParams<
T,
C extends ConstructorFunction<T>,
U extends LookupType,
K extends FunctionName<T, C, U>
K extends FunctionName<T, C, U>,
> = U extends FunctionTypes
? FunctionParams<VerifierTarget<T, C, U>[K]>
: U extends SetterTypes
Expand Down Expand Up @@ -72,7 +72,7 @@ export interface IFunctionWithParametersVerification<
P extends Array<any>,
T,
U extends LookupType,
C extends new (...args: any[]) => T = never
C extends new (...args: any[]) => T = never,
> extends IFunctionVerifier<T, U, C> {
/**
* Checks the parameters in a non-strict equality way.
Expand Down
2 changes: 1 addition & 1 deletion main/mock/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Mock {

setup: (...operators: OperatorFunction<T, C>[]) => {
let operatorMocked = mocked;
operators.forEach(operator => (operatorMocked = operator(mocked)));
operators.forEach((operator) => (operatorMocked = operator(mocked)));
return operatorMocked;
},

Expand Down
6 changes: 3 additions & 3 deletions main/mock/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function setupFunction<T, C extends ConstructorFunction<T>, U extends Fun
export function setupStaticFunction<
T,
C extends ConstructorFunction<T>,
U extends FunctionName<T, C, 'staticFunction'>
U extends FunctionName<T, C, 'staticFunction'>,
>(functionName: U, mockFunction?: C[U]): OperatorFunction<T, C> {
return (mocked: IMocked<T, C>) => {
const functionReplacement = (...args: LookupParams<T, C, 'staticFunction', U>) => {
Expand Down Expand Up @@ -162,7 +162,7 @@ function definePropertyImpl<
T,
C extends ConstructorFunction<T>,
U extends GetterTypes,
K extends FunctionName<T, C, U>
K extends FunctionName<T, C, U>,
>(
mocked: IMocked<T, C>,
lookupType: U,
Expand Down Expand Up @@ -242,7 +242,7 @@ function trackFunctionCall<
T,
C extends ConstructorFunction<T>,
U extends FunctionTypes,
K extends FunctionName<T, C, U>
K extends FunctionName<T, C, U>,
>(mock: IMocked<T, C>, lookupType: U, functionName: K, params: LookupParams<T, C, U, K>) {
const lookup = getLookup(mock, lookupType);

Expand Down
8 changes: 4 additions & 4 deletions main/mock/parameterMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function isParameterMatcher(matcher: unknown): matcher is IParameterMatch
*/
export function toBeDefined(): IParameterMatcher<any> {
return {
isExpectedValue: actualValue => actualValue !== undefined,
isExpectedValue: (actualValue) => actualValue !== undefined,
expectedDisplayValue: '<mustBeDefined>',
};
}
Expand All @@ -29,7 +29,7 @@ export function toBeDefined(): IParameterMatcher<any> {
*/
export function hasValue(): IParameterMatcher<any> {
return {
isExpectedValue: actualValue => actualValue != null,
isExpectedValue: (actualValue) => actualValue != null,
expectedDisplayValue: '<hasValue>',
};
}
Expand All @@ -43,7 +43,7 @@ export function hasValue(): IParameterMatcher<any> {
export function toBe(expectedValue: any): IParameterMatcher<any> {
return {
expectedDisplayValue: mapItemToString(expectedValue),
isExpectedValue: actualValue => actualValue === expectedValue,
isExpectedValue: (actualValue) => actualValue === expectedValue,
};
}

Expand All @@ -56,7 +56,7 @@ export function toBe(expectedValue: any): IParameterMatcher<any> {
export function toEqual(expectedValue: any): IParameterMatcher<any> {
return {
expectedDisplayValue: mapItemToString(expectedValue),
isExpectedValue: actualValue => isEqual(actualValue, expectedValue),
isExpectedValue: (actualValue) => isEqual(actualValue, expectedValue),
};
}

Expand Down
22 changes: 11 additions & 11 deletions main/mock/verifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export type VerifierParams<
T,
C extends ConstructorFunction<T>,
U extends LookupType,
K extends FunctionName<T, C, U>
K extends FunctionName<T, C, U>,
> = U extends SetterTypes ? [VerifierTarget<T, C, U>[K]] : FunctionParams<VerifierTarget<T, C, U>[K]>;

export function createFunctionParameterVerifier<
T,
C extends ConstructorFunction<T>,
U extends LookupType,
K extends FunctionName<T, C, U>
K extends FunctionName<T, C, U>,
>(
mocked: IMocked<T, C>,
type: U,
Expand Down Expand Up @@ -60,7 +60,7 @@ export function createFunctionVerifier<
T,
C extends ConstructorFunction<T>,
U extends LookupType,
K extends FunctionName<T, C, U>
K extends FunctionName<T, C, U>,
>(
mocked: IMocked<T, C>,
type: U,
Expand All @@ -81,7 +81,7 @@ export function createStrictFunctionVerifier<
T,
C extends ConstructorFunction<T>,
U extends LookupType,
K extends FunctionName<T, C, U>
K extends FunctionName<T, C, U>,
>(
mocked: IMocked<T, C>,
type: U,
Expand All @@ -98,7 +98,7 @@ export function verifyParameters<
T,
C extends ConstructorFunction<T>,
U extends LookupType,
K extends FunctionName<T, C, U>
K extends FunctionName<T, C, U>,
>(
parameters: ParameterMatcher<any>[],
mocked: IMocked<T, C>,
Expand Down Expand Up @@ -188,14 +188,14 @@ export function verifyFunctionCalled<T, C extends ConstructorFunction<T>, U exte
);
}

const parameterMatchResults = functionCalls.map(params => matchParameters(params, parameterMatchers));
const parameterMatchResults = functionCalls.map((params) => matchParameters(params, parameterMatchers));
const customMatcherResults = parameterMatchResults.filter(isMatcherResultArray);

if (customMatcherResults.length > 0) {
return customMatcherResults[0][0];
}

const matchingCalls = parameterMatchResults.filter(paramMatch => paramMatch === true);
const matchingCalls = parameterMatchResults.filter((paramMatch) => paramMatch === true);

if (times !== undefined) {
if (times !== matchingCalls.length || (strict && times !== functionCalls.length)) {
Expand Down Expand Up @@ -263,8 +263,8 @@ function buildAllCallsString(
parameterMatchers: (IParameterMatcher<any> | MatchFunction<any>)[] | undefined,
) {
let allCalls: string;
if (functionCalls.some(call => call.length > 0)) {
allCalls = `\n[\n${functionCalls.map(call => functionCallToString(call, parameterMatchers)).join('\n')}\n]`;
if (functionCalls.some((call) => call.length > 0)) {
allCalls = `\n[\n${functionCalls.map((call) => functionCallToString(call, parameterMatchers)).join('\n')}\n]`;
} else {
allCalls = '';
}
Expand All @@ -273,7 +273,7 @@ function buildAllCallsString(

function expectedParametersToString(parameterMatchers: ParameterMatcher<any>[]): string {
return parameterMatchers
.map(matcher => {
.map((matcher) => {
return isParameterMatcher(matcher) ? matcher.expectedDisplayValue : '<customParameterMatchFunction>';
})
.join(', ');
Expand Down Expand Up @@ -333,7 +333,7 @@ function matchParameters(
);
const matcherResults = evaluatedParams.filter(isMatcherResult);

return matcherResults.length > 0 ? matcherResults : (evaluatedParams as boolean[]).every(param => param === true);
return matcherResults.length > 0 ? matcherResults : (evaluatedParams as boolean[]).every((param) => param === true);
}

function evaluateParameterMatcher(
Expand Down
Loading